1. Dockerfile is introduced

Dockerfile is a text file used to build a Docker image, which can also be said to be a command parameter script. The docker build command is used to build an image from a Dockerfile. You can use the -f flag in the Docker build command to point to a Dockerfile anywhere in the file system.

Docker image release steps: 1, write a dockerfile file

2. Docker builds into an image

Docker Run image

4, Docker push image (release image to DockerHub, Ali Cloud image warehouse)

Example of the structure of a mirror:

Dockerfile instruction description

instruction instructions
FROM Specify base image
MAINTAINER Who wrote the image? Name + email
RUN Commands to be executed during image construction
ADD After local files are added to the container, tar files are automatically decompressed (network compressed resources are not decompressed) and network resources can be accessed, similar to wGET
WORKDIR The working directory of the mirror
VOLUME Mount directory
EXPOSE Reserved Port Configuration
EMTRYPOINT Specifies the commands to run when the container is started. You can append commands
ONBUILD When building an inherited DockerFile, the ONBUILD directive is run, triggering the directive
COPY Function is similar to ADD, but it does not automatically decompress files and cannot access network resources
ENV Set environment variables at build time

A diagram illustrating the role of each instruction:

Dockerfile is generally divided into four parts: basic image information, maintainer information, image operation instructions and container startup execution instructions, ‘#’ is a comment in Dockerfile.

DockerFile script:

1. Each reserved keyword (directive) must be in uppercase

2. The instructions in the file are executed FROM top to bottom, the first instruction must be FROM

# indicates a comment

4. Each instruction creates and commits a new mirror layer, and commits!

Dockerfile, official document instruction docs.docker.com/engine/refe…

3. Create a Centos image file

The following by writing Dockerfile file to make Centos image, and add VIm and net-tools on the basis of the official image. Create myDockerfile-centos in /home/dockfile. Then write the file using the above instructions.

