Detailed explanation of Dockerfile usage

1, Dockerfile introduction

1.Dockerfile

Dockerfile is a text file used to build an image, which can deploy and run a container environment you need.
It can be understood as a script to build software dependency, file dependency, network, storage and other environments through dockerfile's own instructions.

2. Create image method

① Manually modify the contents of the container, and then docker commit submits the container as a new image
② A script is formed by defining a series of commands and parameters in the dockerfile, and then these commands are applied to the basic image, adding layers in turn, and finally generating a new image.

3. Components of dockerfile

① Basic image information FROM centos:7.8.2003
② Yum install openrun mirror operation
③ Execute the command CMD ["/ bin/bash"] when the container starts

2, Dockerfile instruction

1. Common instructions

FROM sets the basic image
MAINTAINER formulates MAINTAINER information, which can not be written
RUN adds RUN before the command to operate in the container
ADD adds the files of the host to the container and has the function of automatic decompression
The function of COPY is the same as that of ADD. It copies the files of the host to the container. COPY is just a COPY
WORKDIR sets the current working directory
VOLUME set VOLUME, Mount Host Directory
Export specifies the external port
CMD specifies what to do after the container starts

2. Other directives

COPY copy file
ENV environment variable
The command executed after the ENTRYPOINT container is started

3, Write Dockerfile

1. Create Dockerfile file

[root@node1 ~]# mkdir -p /docker
[root@node1 ~]# cd /docker/
[root@node1 docker]# vim Dockerfile

2. Prepare Dockerfile file

[root@node1 docker]# cat Dockerfile 
FROM nginx
RUN echo '<meta charset=utf8>I want to study docker!' > /usr/share/nginx/html/index.html

3. Build Dockerfile

[root@node1 docker]# docker build .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM nginx
 ---> 4cdc5dd7eaad
Step 2/2 : RUN echo '<meta charset=utf8>I want to study docker!' > /usr/share/nginx/html/index.html
 ---> Running in 670e11acd981
Removing intermediate container 670e11acd981
 ---> ecda82f680dc
Successfully built ecda82f680dc
[root@node1 docker]# docker images 
REPOSITORY                   TAG        IMAGE ID       CREATED          SIZE
<none>                       <none>     ecda82f680dc   23 seconds ago   133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   11 hours ago     386MB
ubuntu                       latest     c29284518f49   9 days ago       72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago      133MB
redis                        latest     08502081bff6   4 weeks ago      105MB
fedora                       latest     055b2e5ebc94   2 months ago     178MB
centos                       latest     300e315adb2f   7 months ago     209MB
centos                       7.8.2003   afb6fca791e0   14 months ago    203MB
opensuse                     latest     d9e50bf28896   2 years ago      111MB


4. Modify the image name

[root@node1 docker]# docker tag ecda82f680dc my_nginx
[root@node1 docker]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED              SIZE
my_nginx                     latest     ecda82f680dc   About a minute ago   133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   11 hours ago         386MB
ubuntu                       latest     c29284518f49   9 days ago           72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago          133MB
redis                        latest     08502081bff6   4 weeks ago          105MB
fedora                       latest     055b2e5ebc94   2 months ago         178MB
centos                       latest     300e315adb2f   7 months ago         209MB
centos                       7.8.2003   afb6fca791e0   14 months ago        203MB
opensuse                     latest     d9e50bf28896   2 years ago          111MB

5. Run image

[root@node1 docker]# docker run -d -p 80:80 my_nginx
5fbf299d43ea252596fba7afbdfb9ba388cc586793016acb2adc9dad92ef7bdb
[root@node1 docker]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                               NAMES
5fbf299d43ea   my_nginx   "/docker-entrypoint...."   5 seconds ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   eager_pike

6. Test port 80 of the host

4, Dickerfile Instruction Learning

1.COPY

The COPY instruction copies files / directories from the host to a new layer of image

copy file*.txt /home

