Docker note (e): a whole own image

Original Address: http://blog.jboost.cn/2019/07/17/docerk-5.html

 

There are two ways to get a mirror, one is mirrored obtain from the warehouse, as the official Docker Hub, the second is the custom. As already describes how to obtain mirror from a mirrored storage, Springboot Based on a project to introduce a custom mirror of the basic flow.

1. custom mirrored nature

We know that the mirror is tiered storage, building mirroring is carried out layer by layer, a layer of building finished, it becomes read-only, and then build the next layer thereon. Therefore customized image, in fact, is the definition of each layer to dry things, such as execute a command to set an environment variable, declare an exposed port and so on. Then when building, by definition layers, layer by layer build, eventually forming a mirror comprising these layers.

2. Dockerfile file

Docker defined layers wanted to do a file called Dockerfile, which is a text file that contains a section of instructions, each instruction corresponding to one image, the contents of the instruction on this layer describes how the construct. An example in a very simple Dockerfile,

FROM nginx
RUN echo '<h1>Hello jboost!</h1>' > /usr/share/nginx/html/index.html

 

We customized image, must be based on a certain image constructed based on the layer on which they need, as an example, we are nginx-based mirroring, and then in the second layer to customize our own content - Modify the index. html content is <h1>Hello jboost!</h1>, so running the default container will not show up when you open the page content nginx home. 

Contacting the above example of two instructions Dockerfile

  • FROM: FROM instruction specifies the base image, each mirror must have a custom base image, it must have a FROM instruction, and the first instruction is Dockerfile
  • RUN: RUN command specifies the command to be executed, followed by the command is like the same executable shell script

Dockerfile also provides a number of other instructions, we'll focus on the follow-up reports, we only do a simple explanation of the instructions come into contact with.

3. Customize a mirror

This part of the project-based to a Springboot introduces basic aspects of a custom image involved. Project address: https://github.com/ronwxy/swagger-register  , the project is a Swagger API document registration services, other items can be registered Swagger API information to the service, unified view and management.

3.1 Definitions Dockerfile file

First, we create a Dockerfile file (the file name is called Dockerfile) in the root directory of the project, which reads:

FROM openjdk:8-jdk-alpine
ENV PROFILE=dev
RUN mkdir /app /logs
COPY ./target/swagger-register-1.0.0-SNAPSHOT.jar /app/app.jar
WORKDIR /app
VOLUME /register-data
EXPOSE 11090
CMD ["java", "-Dspring.profiles.active=${PROFILE}", "-jar", "app.jar"]

 

From top to bottom are described below 

  • The first line: FORM openjdk: 8-jdk- alpine, represented in openjdk:8-jdk-alpinethis image as the base image, because this is a Springboot project must have support jdk, when we customized image, you can find the most suitable image as a base image .
  • Second line: ENV PROFILE = dev, defines a variable environment, the environment variables can be referenced later
  • Third row: RUN mkdir / app / logs, create two directories using the mkdir command to save the file and execute jar log
  • Fourth row: COPY ./target/swagger-register-1.0.0-SNAPSHOT.jar /app/app.jar copy target jar under the package directory to / app directory, rename and
  • Fifth row: WORKDIR / app, specify the working directory is / app, the layers behind the current directory is the working directory specified
  • Sixth line: VOLUME / register-data, define an anonymous data volume, as I said before do not write directly in the container, but to instead write mounted volume catalog data, this can be defined by the container when you run -v to cover.
  • Seventh row: EXPOSE 11090, declare the service provided by the port while running the container, it is only a statement only, just tell people used to map the port, the port can be mapped by -p.
  • Eighth line: CMD [ "java", "-Dspring.profiles.active = $ {PROFILE}", "-jar", "app.jar"], specifies the command to start the container, because it is a Springboot project, so that java -jar execution of a command, the vessel will start when the execution of the command to run Springboot service here quoted the second line defines an environment variable PROFILE

3.2 Configuring maven plugin

After the definition of a good Dockerfile, for convenience constructed mirror, we can use the maven dockerfile plug dockerfile-maven-plugin, was added portion disposed below the build of pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- Docker maven plugin -->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.4.10</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>
        <!-- Docker maven plugin -->
    </plugins>
</build>

 

repository specifies the name of the mirror, docker.image.prefixneed to be defined properties section, I am here springboot.

3.3 Construction of the mirror

Download Source: https://github.com/ronwxy/swagger-register.git  , then execute the following command (provided that the local has been installed docker with maven and jdk) in the root directory of the project

mvn clean package -Dmaven.test.skip=true dockerfile:build


This command is first performed mvn clean package -Dmaven.test.skip=trueon the packaged item, generating ./target/swagger-register-1.0.0-SNAPSHOT.jar file, and then built on Dockerfile file in the current directory, as shown in FIG. 

docker-build

As can be seen from the figure, the mirror constructed in eight steps (corresponding to the eight rows Dockerfile instruction), a step of generating each image layer, each layer has a unique ID. Can also be seen from the figure, in addition to commands such as COPY, each layer is actually constructed to start a container-based layer, and then performs the operation defined by the layer, and then remove the container to be achieved, as the eighth step

Step 8/8 : CMD ["java", "-Dspring.profiles.active=${PROFILE}", "-jar", "app.jar"]
[INFO] 
[INFO]  ---> Running in f4acd0b53bca
[INFO] Removing intermediate container f4acd0b53bca
[INFO]  ---> a9ee579f2d62

先启动一个ID为f4acd0b53bca的容器,在其中执行CMD所定义的命令,然后再移除容器f4acd0b53bca,最后生成ID为a9ee579f2d62的镜像。 

构建完后,我们就可以在本地镜像中通过docker iamges看到我们定制的镜像了,如图
docker-image

图中springboot/swagger-register镜像即为我们刚刚构建好的定制镜像。

3.4 启动容器

我们可以通过以下命令来启动一个刚才定制镜像的容器

docker run -d --name swagger-register -p 11090:11090 -v /home/jenkins/swagger-register/register-data:/register-data -v /home/jenkins/swagger-register/logs:/logs --restart=always springboot/swagger-register:latest

其中: 

  • -d 表示以后台进程方式运行
  • –name 指定容器名称
  • -p 指定端口映射,左边为宿主机端口,右边为容器服务端口
  • -v 指定数据卷挂载,左边为宿主机目录,右边为容器目录
  • –restart=always 表示在docker启动时自动启动该容器

关于容器相关的内容后面详细介绍,这里不展开说明了。启动容器后, 我们就可以浏览器打开地址 http://宿主机ip:11090/doc.html 来访问服务了(打开页面后内容是空的,因为没有任何服务注册Swagger API, 相关内容可参考 swagger api文档集中化注册管理

4. 总结

This article describes the Docker image customization and use a project-based Springboot, right, and run Dockerfile basic instructions and container construction process mirrored made a basic introduction. Other instructions will follow Dockerfile and best practices Dockerfile be more detail, welcome attention.


My personal blog address: http://blog.jboost.cn
my micro-channel public number: jboost-ksxy (a technique not only dry numbers of the public are welcome attention, timely access to updates)
-------- ---------------------------------------------
Micro-channel public number

Guess you like

Origin www.cnblogs.com/spec-dog/p/11204914.html