Dockerfile function:
Dockerfile is a "command file" used in Docker to install all dependencies of the project with one click. It is written according to the official requirements of Docker. During build, it will execute step by step according to similar shell commands and build an image;
explain:
From the perspective of application software, DockerFile, docker image and docker container represent three different stages of software respectively.
DockerFile is the raw material (code) of the software
Docker images are software deliverables (. apk)
Docker container is the running state of the software (downloaded and installed by customers)
DockerFile is development oriented, Docker image becomes the delivery standard, and Docker container involves deployment and operation and maintenance. None of the three is indispensable!
Dockerfile common instructions:
keyword:
FROM # basic image. Which image is the current new image based on
MAINTAINER # image MAINTAINER information, usually name + email
Commands to RUN when the RUN # container is built
Export # the port reserved by the current container
WORKDIR # specifies the working directory in which the terminal logs in by default after creating the container
Setting environment variables during ENV # building images
ADD # copies the files in the host directory into the image, and the ADD command will automatically process the URL and decompress the tar compressed package
COPY # is similar to ADD, which copies the files and directories of the host to the image
VOLUME # container data VOLUME, used for data saving and persistence
CMD # specifies the command to be run when a container is started. There can be multiple CMD instructions in dockerfile, but only the last one takes effect
ENTRYPOINT # specifies a command to run when the container starts, which is the same as CMD
ONBUILD # the command that runs when building an inherited Dockerfile. After the parent image is inherited by the child image, the ONBUILD of the parent image is triggered
Build your own Centos image using Dockerfile
1. Write Dockerfile
To view the official default CentOS:
Purpose: make our own image have the following: default path after login, vim editor, view network configuration, ifconfig support
Prepare to write Dockerfile file
[root@wxy home]# mkdir mycentos [root@wxy home]# cd mycentos/ [root@wxy mycentos]# ls [root@wxy mycentos]# mkdir dockerfile-test [root@wxy mycentos]# ls dockerfile-test [root@wxy mycentos]# cd dockerfile-test/ [root@wxy dockerfile-test]# vim mydockerfile-centos [root@wxy dockerfile-test]# cat mydockerfile-centos FROM centos MAINTAINER wxy<12345@qq.com> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 9800 CMD echo $MYPATH CMD echo "--------end---- -" CMD /bin/bash
2. Build
docker build -f mydockerfile-centos -t mycentos:0.1 .
Pay attention to the last one Don't miss it Represents the current directory
[root@wxy dockerfile-test]# docker build -f mydockerfile-centos -t mycentos:0.1 . Sending build context to Docker daemon 2.048 kB Step 1/10 : FROM centos ---> 5d0da3dc9764 Step 2/10 : MAINTAINER wxy<12345@qq.com> ---> Running in 3a1cb3e6e5a4 ---> 802bfb07b9cc Removing intermediate container 3a1cb3e6e5a4 Step 3/10 : ENV MYPATH /usr/local ---> Running in 4deaa628d401 ---> c2fef24e1a8b Removing intermediate container 4deaa628d401 Step 4/10 : WORKDIR $MYPATH ---> 97d27980f64c Removing intermediate container 1ee53e441e70 Step 5/10 : RUN yum -y install vim ---> Running in ab569156525f CentOS Linux 8 - AppStream 5.3 MB/s | 8.4 MB 00:01 CentOS Linux 8 - BaseOS 8.8 MB/s | 4.6 MB 00:00 CentOS Linux 8 - Extras 12 kB/s | 10 kB 00:00 Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: vim-enhanced x86_64 2:8.0.1763-16.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-16.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-16.el8 appstream 49 k which x86_64 2.21-16.el8 baseos 49 k Transaction Summary ================================================================================ Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: (1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 421 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 1.3 MB/s | 49 kB 00:00 (3/5): which-2.21-16.el8.x86_64.rpm 1.4 MB/s | 49 kB 00:00 (4/5): vim-common-8.0.1763-16.el8.x86_64.rpm 16 MB/s | 6.3 MB 00:00 (5/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 2.8 MB/s | 1.4 MB 00:00 -------------------------------------------------------------------------------- Total 6.1 MB/s | 7.8 MB 00:01 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-16.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-16.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-16.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 4/5 Verifying : which-2.21-16.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64 vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch which-2.21-16.el8.x86_64 Complete! ---> 7760f2961d01 Removing intermediate container ab569156525f Step 6/10 : RUN yum -y install net-tools ---> Running in 1b744805bd84 Last metadata expiration check: 0:00:08 ago on Sat Jan 15 06:27:34 2022. Dependencies resolved. ================================================================================ Package Architecture Version Repository Size ================================================================================ Installing: net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k Transaction Summary ================================================================================ Install 1 Package Total download size: 322 k Installed size: 942 k Downloading Packages: net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 6.1 MB/s | 322 kB 00:00 -------------------------------------------------------------------------------- Total 320 kB/s | 322 kB 00:01 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1 Installed: net-tools-2.0-0.52.20160912git.el8.x86_64 Complete! ---> aa6cc4dd61fd Removing intermediate container 1b744805bd84 Step 7/10 : EXPOSE 9800 ---> Running in a8f69025a496 ---> a01c616f0ae3 Removing intermediate container a8f69025a496 Step 8/10 : CMD echo $MYPATH ---> Running in ca8f58a9b757 ---> 858b4f5cc829 Removing intermediate container ca8f58a9b757 Step 9/10 : CMD echo "--------end---- -" ---> Running in 7f6a92f39011 ---> 319cf5a42b4a Removing intermediate container 7f6a92f39011 Step 10/10 : CMD /bin/bash ---> Running in 36e18074ee53 ---> 87d59549a036 Removing intermediate container 36e18074ee53 Successfully built 87d59549a036
3. Run
docker run -it new image name: TAG
[root@wxy dockerfile-test]# docker run -it mycentos:0.1 [root@65bc0003ac9c local]# docker ps -a bash: docker: command not found [root@65bc0003ac9c local]# pwd /usr/local [root@65bc0003ac9c local]# vim [root@65bc0003ac9c local]# vim test [root@65bc0003ac9c local]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.0.4 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe12:4 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:12:00:04 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) 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 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@65bc0003ac9c local]#
As you can see, our own image already supports vim and ifconfig commands.