Introduction to the use of common Dockerfile commands

Introduction to the use of common Dockerfile commands

In previous articles, we have talked about some DockerFile related commands such as RUN, FROM, MAINTAINER, EXPOSE, etc. Today we will look at other DockerFile commands.

01 CMD

The CMD command is used to specify the command that needs to be run when a container is started. It is similar to the RUN instruction, but the differences are:

The RUN instruction is the command that needs to be executed when the image is built;

The CMD instruction specifies the instruction to be run when the container is started. Example:

docker run -it container_name /bin/ls

This command is to start a container and run /bin/ls

It is equivalent to manually entering docker and executing the ls command. You can use the following command instead in DockerFile:

CMD [ "/bin/ls" ]

It should be noted that only one CMD command can be specified in the DockerFile. Even if we specify multiple commands, only the last one can be executed.

If we use the CMD instruction in DockerFile, the CMD instruction will be run directly after the docker run command, for example:

docker run -it container_name

After starting this container, the /bin/ls command will be run directly.

02 ENTRYPOINT

This command is very similar to the CMD command above, except that any parameters specified after the docker run command will be passed as parameters to the command in the ENTRYPOINT instruction again.

03 WORKDIR

The WORKDIR instruction is used to set a working directory inside a container when creating a new container from an image. The ENTRYPOINT or CMD command will be executed in this directory.

This command often appears at the beginning of the DockerFile to set the working directory for a series of subsequent commands. After specifying certain commands, you can also use this command to switch the current directory. So, usually, the most common way to use the WORKDIR command should be as follows:

WORKDIR /data1/xxxxx

RUN xxxxx

RUN xxxxxx

WORKDIR /data2/xxxxx

RUN xxxxx

RUN xxxxx

It is a tool for constantly switching directories.

What if we specify a directory in DockerFile but don’t want to use it when running Docker?

The answer is to use docker run -w /var/dir1

After using the -w parameter, /var/dir1 will be used to overwrite the path in our DockerFile.

04 ENV

The ENV instruction is used to specify environment variables when generating a DockerFile. For example:

ENV PYRHON_HOME /usr/bin/python

It can be combined with the WORKDIR above, for example:

ENV PYRHON_HOME /usr/bin/python

WORKDIR $PYTHON_HOME

You can also use the env command in the container generated by the ENV command to view the environment variables of the current container, and you can see the value of PYRHON_HOME we set.

In addition to this method, we can use the docker -e command to pass environment variables, but the environment variables passed by this method are only valid at runtime.

05 USER

The USER instruction is used to specify the user as which the image will be executed. For example:

USER mysql

Of course, you can also use

USER user

USER user:group

USER uid

USER uid:gid

There are two points to note:

1. You can use the -u command in docker run to override the USER option in DockerFile;

2. If the USER option is not specified, the default user is root

06 VOLUME

The VOLUME instruction is used to add a volume to a container created from an image. A volume can exist in a specific directory within one or more containers. This directory can provide the function of sharing data or persisting data, for example:

1. Volumes can be shared and reused between containers

2. A container does not have to share volumes with other containers

3. Changes to the volume take effect immediately

4. The volume will exist until no container needs it

This feature allows us to add some code or data to the image instead of submitting it to the image. This will greatly reduce the size of the image. It allows us to share these contents between multiple containers, so VOLUME is often used to test the correctness of the container.

VOLUME [ "/volume" ]

This command will create a mount point named /volume for containers created using this image.

You can also use an array to create multiple mount points:

VOLUME [ "/data1", "/data2" ]

07 ADD

The ADD command is used to copy files and directories in the build environment to the image. The source and target locations of the file are required during the use of the ADD command, as follows:

ADD aaa.txt /data1/aaa.txt

This command will copy the aaa.txt file in the image build directory to the /data1/aaa.txt directory in the image. In addition to being a file in the build environment context, a source file can also be a URL.

During the ADD file process, if the destination address ends with /, DockerFile will consider the source location to be a directory, otherwise it will consider the source file to be a file.

Another thing to note: when the source file is a local archive file, such as tar.gz, Docker will decompress it into the target file.

08 COPY

The COPY command is very similar to ADD. The difference from ADD is that COPY does not support the URL method and will not actively decompress the file. The destination of the COPY instruction must be an absolute path inside the container.

Note:

1. The UID and GID of any file or directory created by this command will be set to 0

2. If the destination location does not exist, Docker will automatically create all required directory structures. Just like mkdir -p.

That’s all for today. It may seem a bit dry. I will add examples later to illustrate these commands.

The above is the detailed content of the introduction to the use of common Dockerfile commands. For more information on the use of Dockerfile commands, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Summary of common commands in Dockerfile
  • Detailed explanation of COPY and ADD commands in Dockerfile
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • A detailed introduction to the Dockerfile image building file and related commands in Docker

<<:  Detailed explanation of the role of the default database after MySQL installation

>>:  Implement a simple data response system

Recommend

Detailed explanation of JavaScript prototype chain

Table of contents 1. Constructors and instances 2...

Introduction to the use of select optgroup tag in html

Occasionally, I need to group select contents. In ...

Linux uses dual network card bond and screwdriver interface

What is bond NIC bond is a technology that is com...

Mysql 5.6 adds a method to modify username and password

Log in to MySQL first shell> mysql --user=root...

Analyzing ab performance test results under Apache

I have always used Loadrunner to do performance t...

How to introduce pictures more elegantly in Vue pages

Table of contents Error demonstration By computed...

How to implement Docker to dynamically pass parameters to Springboot projects

background Recently, some friends who are new to ...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...

A complete list of common Linux system commands for beginners

Learning Linux commands is the biggest obstacle f...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...

HTML text escape tips

Today I saw a little trick for HTML text escaping ...

Implementation of single process control of Linux C background service program

introduce Usually a background server program mus...