docker: commit and dockerfile
1. Use commit to build an image:
commit is an image built on the basis of the original image. The purpose of using this method to build an image is to save some configuration information and modified information in the image. Equivalent to a mirrored snapshot.
2. Use dockerfile to build an image:
dockerfile is the (custom) image required for fast build.
Instructions for dockerfile:
FROM: Specifies the basic image (FROM is a required instruction and must be the first instruction).
RUN: used to execute command line commands. Its basic format:
shell format: RUN < command >, just enter the command in bash environment. A dockerfile can use RUN no more than 127 layers. Therefore, use RUN once, use '\' to wrap lines, and use '& &' to execute the next command. This format is generally used;
exec format: RUN < "executable", "parameter 1", "parameter 2" >, which is like the format in function call;
COPY: COPY files. Its basic format:
Format 1: copy < source path >< Target path >
Format 2: COPY ["< source path 1 >",... "< destination path >"]
ADD: a more advanced COPY file, which adds some functions on the basis of COPY. If you COPY a compressed package, it will be decompressed directly instead of using RUN to decompress it;
CMD: container start command. Its basic format:
shell format: CMD < command >
exec format: CMD ["executable", "parameter 1", "parameter 2"...]
Parameter list format: CMD ["parameter 1", "parameter 2"...], After specifying the ENTRYPOINT instruction, use CMD to specify specific parameters
ENTRYPOINT: entry point. Its basic format is divided into exec and shell,
The purpose of entrypoint, like CMD, is to start the program and parameters in the specified container. entrypoint can be replaced in operation, but it is more complicated than CMD. It needs to be specified through the parameter of docker run -- entrypoint. When entrypoint is specified, the meaning of CMD changes. Instead of directly running its command, the content of CMD is passed to the entrypoint instruction as a parameter. When it is executed, it becomes: < entry point > "< CMD >"
ENV: set the environment variable. (you can use the variables used here) its basic format:
Format 1: env < key > < value >
Format 2: env < key1 > = < value1 > < key2 > = < value >
ARG: build parameters. The effect of build parameters is the same as that of ENV, which is to set environment variables. The difference is that the environment variables built by ARG will not exist when the container runs in the future. Its basic format:
Format 1: Arg < parameter name > [= < default value >]
Format 2: the default value can be overwritten with -- build Arg < parameter name > = < value > in the build command docker build
VOLUME: defines an anonymous VOLUME. Its basic format:
Format 1: Volume ["< path 1 >", "< path 2 >"...]
Format 2: Volume < Path >
EXPOSE: EXPOSE port. The export instruction declares the port provided by the runtime container. When starting the container, the port will not be opened because of this declaration. Its basic format:
Format 1: export < port 1 > [< port 2 >...]
WORKDIR: Specifies the working directory. Its basic format:
Format 1: workdir < working directory path >
USER: Specifies the current USER. Switch to USER help. Its basic format:
Format 1: user < user name >
Health check: health check to judge whether the state of the container is normal. Its basic format:
Format 1: HEALTCHECK [options] CMD < command >: set the command to check the health of the container
Format 2: healthcheck none: if the underlying image has health check instructions, use this format to mask the health check instructions
To build an nginx image:
Create a directory and write dockerfile in it:
1 2 3 4 5 [root@docker ~]# mkdir mynginx [root@docker ~]# cd mynginx/ [root@docker mynginx]# pwd /root/mynginx [root@docker mynginx]#
Download the nginx source package to the created directory (mynginx directory):
1 [root@docker ~]# wget -P /root/mynginx/ http://nginx.org/download/nginx-1.15.2.tar.gz
Write Dockerfile:
[root@docker mynginx]# vi Dockerfile
The contents are as follows:
+ View Code
Run the docker command to build an image:
[root@docker mynginx]# docker build -t nginx:v3 . Sending build context to Docker daemon 1.029MB Step 1/7 : FROM centos ---> 5182e96772bf Step 2/7 : RUN ping -c 1 www.baidu.com ---> Using cache ---> 2f70f8abaf2a Step 3/7 : RUN yum -y install gcc make pcre-devel zlib-devel tar zlib ---> Using cache ---> dbdda4b7ae6f Step 4/7 : ADD nginx-1.15.2.tar.gz /usr/src/ ---> Using cache ---> 18ace6285668 Step 5/7 : RUN cd /usr/src/nginx-1.15.2 && mkdir /usr/local/nginx && ./configure --prefix=/usr/local/nginx && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ && nginx ---> Using cache ---> 99629488ede9 Step 6/7 : RUN rm -rf /usr/src/nginx-1.15.2 ---> Using cache ---> 869fbad71879 Step 7/7 : EXPOSE 80 ---> Using cache ---> 384bed72ea6f Successfully built 384bed72ea6f Successfully tagged nginx:v3
If you output two successes, the build is successful!
To start a custom image:
Use docker images to view the built image:
Start a custom image:
[root@docker ~]# docker run -d -p 80:80 --name nginx nginx:v3 4ac935e955b1c3ac49eed68f3372f3e96a8934fd8ccf4614afa3d7c29eb96c08 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ac935e955b1 nginx:v3 "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago nginx
Note: at this time, no matter how you start the container, it is still in the exited state.
After various solutions, we finally know what the problem is. Originally, when the container is started, it is started in the background corresponding to a thread. It has been started when it is started, but it exits after executing the command and is not running in the background, so use the - dit parameter to let it run in the background.
[root@docker ~]# docker run -dit -p 80:80 --name nginx nginx:v3 ecaafe1190447878b98dfb0198e92439db60ff7dab57a1674e0e9e7282a9c858 [root@docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ecaafe119044 nginx:v3 "/bin/bash" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp nginx
However
At this time, there is a problem again. Although it rises, the web page interface of nginx cannot be accessed, and the connection is refused!!!!
[root@docker ~]# curl 192.168.100.22 curl: (7) Failed connect to 192.168.100.22:80; connection denied [root@docker ~]# elinks --dump 192.168.100.22 ELinks: connection denied
Then, after asking Baidu, looking over the wall at Google, I finally found the problem. Originally, just use exec to enter the container and start nginx.
[root@docker ~]# docker exec -it nginx bash [root@ecaafe119044 /]# nginx [root@ecaafe119044 /]# exit exit
[root@docker ~]# curl 192.168.100.22 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
That's it! nginx image is built successfully!!!!