Make your own Docker image

There are two ways to create an image, one is to convert the container into an image, and the other is to use dockerfile to create the image, which is more commonly used.

Container to mirror

  • Use docker commitcommands to convert containers to images
docker commit 容器id 镜像名称:版本号
  • When you need to transfer the image, pack the image into a package
docker save -o 压缩文件名称 镜像名称:版本号
  • When loading this image on another computer, load this package
docker load –i 压缩文件名称

Create image using dockerfile (recommended)

dockerfile is a text file that contains a command, each command builds a layer, based on the basic image, and finally build a new image.

Keywords used in dockerfile

Keyword effect Remarks
FROM Specify parent mirror Specify the dockerfile to build based on that image
MAINTAINER author information Used to indicate who wrote this dockerfile
LABEL label The label used to indicate dockerfile can use Label instead of Maintainer. In the end, it can be viewed in the basic information of docker image
RUN Excuting an order The default for executing a command is / bin / sh format: RUN command or RUN ["command", "param1", "param2"]
CMD Container start command Provide the default command when starting the container to use with ENTRYPOINT. Format CMD command param1 param2 or CMD ["command", "param1", "param2"]
ENTRYPOINT Entrance Generally used in making some containers that are closed after execution
COPY Copy file Copy files to image during build
ADD add files Adding files to the image during the build is not limited to the current build context can come from the remote service
ENV Environment variables Specify environment variables at build time to override the format ENV name = value with -e when starting the container
ARG Build parameters The build parameters are only used when building. If there is ENV, the value of ENV with the same name always overrides the parameter of arg
VOLUME Define the external data volume that can be mounted Specify which directories of the build image can be mounted to the file system when they are started, and use the -v binding format VOLUME ["directory"]
EXPOSE Exposed port Define the port to monitor when the container is running. Use the -p to bind the exposed port format: EXPOSE 8080 or EXPOSE 8080 / udp
WORKDIR Work list The working directory inside the specified container is automatically created if not created
USER Designated 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 many times the application itself has a health monitoring mechanism
ONBUILD trigger When the image with the ONBUILD keyword is used as the base image, the ONBUILD command will be executed after the FROM is completed, but it does not affect the current image. It is not very useful
STOPSIGNAL Send semaphore to host The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit.
SHELL Specify a shell to execute the script Specifies the shell used when RUN CMD ENTRYPOINT executes a command

dockerfile case

Custom centos image

Claim:

  • The default login path is / usr
  • Can use vim

Implementation steps:

  1. vi centos_dockerfile Enter the following in the file to save and exit:
FROM centos:7	# 定义父镜像

MAINTAINER itheima<[email protected]>	# 定义作者信息

RUN yum install -y vim	# 执行安装vim命令

WORKDIR /usr	# 定义默认的工作目录

CMD /bin/bash	# 定义容器启动执行的命令
  1. By centos_dockerfilebuilding an image:
docker bulid –f ./centos_dockerfile –t 镜像名称:版本 .

(Note that there is a point at the end, indicating the directory of the context environment during the image building process). The installation of vim may fail due to network reasons. Just execute this command a few times.

Deploy Spring boot project

demand:

  • Define dockerfile to publish Spring boot project

achieve:

  1. New springboot_dockerfilefile, jar package and dockerfilefile need to be in the same directory
FROM java:8

MAINTAINER itheima<[email protected]>

ADD springboot-hello-0.0.1-SNAPSHOT.jar app.jar # 把springboot项目的jar包添加到镜像中并换个简短的名字app.jar

CMD java -jar app.jar # 运行jar包
  1. By springboot_dockerfilebuilding an image
docker build -f ./springboot_dockerfile -t app . # 新的镜像名称为app

Guess you like

Origin www.cnblogs.com/lxy0/p/12713382.html