# Multiple files and wildcards are supported to copy. The syntax needs to meet the filepath.Match of Golang
copy file* /data/playbook-?.yml /data/
# The copy instruction can retain the metadata information of the file, such as permission, timestamp, etc.

2.ADD

The features are basically the same as COPY, but with some more functions

1. The source file is a URL. At this time, the docker engine will download the link and put it into the target path, and the permission is automatically set to 600. If this is not the expected result, a RUN instruction must be added to modify the permission of the file.
2. The source file is a URL and a compressed package. It will not be decompressed automatically. It must also be decompressed separately using the RUN instruction.
3. When the source file is a compressed file and is gzip, bzip2,xz,tar, etc., the ADD instruction will automatically decompress the file to the target path.

ADD file.tgz /home
RUN chmod a+x xxxx #You can modify the permissions of remote files through the RUN command

3.CMD

① CMD command introduction

CMD runs a command in the container to start the program

CMD ["Parameter 1" , "Parameter 2"]

The docker is not a virtual machine, and the container is a process. When the program starts, it needs to specify some running parameters, which is the function of the CMD instruction.

② Example

ps:

docker run -it centos cat /etc/os-release
# CMD runs shell instructions and will also be converted to shell form
CMD echo $PATH
 Will translate into CMD ["sh","-c","echo $PATH"]

Host command line docker run -it centos cat /etc/os-release
 Equivalent to dockerfile Medium CMD ["cat","/etc/os-release"]

4. In vessel operation procedure

Incorrect writing: CMD ["sh"."-c","systemctl start nginx" ]
The main process of such a command is the interpreter, which ends immediately after execution, so the container exits
 Correct writing: CMD ["nginx","-g","daemon off;"]

5.ENV and ARG

① Setting environment variables

ENV NAME="ZHANGSAN"
ENV AGE="18"
ENV MYSQL_VERSION=5.6
# For all subsequent operations, you can directly obtain the variable value through $NAME

② Difference between ENV and ARG

ARG and ENV are environment variables
The difference is ENV. This variable can be used either when the image is built or when the container is running
ARG is only a variable that needs to be set when building an image, and the container disappears when it runs.

6.VOLUME

When the container is running, it should be ensured that no data is written in the storage layer. The data generated in the container is recommended to be mounted and written to the host computer. Perform maintenance.

① volume introduction

VOLUME /data
 # The / data folder in the container is automatically mounted as an anonymous volume when the container is running,
 Any data written to the directory will not be recorded by the container to ensure that the container storage layer is stateless.

② Using volume in Dockerfile

# Dockerfile
FROM centos
MAINTAINER ZHANGSAN
VOLUME ["/data1","/data2"]

③ Generate image

[root@node1 docker]# docker build .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos:7.8.2003
 ---> afb6fca791e0
Step 2/3 : MAINTAINER ZHANGSAN
 ---> Running in 5023bed7a914
Removing intermediate container 5023bed7a914
 ---> b2242678f857
Step 3/3 : VOLUME ["/data1","/data2"]
 ---> Running in 8a181e92a341
Removing intermediate container 8a181e92a341
 ---> 6cc85e0cee71
Successfully built 6cc85e0cee71
[root@node1 docker]# docker tag  6cc85e0cee71 centos_volume
[root@node1 docker]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED          SIZE
centos_volume                latest     6cc85e0cee71   26 seconds ago   203MB
centos_curl_new              latest     d61875c20211   34 minutes ago   471MB
centos_curl                  latest     ded6737bf511   53 minutes ago   471MB
my_nginx                     latest     ecda82f680dc   10 hours ago     133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   21 hours ago     386MB
ubuntu                       latest     c29284518f49   9 days ago       72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago      133MB
redis                        latest     08502081bff6   4 weeks ago      105MB
fedora                       latest     055b2e5ebc94   2 months ago     178MB
centos                       latest     300e315adb2f   7 months ago     209MB
centos                       7.8.2003   afb6fca791e0   14 months ago    203MB
opensuse                     latest     d9e50bf28896   2 years ago      111MB

