Explanation of reserved word instruction in Dockerfile

Keywords: CentOS Docker bash

I learned Docker briefly before. At that time, it was to quickly deploy a project. It passed quickly. I was a little unfamiliar with the writing of Dockerfile files.

So I wrote this article. I hope I can help you!!!

1, What is Dockerfile?

Concept:

Dockerfile is a build file used to build Docker images. It is a script composed of a series of commands and parameters

Three steps of Construction:

  1. Write Dockerfile file
  2. docker build
  3. docker run

Centos case:

centos case:

FROM scratch  #True basic image,
ADD centos-7-x86_64-docker.tar.xz /

# Meaning of label description
LABEL \  
    org.label-schema.schema-version="1.0" \
    org.label-schema.name="CentOS Base Image" \
    org.label-schema.vendor="CentOS" \
    org.label-schema.license="GPLv2" \
    org.label-schema.build-date="20201113" \
    org.opencontainers.image.title="CentOS Base Image" \
    org.opencontainers.image.vendor="CentOS" \
    org.opencontainers.image.licenses="GPL-2.0-only" \
    org.opencontainers.image.created="2020-11-13 00:00:00+00:00"

CMD ["/bin/bash"] #Command executed on the last line

Where can I find it? Find it on hub.docker.com: centos.

We won't, but we can first see how others write and copy homework. I think everyone is familiar with it. Commonly known as CV Dafa 😂.

2, Analysis of Dockerfile construction process

