SoFunction
Updated on 2025-03-10

Detailed introduction to Dockerfile

1. Introduction to dockerfile

1.1 About dockerfile

Introduction in the official website: Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Dockerfile is a text file that contains basic images and various instructions used to assemble new images.

Use the dockerfile file to define the image, then run the image and start the container.

1.2 Components of dockerfile file

A dockerfile file contains the following parts:

  • Basic image information: Use the FROM keyword to specify the basic image information, FROM is the first instruction in the dockerfile file.
  • Maintainer information: Use the MAINTAINER keyword to specify, you can usually use the name of the dockerfile file creator or email as the maintainer's information.
  • Mirror operation instructions: If no mirror operation instructions are executed, a new layer will be added to the mirror.
  • Container startup execution command: The user specifies the commands that need to be executed when starting the container, specified by: CMD ENTRYPOINT

Common mirror operation instructions:

instruction describe
FROM # Basic mirroring, everything starts from here
MAINTAINER Who wrote the mirror, name + email
RUN Commands that need to be run when building a mirror
ADD Add content: For example, add a tomcat compressed package
WORKDIR Mirrored working directory
VOLUME Mirror mounted directory
EXPOSE Keep exposed ports
CMD Specify the command to be run when this container is started, only the last one will take effect and can be replaced
ENTRYPOINT Specify the commands that need to be run when this container is started, and you can add commands
ONBUILD When building a DockerFile inherited, the ONBUILD instruction will be run. Trigger command
COPY Similar to ADD, copy our files to the mirror
ENV Set environment variables during construction

1.3 Dockerfile execution

After completing the writing of the dockerfile file, execute the docker build command, and a new docker image will be built based on the content of the context in the dockerfile file.

The entire construction process will be processed recursively. If a path or URL is included in the dockerfile, it will be constructed recursively.

2. Introduction to the docker bulid command

The docker build command is used to create images using Dockerfile

grammar:

docker build [OPTIONS] PATH | URL | -

Common parameters:

parameter explain
–build-arg=[] Set variables when creating images;
–cpu-shares Set CPU usage weights;
–cpu-period Limit CPU CFS cycle;
–cpu-quota Limit CPU CFS quota;
–cpuset-cpus Specify the CPU id to use;
–cpuset-mems Specify the memory id to use;
–disable-content-trust Ignore the verification and enable it by default;
-f Specify the Dockerfile path to use;
–force-rm Delete the intermediate container during the setting of the mirror;
–isolation Use container isolation technology;
–label=[] Set the metadata used by the mirror;
-m Set the maximum memory value;
–memory-swap Set the maximum value of Swap to memory + swap, "-1" means unlimited swap;
–no-cache The process of creating the image does not use cache;
–pull Try to update the new version of the image;
–quiet, -q Quiet mode, only the mirror ID is output after success;
–rm Delete the intermediate container after setting the mirror successfully;
–shm-size Set the size of /dev/shm, the default value is 64M;
–ulimit Ulimit configuration.
–squash Compress all operations in the Dockerfile into one layer.
–tag, -t: The name and tag of the image are usually in name:tag or name format; multiple tags can be set for one image in a single build.
–network: Default default. Setting the network mode of the RUN instruction during build

3. Use dockerfile to build images

Write a simple centos image

Create a file locally with the file name Dockerfile

  • The code is as follows
FROM centos:7.9.2009
# Specify the basic image as centosMAINTAINER  mufenggrow mufenggrow@
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


  • Build and test
    Build the image through commands and finally there is a.The syntax is as follows:

docker build -f dockerfile file name -t image name:[tag].

test:

Because the file we created at the beginning was calledDockerfileSo, no need to use-f parameter

[root@mufenggrow ~]# ls
  Dockerfile 

[root@mufenggrow ~]# docker build -t mufeng_centos .
[root@mufenggrow ~]# docker images
REPOSITORY      TAG        IMAGE ID       CREATED         SIZE
mufeng_centos   latest     5233b48a8b4e   9 seconds ago   667MB
centos          7.9.2009   eeb6ee3f44bd   19 months ago   204MB

  • Start the test to see ifconfig command
[root@mufenggrow ~]# ls
  Dockerfile    public  template  video  picture  document  download  music  desktop
[root@mufenggrow ~]# docker run -itd mufeng_centos /bin/bash
0801e25a61e5999b1616d1f0073f39a7e5431e34d983445b3f349b7da6bf3fe6
[root@mufenggrow ~]# docker exec -it 0801e25a /bin/bash
[root@0801e25a61e5 local]# pwd
/usr/local
[root@0801e25a61e5 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.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

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.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

Summarize

💕 OK, this is all I want to share with you today. See you next time!

This is the end of this article about the detailed introduction of Dockerfile files. For more related Dockerfile files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!