④ Run the mirror

[root@node1 docker]# docker run centos_volume
[root@node1 docker]# 

⑤ View container records and related information

[root@node1 docker]# docker inspect 447041a55a0d 
[
    {
        "Id": "447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa",
        "Created": "2021-07-23T13:25:43.107269297Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-23T13:25:43.373146025Z",
            "FinishedAt": "2021-07-23T13:25:43.372437154Z"
        },
        "Image": "sha256:6cc85e0cee71b1927a79d9ca3a26b11323f5e88ee6bb9a2b878354f46f357e04",
        "ResolvConfPath": "/var/lib/docker/containers/447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa/hostname",
        "HostsPath": "/var/lib/docker/containers/447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa/hosts",
        "LogPath": "/var/lib/docker/containers/447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa/447041a55a0d681c123fbd6286a05fb7ab9d499949bf80d3e2aac96091596afa-json.log",
        "Name": "/exciting_burnell",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/78b450fd961317740de1225ce03c760b514623c7a089bfecb591a861ecfd3b15-init/diff:/var/lib/docker/overlay2/c4fc8645dba560891da2786efe974432eb221f63cc8986ecccb4a28a77176ce9/diff",
                "MergedDir": "/var/lib/docker/overlay2/78b450fd961317740de1225ce03c760b514623c7a089bfecb591a861ecfd3b15/merged",
                "UpperDir": "/var/lib/docker/overlay2/78b450fd961317740de1225ce03c760b514623c7a089bfecb591a861ecfd3b15/diff",
                "WorkDir": "/var/lib/docker/overlay2/78b450fd961317740de1225ce03c760b514623c7a089bfecb591a861ecfd3b15/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "05ba887cb41240b61b9ae7ba36aeb5509535fb5c370abccad8a0059c200d0927",
                "Source": "/var/lib/docker/volumes/05ba887cb41240b61b9ae7ba36aeb5509535fb5c370abccad8a0059c200d0927/_data",
                "Destination": "/data1",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "6d8df1897042a46ce8c339fe462405771b43e2107034ddf9805f2ef79d9ebb9d",
                "Source": "/var/lib/docker/volumes/6d8df1897042a46ce8c339fe462405771b43e2107034ddf9805f2ef79d9ebb9d/_data",
                "Destination": "/data2",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "447041a55a0d",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos_volume",
            "Volumes": {
                "/data1": {},
                "/data2": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200504",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS",
                "org.opencontainers.image.created": "2020-05-04 00:00:00+01:00",
                "org.opencontainers.image.licenses": "GPL-2.0-only",
                "org.opencontainers.image.title": "CentOS Base Image",
                "org.opencontainers.image.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "97f42a5aae093e45b987aeac823fe9fcebba9500c67dd4f41215d820fb0fe49f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/97f42a5aae09",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d69c7098d163d0e7e02b1e6da0feed684de33bd6b5a6f16d75900d74fa3f91c",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]


⑥ . container record information filtering

⑦ . container data mounting method

① The container data is mounted by specifying the volume directory through dockerfile.
② Directly set the directory to be mapped and mounted through the docker run -v parameter.

7.EXPOSE

① Introduction to export

Specifies the external port services provided by the container runtime
Help users of the image to quickly understand one port service of the container

② Export related commands

docker port container
docker run -p Host port:Container port
docker run -P Random host port: container port

8.WORKDIR

Used to switch directories and change working directories in dockerfile

WORKDIR /opt

9.USER

It is used to change the environment and switch users

USER ROOT
USER ZHANGSAN

5, ENTRTPOINT learning

1. Introduction to entrtpoint

The function is the same as CMD, which starts the program and parameters in the specified container.
However, when ENTRYPOINT is specified, the semantics of the CMD instruction changes. At this time, the CMD content is passed to the ENTRYPOINT instruction as a parameter.

The function is the same as CMD, which starts the program and parameters in the specified container.
However, when ENTRYPOINT is specified, the semantics of the CMD instruction changes. At this time, the CMD content is passed to the ENTRYPOINT instruction as a parameter.

2. Quickly edit dockerfile

① Prepare a dockerfile

FROM centos:7.8.2003
RUN rpm --rebuilddb && yum install epel-release -y
RUN rpm --rebuilddb && yum install curl -y
CMD ["curl","-s","cip.cc"]

② Build mirror

[root@node1 docker]# echo > Dockerfile 
[root@node1 docker]# vim Dockerfile 
[root@node1 docker]# cat Dockerfile 
FROM centos:7.8.2003
RUN rpm --rebuilddb && yum install epel-release -y
RUN rpm --rebuilddb && yum install curl -y
CMD ["curl","-s","cip.cc"]
[root@node1 docker]# 
[root@node1 docker]# docker build .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos:7.8.2003
 ---> afb6fca791e0
Step 2/4 : RUN rpm --rebuilddb && yum install epel-release -y
 ---> Running in 158ebc6f1701
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-11            extras            15 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-8.2003.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                     1/1 
  Verifying  : epel-release-7-11.noarch                                     1/1 

Installed:
  epel-release.noarch 0:7-11                                                    

Complete!
Removing intermediate container 158ebc6f1701
 ---> 96ed23874ab7
Step 3/4 : RUN rpm --rebuilddb && yum install curl -y
 ---> Running in a7c0bfbbcd58
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror.lzu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package curl.x86_64 0:7.29.0-57.el7 will be updated
---> Package curl.x86_64 0:7.29.0-59.el7_9.1 will be an update
--> Processing Dependency: libcurl = 7.29.0-59.el7_9.1 for package: curl-7.29.0-59.el7_9.1.x86_64
--> Running transaction check
---> Package libcurl.x86_64 0:7.29.0-57.el7 will be updated
---> Package libcurl.x86_64 0:7.29.0-59.el7_9.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch           Version                   Repository       Size
================================================================================
Updating:
 curl            x86_64         7.29.0-59.el7_9.1         updates         271 k
Updating for dependencies:
 libcurl         x86_64         7.29.0-59.el7_9.1         updates         223 k

Transaction Summary
================================================================================
Upgrade  1 Package (+1 Dependent package)

Total download size: 494 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                              1.7 MB/s | 494 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libcurl-7.29.0-59.el7_9.1.x86_64                             1/4 
  Updating   : curl-7.29.0-59.el7_9.1.x86_64                                2/4 
  Cleanup    : curl-7.29.0-57.el7.x86_64                                    3/4 
  Cleanup    : libcurl-7.29.0-57.el7.x86_64                                 4/4 
  Verifying  : curl-7.29.0-59.el7_9.1.x86_64                                1/4 
  Verifying  : libcurl-7.29.0-59.el7_9.1.x86_64                             2/4 
  Verifying  : curl-7.29.0-57.el7.x86_64                                    3/4 
  Verifying  : libcurl-7.29.0-57.el7.x86_64                                 4/4 

Updated:
  curl.x86_64 0:7.29.0-59.el7_9.1                                               

Dependency Updated:
  libcurl.x86_64 0:7.29.0-59.el7_9.1                                            

Complete!
Removing intermediate container a7c0bfbbcd58
 ---> 9a8cae434aa1
Step 4/4 : CMD ["curl","-s","cip.cc"]
 ---> Running in af92ee01da66
Removing intermediate container af92ee01da66
 ---> ded6737bf511
Successfully built ded6737bf511

③ View mirror

[root@node1 docker]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED              SIZE
<none>                       <none>     ded6737bf511   About a minute ago   471MB
my_nginx                     latest     ecda82f680dc   9 hours ago          133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   20 hours ago         386MB
ubuntu                       latest     c29284518f49   9 days ago           72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago          133MB
redis                        latest     08502081bff6   4 weeks ago          105MB
fedora                       latest     055b2e5ebc94   2 months ago         178MB
centos                       latest     300e315adb2f   7 months ago         209MB
centos                       7.8.2003   afb6fca791e0   14 months ago        203MB
opensuse                     latest     d9e50bf28896   2 years ago          111MB
[root@node1 docker]# docker tag  ded6737bf511 centos_curl
[root@node1 docker]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED              SIZE
centos_curl                  latest     ded6737bf511   About a minute ago   471MB
my_nginx                     latest     ecda82f680dc   9 hours ago          133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   20 hours ago         386MB
ubuntu                       latest     c29284518f49   9 days ago           72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago          133MB
redis                        latest     08502081bff6   4 weeks ago          105MB
fedora                       latest     055b2e5ebc94   2 months ago         178MB
centos                       latest     300e315adb2f   7 months ago         209MB
centos                       7.8.2003   afb6fca791e0   14 months ago        203MB
opensuse                     latest     d9e50bf28896   2 years ago          111MB

④ Run the image to generate container records

[root@node1 docker]# docker run centos_curl
IP	: xx.xx.xx.195
 address	: Ezhou, Hubei, China
 Operator	: Unicom

Data II	: Xiangyang City, Hubei Province | Unicom

Data three	: Ezhou, Hubei, China | Unicom

URL	: http://www.cip.cc/xx.xx.xxx.195

⑤ Parameters cannot be passed in directly again

When tested, parameters cannot be directly passed in. This form is to overwrite the CMD in the image

[root@node1 docker]# docker run centos_curl -I
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-I": executable file not found in $PATH: unknown.
[root@node1 docker]# docker run centos_curl pwd
/

3. Use entrypoint to pass in parameters to the container normally

Edit Dockerfile

① Modify Dockerfile

[root@node1 docker]# cat Dockerfile 
FROM centos:7.8.2003
RUN rpm --rebuilddb && yum install epel-release -y
RUN rpm --rebuilddb && yum install curl -y
CMD ["curl","-s","cip.cc"]
ENTRYPOINT ["curl","-s","cip.cc"]

② Generate a new image

[root@node1 docker]# docker build .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM centos:7.8.2003
 ---> afb6fca791e0
Step 2/5 : RUN rpm --rebuilddb && yum install epel-release -y
 ---> Using cache
 ---> 96ed23874ab7
Step 3/5 : RUN rpm --rebuilddb && yum install curl -y
 ---> Using cache
 ---> 9a8cae434aa1
Step 4/5 : CMD ["curl","-s","cip.cc"]
 ---> Using cache
 ---> ded6737bf511
Step 5/5 : ENTRYPOINT ["curl","-s","cip.cc"]
 ---> Running in 02884a32cdc9
Removing intermediate container 02884a32cdc9
 ---> d61875c20211
Successfully built d61875c20211
[root@node1 docker]# docker tag d61875c20211 centos_curl_new
[root@node1 docker]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED          SIZE
centos_curl_new              latest     d61875c20211   32 seconds ago   471MB
centos_curl                  latest     ded6737bf511   20 minutes ago   471MB
my_nginx                     latest     ecda82f680dc   9 hours ago      133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   20 hours ago     386MB
ubuntu                       latest     c29284518f49   9 days ago       72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago      133MB
redis                        latest     08502081bff6   4 weeks ago      105MB
fedora                       latest     055b2e5ebc94   2 months ago     178MB
centos                       latest     300e315adb2f   7 months ago     209MB
centos                       7.8.2003   afb6fca791e0   14 months ago    203MB
opensuse                     latest     d9e50bf28896   2 years ago      111MB

③ Pass in parameters to the container normally

[root@node1 docker]# docker run centos_curl_new -I
HTTP/1.1 200 OK
Server: openresty
Date: Fri, 23 Jul 2021 12:46:47 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-cip-c: H

[root@node1 docker]# 

Note: the CMD instruction passed in is regarded as the parameter of ENTRYPOINT
The complete command executed in the container is curl - s cip.cc -I

Tags: Docker dockerfile

Posted by frostyhorse on Sat, 24 Jul 2021 07:54:09 +0930