Getting started:

  1. Each reserved word instruction (today's focus) must be uppercase and followed by at least one parameter.

    For example:

    FROM scratch  #True basic image,
    ADD centos-7-x86_64-docker.tar.xz /
    
  2. Instructions are executed from top to bottom

  3. #Indicates a comment.

    #This is the annotation
    
  4. Each instruction creates a new mirror layer and commits the mirror.

    Just like the following, you can set a doll.

Dockerfile performs process analysis:

  1. docker runs a container from the underlying image
  2. Execute an instruction and make changes to the container
  3. Perform an operation similar to docker commit to submit a new image layer.
  4. docker then runs a new container based on the image just submitted
  5. Execute the next instruction in the dockerfile until all instructions are executed

There are cases in the following text. It is easier to understand when looking back with cases.

Xiaofan:

At this stage, we regard Dockerfile, Docker image and Docker container as three different stages of software.

Dockerfile is development oriented -- > docker image becomes the delivery standard -- > docker container involves deployment and operation and maintenance

  • Everything required by the process is defined in the Dockerfile. The environment variables, dependent packages and runtime environment that need to be relied on before are written into the Dockerfile file. It's much simpler than downloading so many software and configuring so many in the Linux server before. At least it's much easier for me to deploy with Docker.

  • Docker image is a docker image generated when docker build s after a file is defined with Dockerfile. Services will not be provided until the docker image is run.

  • Docker container can provide services when running.

3, Dockerfile reserved word instruction

The dockerfield reserved word instructions are roughly as follows:

  1. FROM
  2. MAINTANINER
  3. RUN
  4. EXPOSE
  5. WORKDIR
  6. ENV
  7. ADD
  8. COPY
  9. VOLUME
  10. CMD
  11. ENTRYPOINT
  12. ONBUILD

3.1,FROM

Base image, that is, the image on which the current new image is created.

#Creating image based on openjdk:8
FROM openjdk:8

3.2,MAINTAINER

Name and email address of the image maintainer

MAINTAINER Ning Zaichun<crush@163.com>

3.3,RUN

Instructions that need to be run when the container is built

RUN mkdir -p /conf/my.cn

3.4,EXPOSE

The exposed port of the current container

#Expose required ports for MyCat
EXPOSE 8066 9066

3.5,WORKDIR

Specify the default login working directory of the terminal after creating the container

#Container data volume for data storage and persistence
WORKDIR /usr/local/mycat

3.6,ENV

Used to set environment variables during image building

#Used to set the environment variable env MYCAT during the mirror building process_ HOME=/usr/local/mycat

This environment variable can be used in any subsequent RUN instruction, just as the environment variable prefix is specified in front of the command; These environment variables can also be used directly in other instructions.

For example:

RUN $MYCAT_HOME/mycat

3.7 ADD and COPY

ADD:

Copy the files in the host directory into the image, and the ADD command will automatically process the URL and decompress the tar compressed package

ADD centos-6-docker.tar.xz / 

COPY:

Similar to ADD, copy files and directories to the image.

Copy the file / directory from the < source path > in the build context directory to the < target path > location in the mirror of the new layer

COPY src destCOPY ["src" "dest"]

3.8,VOLUME

Container data volume, used for data persistence and data saving.

#Expose the address of the mycat configuration file to the mapping address, and directly map the folder VOLUME /usr/local/mycat of the host machine at startup

3.9. CMD and ENTRYPOINT

CMD

CMD instructions are similar to RUN in two formats:

  • shell format: CMD < command >
  • exec format: CMD ["executable", "parameter 1", "parameter 2"....]

There can be multiple CMD instructions in Dockerfile, but only the last one takes effect. CMD will be replaced by the parameters after docker run.

ENTRYPOINT

Specify a command to run when the container starts.

The purpose of ENTRYPOINT, like CMD, is to start the program and parameters in the specified container.

difference:

Here is a brief explanation of the difference. You can understand CMD as coverage

CMD cat /conf/my.cnfCMD /bin/bash

These two instructions are written in the Dockerfile file. Only CMD /bin/bash will be executed instead of CMD cat /conf/my.cnf, because CMD /bin/bash directly overwrites the previous one.

ENTRYPOINT is different. You can simply understand ENTRYPOINT as adding.

It is mainly reflected in docker run. If the last CMD in the dockerfile file is used, no additional commands can be added at runtime, otherwise the CMD command in the dockerfile will be overwritten.

At the end of the last line entry point in the Dockerfile file, you can append some commands after the docker run command

3.10,ONBUILD

When building an inherited Dockerfile, run the command. After the parent image is inherited by the child, the onbuild of the parent image is triggered.

4, Actual combat cases

4.1. Create your own Centos image

4.1.1 introduction:

Let's first pull a centos from alicloud to see what the problems are, and then we can customize it.

docker pull centos # Pull image docker run -it centos #Run mirror# =====Test = = = = = vim ceshi.txtifconfig pwd

Why is this? Because Centos in the docker warehouse is a compact version, which only has a kernel and nothing else.

Custom Centos is required to solve the above problems.

4.1.2. Prepare Dockerfile file

Write Dockerfile file for our custom Centos

FROM centosMAINTAINER Ning Zaichun<crush@163.com>ENV MYPATH /usr/localWORKDIR $MYPATHRUN yum -y install vimRUN yum -y install net-toolsEXPOSE 80 CMD echo $MYPATHCMD echo "success"CMD /bin/bash  #Only the last one will run

Then copy this in.

mkdir -p /usr/local/docker/mycentos # Create your own storage location vim Dockerfile

4.1.3. Build centos image

docker build -f /usr/local/docker/mycentos/Dockerfile  -t mycentos:1.1 .

Explanation:

  • -f: Followed by the Dockerfile file

  • -t: Followed by the image name and version number.

  • Last decimal point: indicates the current directory.

  • docker build -f Dockerfile file -t Image name:tag .
    
  • When the dockerfile is named dockerfile and in the current directory, it can be abbreviated as:

  • docker build  -t Image name:tag .docker build   -t mycentos:1.1 .
    

Execution:

Seeing the last one means success.

docker images view all images:

4.1.4. Run Centos image

docker run -it mycentos:1.3pwdifconfig

The reason why we switch the directory of the container from / to / usr/local is that it is written in the dockerfile file.

ENV MYPATH /usr/localWORKDIR $MYPATH

4.1.5. View the change history of the image

docker history mycentos:1.1

It can also be seen here that the image is built layer by layer by the instructions in the Dockerfile file.

4.2. ONBUILD example

Take the lead in building an image

Write a dockerfile file named dockerfile2

FROM centosRUN yum -y install  curlONBUILD RUN echo "I am inherited by the sub image and output this statement"CMD ["crul", "-s","http://ip.cn"]
docker build -f /usr/local/docker/mycentos/Dockerfile2 -t my_father_centos .

Building an image inherits an image

Write a dockerfile file named dockerfile3

FROM my_father_centosRUN yum -y install  curlCMD ["crul", "-s","http://ip.cn"]
docker build -f /usr/local/docker/mycentos/Dockerfile3 -t my_son_centos .

You can see that the statements in the parent image are output.

5, Talk to yourself

I feel shallow on paper. I absolutely know that I have to practice it.

Hello, I'm blogger Ning Zaichun: homepage

A young man who likes literature and art but embarks on the road of programming.

Hope: when we meet on another day, we have achieved something.

Change the kitten's expression and let's slap together.

Posted by tomlei on Tue, 02 Nov 2021 03:42:41 -0700