1. 程式人生 > 程式設計 >Docker容器學習(三)之Docker 映象構建

Docker容器學習(三)之Docker 映象構建

dockerlogo

Docker 映象構建一般使用Dockerfile,首先我們需要了解Dockerfile語法(Dockerfile官方檔案),然後我們編寫好Dockerfile檔案之後就可以開始構建我們的專案對應Docker映象,如果構建呢?我們可以手動使用docker命令構建,也可以使用開源外掛幫助構建,請往下看。

Dockerfile 構建映象

自己手動構建

  • 首先我們使用maven 命令將專案打包成 jar 包(jar包一般生成在專案的target目錄中)
 mvn install
複製程式碼
  • 將jar包複製到伺服器我們想要的位置,使用docker命令構建docker映象
# 格式:docker build -t 標籤名稱 Dockerfile的相對位置
docker build -t configserver . 複製程式碼
  • Dockerfile 內容
# 基於哪個映象
FROM java:8

# 將本地資料夾掛載到當前容器
VOLUME /data/cloudtest

# 拷貝檔案到容器,也可以直接寫成ADD xxxxx.jar /app.jar
ADD configserver-0.0.1-SNAPSHOT.jar  /app.jar

# 宣告需要暴露的埠
EXPOSE 8666

# 配置容器啟動後執行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar"
,"/app.jar"] 複製程式碼
  • 啟動構建好的映象
docker run -p 8666:8666 configserver
複製程式碼

使用外掛來構建映象

  • 使用外掛之前,如果本地環境中沒有安裝docker,則我們也可以使用遠端伺服器安裝的docker來幫助我們構建,如何使用遠端docker呢,首先我們要開啟docker的遠端訪問

Centos7中docker開啟遠端訪問

  • 在/usr/lib/systemd/system/docker.service,配置遠端訪問。主要是在[Service]這個部分,加上下面兩個引數
# vim /usr/lib/systemd/system/docker.service
[Service] ExecStart= ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock 複製程式碼
  • 以上配置完成之後我們需要重新讀取docker配置檔案,重新啟動docker服務
systemctl daemon-reload
systemctl restart docker
複製程式碼
  • 看程式docker是否已經監聽2375埠,命令為 ps aux | grep docker
root      8902  0.5  0.1 637948 37388 ?        Ssl  10:34   1:30 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
root      8909  0.0  0.0 432312 10904 ?        Ssl  10:34   0:09 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
複製程式碼
  • 此時docker已經開啟遠端訪問地址為 伺服器地址:2375(別忘了伺服器防火牆需要開放2375埠)

docker-maven-plugin(spotify)

  • 使用docker-maven-plugin來構建映象
  • 配置POM build 模組,使用docker 映象私服(nexus)作為映象倉庫
<build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <configuration>
                    <imageName>172.31.116.12:9290/mao/microservice-discovery-eureka:0.0.1</imageName>
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!--遠端docker 地址-->
                    <dockerHost>http://172.31.76.16:2375</dockerHost>
                    <!--<dockerCertPath></dockerCertPath>-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>

                    <!-- 與maven配置檔案settings.xml中配置的server.id一致,用於推送映象 -->
                    <serverId>docker-nexus</serverId>
                </configuration>
            </plugin>
        </plugins>
    </build>
複製程式碼
  • 建立並上傳映象執行
mvn clean package docker:build  -DpushImage
複製程式碼
  • 構建成功,使用docker 檢視構建成功的映象

使用docker-maven-plugin來構建映象成功
docker-maven-plugin構建成功的映象

  • 可藉助imageTags元素更為靈活地指定映象名稱和標籤
