1. Dockerfile concept
Dockerfile is a set of rules for customizing images.
Dockerfie is composed of multiple instructions. Each instruction in the Dockerfile corresponds to each layer in the Docker image.
Dockerfile command
Keywords | effect | Remark |
---|---|---|
FROM | Specify parent image | Specify which image the dockerfile is built on |
MAINTAINER | author information | Used to indicate who wrote this dockerfile |
LABEL | Label | Label used to mark dockerfile can be used instead of Maintainer. Finally, it can be viewed in the basic information of docker image. |
RUN | Excuting an order | The default format for executing a command is /bin/sh: RUN command or RUN ["command", "param1", "param2"] |
CMD | Container startup command | Provides the default command when starting the container for use with ENTRYPOINT. The format is CMD command param1 param2 or CMD ["command", "param1", "param2"] |
ENTRYPOINT | Entrance | It is generally used when making some containers that will be closed after execution. |
COPY | Copy files | Copy files to image when building |
ADD | add files | Adding files to the image when building is not limited to the current build context and can come from remote services. |
ENV | environment variables | The environment variables specified during build can be overridden in the format ENV name=value by -e when starting the container. |
ARG | Build parameters | Build parameters are only used when building. If there is ENV, the value of the same name of ENV will always overwrite the arg parameter. |
VOLUME | Define externally mountable data volumes | Specify which directories of the build image can be mounted to the file system when starting. When starting the container, use the -v binding format VOLUME ["directory"] |
EXPOSE | exposed port | Define the listening port when the container is running. Use -p to start the container to bind the exposed port format: EXPOSE 8080 or EXPOSE 8080/udp. |
WORKDIR | Work list | Specify the working directory inside the container. If it has not been created, it will be created automatically. If / is specified, an absolute address is used. If it does not start with /, then it is a relative path to the path of the previous workdir. |
USER | Specify execution user | Specify the user when RUN CMD ENTRYPONT is executed during build or startup. |
HEALTHCHECK | health examination | The command to specify the health monitoring of the current container is basically useless because in many cases the application itself has a health monitoring mechanism. |
ONBUILD | trigger | When there is an image with the ONBUILD keyword as the base image, the ONBUILD command will be executed after the FROM execution is completed, but it does not affect the current image and is not very useful. |
STOP SIGNAL | Send semaphore to host | The STOPSIGNAL directive sets the system call signal that will be sent to the container to exit. |
SHELL | Specify the shell to execute the script | Specify the shell used when RUN CMD ENTRYPOINT executes the command |
Dockerfile packaging project example:
FROM java:8
MAINTAINER 作者 <邮箱>
# 指定可以被宿主机文件挂载目录
VOLUME /opt/demo/
# 指定路径
WORKDIR /opt/demo/
#添加demo-0.0.1-SNAPSHOT.jar包到WORKDIR 以 demo.jar保存
ADD demo-0.0.1-SNAPSHOT.jar demo.jar
EXPOSE 8083
ENTRYPOINT ["java","-jar","demo.jar"]
docker build -t 镜像名 .
# -t:指定dockerfile编译后所生成的镜像名称
# . :表示Dockfile在当前路径
#启动项目shell命令换行操作为空格\
docker run -d -p 8083:8083 \
# 宿主机/opt/demo指向容器/opt/demo/进行绑定
-v /opt/demo:/opt/demo/ \
--name demo demo:latest
If the jdk version you are using is not 1.8 and you need to customize the jdk image, please see Customizing the JDK Image