[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER ethan<[email protected]>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
Copy the code

Instructions to explain the Dockerfile line by line:

  • FROM centos: This image file inherits the official centos and is followed by a colon (for example, centos:7) to specify the image version
  • ENV MYPATH /usr/local: Sets the environment variable MYPATH
  • WORKDIR $MYPATH: Specify /usr/local as the working directory directly using the environment variable set above
  • RUN yum -y install vim and RUN yum -y install net-tools: In /usr/local, run yum -y -install vim and yum -y install net-tools to install the tools. Note that all dependencies and tools are packaged into the image file after installation
  • EXPOSE 80: Exposes port 80 of the container, allowing external connections to this port
  • CMD: specifies when the container is started to run the command

Docker build -f dockerfile file path -t image name [: version number].

In the above command, the -t parameter is used to specify the name of the image file, followed by a colon to specify the label. If not specified, the default tag is Latest. The last dot represents the path where the Dockerfile file is located. The above example is the current path, so it is a dot.

Run the build command to generate the image file. If the command is executed successfully, you can view the newly generated image file through Docker images.

[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker build -f mydockerfile-centos -t mycentos: 1.0. Sending build context To Docker daemon 2.048kB Step 1/10: FROM centos ---> 300e315adb2f
Step 2/10 : MAINTAINER ethan<[email protected]>
 ---> Running in bbfd1a4949e1
Removing intermediate container bbfd1a4949e1
 ---> b2b6851e55fa
Step 3/10 : ENV MYPATH /usr/local
 ---> Running in 7cd8c84a5b70
Removing intermediate container 7cd8c84a5b70
 ---> e27a56b5247b
Step 4/10 : WORKDIR ${MYPATH}
 ---> Running in 770e175260c4
Removing intermediate container 770e175260c4
 ---> 7d46880ef0fd
Step 5/10 : RUN yum -y install vim
Step 7/10 : EXPOSE 80
 ---> Running in 24cfcfa56460
Removing intermediate container 24cfcfa56460
 ---> 2064c1540e8e
Step 8/10 : CMD echo ${MYPATH}
 ---> Running in 83dd9766da3c
Removing intermediate container 83dd9766da3c
 ---> 9b8661c812c4
Step 9/10 : CMD echo "---end---"
 ---> Running in 974afa805b27
Removing intermediate container 974afa805b27
 ---> 83459c404586
Step 10/10 : CMD /bin/bash
 ---> Running in 4fba3174f9d8
Removing intermediate container 4fba3174f9d8
 ---> 1185a46e3a12Successfully built 1185a46e3a12 Successfully tagged Mycentos :1.0 [root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker Images REPOSITORY TAG IMAGE ID CREATED SIZE Mycentos 1.0 1185a46e3a12 4 minutes ago 291MBCopy the code

Create a container and test related commands to check whether the default working directory is set successfully and whether vim and Net-tools are downloaded successfully.

[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker run -it mycentos:1.0
[root@e548efe82b30 local]# pwd
/usr/local
#The net-tools tool provides the ifconfig command[root@e548efe82b30 local]# ifconfig eth0: Flags = 4163 < UP, BROADCAST, RUNNING, MULTICAST > mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 BROADCAST 172.17.255.255 Mr 02:42: AC :11:00:03 TXQueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0b) RX errors 0 Dropped 0 Overruns 0 frame 0 TX Packets 0 bytes 0 (0.0b) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: Flags =73<UP,LOOPBACK,RUNNING> MTU 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local LOOPBACK) RX Packets 0 bytes 0 (0.0b) RX errors 0 dropped 0 Overruns 0 Frame 0 TX packets 0 bytes 0 (0.0b) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@e548efe82b30 local]# vi test.txt [root@e548efe82b30 local]# cat test.txt Hello world!Copy the code

In addition, we use the Docker history container ID command to see how the image is built

[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker history 1185a46e3a12 IMAGE CREATED CREATED BY SIZE COMMENT CMD ["/bin/sh" "-c" "/bin... 0B 83459c404586 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo... 0B 9b8661C812C4 13 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo...  0B 2064c1540e8e 13 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B 1b15d4a1fd5e 13 minutes ago /bin/sh -c yum -y install Nettools 23.3MB 9336c20f0b6d 13 minutes ago /bin/sh -c yum -y install vim 7d46880ef0fd 14 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B e27a56b5247b 14 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B b2b6851e55fa 14 Minutes ago /bin/sh -c #(nop) MAINTAINER Ethan <12583985...  0B 300e315adb2f 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 3 weeks ago /bin/sh -c #(nop) LABEL 0B <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7... 209MBCopy the code

4. Differences between RUN, CMD and ENTRYPOINT

What is the difference between RUN and CMD?

Simply put, the RUN command is executed during the construction phase of the image file, and the execution results are packaged into the image file. CMD is executed after the container is started. Also, a Dockerfile can contain multiple RUN commands, but only one CMD command.

Note that once CMD is specified, the docker container run command cannot attach commands (such as /bin/bash previously), otherwise it overrides CMD.

What is the difference between CMD and ENTRYPOINT?

  • CMD: specifies the command to run when the container is started. Only the last command takes effect
  • ENTRYPOINT: Specifies the command to run when the container is started. Commands can be appended

The first is to use the CMD directive

[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# cat dockerfile-cmd-test FROM centos CMD ["ls","-a"] [root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest: 1.0. Sending build context to Docker daemon Step 1/2: FROM centos ---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 6d4d0112322f
Removing intermediate container 6d4d0112322f
 ---> b6ec5224d2acSuccessfully built b6ec5224d2ac Successfully tagged cmdtest:1.0 [root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker run Cmdtest: 1.0... .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var#-l replaces ls -a, and the -l command does not exist.
[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker run cmdtest:1.0 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
Copy the code

You can see that the append command -l has an error.

Let’s use ENTRYPOINT to build an image

#1. Modify dockerfile
[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# cat dockerfile-cmd-test
FROM centos
ENTRYPOINT ["ls","-a"]
#2. Build an image[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest: 2.0. Sending build context to Docker daemon Step 1/2: FROM centos ---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 61389c0c1967
Removing intermediate container 61389c0c1967
 ---> ac7b7e83ff88
Successfully built ac7b7e83ff88
Successfully tagged cmdtest:2.0
#3. Run the image[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker run cmdtest:2.0.. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var#4. Run the append mirror again[root@iZwz99sm8v95sckz8bd2c4Z dockerfile]# docker run cmdtest:2.0 -l total 56 drwxr-xr-x 1 root root 4096 Jan 1 03:55. drwxr-xr-x 1 root root 4096 Jan 1 03:55 .. -rwxr-xr-x 1 root root 0 Jan 1 03:55 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x 5 root root 340 Jan 1 03:55 dev drwxr-xr-x 1 root root 4096 Jan 1 03:55 etc drwxr-xr-x 2 root root 4096 Nov 3 15:22 home lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64 drwx------ 2  root root 4096 Dec 4 17:37 lost+found drwxr-xr-x 2 root root 4096 Nov 3 15:22 media drwxr-xr-x 2 root root 4096 Nov 3 15:22 mnt drwxr-xr-x 2 root root 4096 Nov 3 15:22 opt dr-xr-xr-x 106 root root 0 Jan 1 03:55 proc dr-xr-x--- 2 root root  4096 Dec 4 17:37 root drwxr-xr-x 11 root root 4096 Dec 4 17:37 run lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 15:22 srv dr-xr-xr-x 13 root root 0 Dec 29 15:41 sys drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr drwxr-xr-x 20 root root 4096 Dec 4 17:37 varCopy the code

5. Create a Tomcat image and publish the image

5.1 Creating a Tomcat Image

1. Prepare the image file Tomcat and JDK package

[root@iZwz99sm8v95sckz8bd2c4Z tomcat]# vi readme.txt
[root@iZwz99sm8v95sckz8bd2c4Z tomcat]# ll
total 200700
-rw-r--r-- 1 root root  10371538 Jan  1 16:11 apache-tomcat-8.5.55.tar.gz
-rw-r--r-- 1 root root 195132576 Jan  1 16:13 jdk-8u251-linux-x64.tar.gz
-rw-r--r-- 1 root root        20 Jan  1 16:14 readme.txt
Copy the code

2. Create a dockerfile file with the official name of the file: dockerfile. When building, the file will be found in the current directory by default

[root@iZwz99sm8v95sckz8bd2c4Z tomcat]# vim Dockerfile [root@iZwz99sm8v95sckz8bd2c4Z tomcat]# cat Dockerfile FROM centos MAINTAINER ethan<[email protected]> COPY readme.txt /usr/local/readme.txt ADD jdk-8u251-linux-x64.tar.gz /usr/local/ ADD Apache-tomcat-8.5.55.tar. gz /usr/local/run yum -y install vim ENV MYPATH /usr/localworkdir $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_251 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.55 ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.55 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD / usr/local/apache tomcat - 8.5.55 / bin/startup. Sh && tail -f/usr/local/apache tomcat - 8.5.55 / bin/logs/catalina. OutCopy the code

3. Use the Dockerfile to build an image

[root@iZwz99sm8v95sckz8bd2c4Z tomcat]# docker build-t diytomcat:1.0Copy the code

4. Start the image and build the Tomcat container. Here set the data volume, the host machine/home/dockerfile/tomcat/test of containers should be/usr/local/apache tomcat – 8.5.55 / webapps/test. In this way, fixes for the Test project only need to be made on the host rather than in the container.

[root@iZwz99sm8v95sckz8bd2c4Z tomcat]# docker run -d -p 8088:8080 --name diytomcat -v / home/dockerfile/tomcat/test: / usr/local/apache tomcat - 8.5.55 / webapps/test diytomcat: 1.0Copy the code

5. In the/home/dockerfile/tomcat/test directory, a new index. The HTML test whether tomcat can normal use.

The operation can be performed directly on the host machine because volume mount is configured.

<! DOCTYPE HTML > < HTML > <head> <meta charset="UTF-8"/> <title> This is a title </title> </head> <body> <h1> This is a simple HTML</h1> <p>Hello World! </p> </body> </html>Copy the code

6. Perform an access test. Check whether the browser can access the test

If the page displays garbled characters, you need to modify the Tomcat server.xml file

  <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding="UTF-8" />
Copy the code

Here we add a property: URIEncoding, which is set to UTF-8 to have Tomcat (default ISO-8859-1 encoding) process GET requests in UTF-8 encoding.

5.2 Publishing an Image to DockerHub

1. Log on to hub.docker.com/ DockerHub website to register

2. Login using the docker login -u user name

The login command

[root@iZwz99sm8v95sckz8bd2c4Z test]# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Usernames
Copy the code

3. Use the Docker push command to push the image to the repository on DockerHub

[root @ iZwz99sm8v95sckz8bd2c4Z test] # docker tag 0975 df661526 ethanhuang824 / diytomcat: 1.0 [root @ iZwz99sm8v95sckz8bd2c4Z Test]# Docker images REPOSITORY TAG ID CREATED SIZE Diytomcat 1.0 0975df661526 About an hour ago 688MB Ethanhuang824 / Diytomcat 1.0 0975DF661526 About an hour ago 688MB CMdTest 2.0 AC7b7e83ff88 6 hours ago 209MB CMdtest 1.0 B6ec5224d2ac 6 hours ago 209MB Ethan /centos 1.01DF90e6FD790 2 days ago 209MB Mytomcat 1.0f189aAC861de 3 days ago 653MB Mysql 5.7f07dfa83b528 10 days ago 448MB Tomcat latest feba8d001e3f 2 weeks ago 649MB nginx latest AE2feFF98A0C 2 weeks Ago 133MB centos Latest 300E315ADB2f 3 weeks ago 209MB ElasticSearch 7.6.2f29a1EE41030 9 months ago 791MB [root @ iZwz99sm8v95sckz8bd2c4Z test] # docker push ethanhuang824 / diytomcat: 1.0Copy the code

For push, the image name needs to be preceded by a user name (ethanhuang824 is my user name). If the user name is not the current login user, the push request will be rejected. Therefore, you need to use the docker tag image name command to copy the image name and tag the image again.

5.3 Publishing an image to Ali Cloud Container Service

1. Log in to Aliyun and find the container image service

2. Create a namespace

3. Similar to the operation of DockerHub above, just follow the operation guide provided by the official.