<build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <configuration>
                    <imageName>172.31.116.12:9390/test/eureka</imageName>
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!--遠端docker 地址-->
                    <dockerHost>http://172.31.76.16:2375</dockerHost>
                    <!--<dockerCertPath></dockerCertPath>-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                           <!--Dockerfile檔案路徑 此路徑代表本專案根目錄--> <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                    <!--重複構建相同標籤名稱的映象,可將forceTags設為true,這樣就會覆蓋構建相同標籤的映象-->
                    <forceTags>true</forceTags>
                     <!--藉助imageTags元素更為靈活地指定映象名稱和標籤-->
                    <imageTags>
                        <imageTag>0.0.1</imageTag>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!-- 與maven配置檔案settings.xml中配置的server.id一致,用於推送映象 -->
                    <serverId>docker-nexus</serverId>
                </configuration>
            </plugin>
        </plugins>
    </build>
複製程式碼
  • 使用如下命令進行構建(注意構建命令和沒有加入imageTags有所不同)
 mvn clean package docker:build  -DpushImageTag
複製程式碼

docker-maven-plugin 多專案共同構建

  • 以上配置我們只是針對單個專案配置,如果專案中包含多個子專案,我們希望一起打包成映象並上傳到私服,則需要用到phase 將映象構建繫結到maven 命令上
  • 平常打包構建命令為mvn clean package docker:build,而對應maven的命令格式為mvn phase:goal,所以打包構建命令中package docker對應為phase,build則對應goal,這樣根據官方檔案提示將POM改造得出
<build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.2.0</version>
                <!--將構建的觸發繫結到對應的命令上-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>tag-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>tag</goal>
                        </goals>
                        <configuration>
                            <image>test1/eureka1:${project.version}</image>
                            <newName>172.31.116.12:9290/test1/eureka1:${project.version}</newName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>push-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                            <imageName>172.31.116.12:9290/test1/eureka1:${project.version}</imageName>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <imageName>test1/eureka1:${project.version}</imageName> <!--映象名-->
                    <!--Dockerfile檔案路徑 此路徑代表本專案根目錄-->
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!--本地不安裝 docker 使用遠端docker 地址,該地址為docker 開啟遠端訪問地址 -->
                    <dockerHost>http://172.31.76.16:2375</dockerHost>
                    <!--<dockerCertPath></dockerCertPath>-->
                    <resources>
                        <resource> <!-- 指定資原始檔 -->
                            <targetPath>/</targetPath>  <!-- 指定要複製的目錄路徑,這裡是當前目錄 -->
                            <directory>${project.build.directory}</directory> <!-- 指定要複製的根目錄,這裡是target目錄 -->
                            <include>${project.build.finalName}.jar</include> <!-- 指定需要拷貝的檔案,這裡指最後生成的jar包 -->
                        </resource>
                    </resources>
                    <!--重複構建相同標籤名稱的映象,可將forceTags設為true,這樣就會覆蓋構建相同標籤的映象-->
                    <forceTags>true</forceTags>  <!--覆蓋相同標籤映象-->
                    <!-- 與maven配置檔案settings.xml中配置的server.id一致,用於推送映象 -->
                    <serverId>docker-nexus</serverId>
                </configuration>
            </plugin>
        </plugins>
    </build>
複製程式碼
  • 如此我們只要在專案根目錄使用命令,則專案就會自動打包構建映象並上傳到nexus私服
## -DskipTests 表示 跳過Test
mvn clean package -DskipTests
複製程式碼

整個專案構建成功並上傳私服

Docker 私有倉庫結合 nexus 搭建與使用

  • 每次構建好的映象我們存放在哪呢,這裡可以搭建自己的私服倉庫來存放docker映象。

安裝nexus

建立私有倉庫

nexus建立docker私服倉庫

docker連線私服倉庫

  • 上一小節我們建立好了私服倉庫,還需配置,在docker伺服器新建檔案
# 配置 vim /etc/docker/daemon.json 填寫我們倉庫地址和對應的倉庫埠號

{
    "insecure-registries": ["xxx.xxx.xxx.xxx:9290"] 
    }
# 重啟docker
systemctl daemon-reload
systemctl restart docker

複製程式碼
  • 登入倉庫,輸入nexus的使用者名稱和密碼,預設使用者名稱為admin,密碼為admin123
docker login xxx.xxx.xxx.xxx:9290
複製程式碼