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

MySQL5.7 parallel replication principle and implementation

Anyone who has a little knowledge of data operati...

Detailed introduction of Chrome developer tools-timeline

1. Overview Users expect the web applications the...

Browser compatibility summary of common CSS properties (recommended)

Why do we need to summarize the browser compatibi...

How to quickly deploy Redis as a Docker container

Table of contents getting Started Data storage Co...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

Solution to the error problem of Vscode remotely connecting to Ubuntu

1. Background of the incident: Because of work ne...

React antd tabs switching causes repeated refresh of subcomponents

describe: When the Tabs component switches back a...

How to configure Nginx to support ipv6 under Linux system

1. Check whether the existing nginx supports ipv6...

A brief discussion on the font settings in web pages

Setting the font for the entire site has always b...

Two ways to completely delete users under Linux

Linux Operation Experimental environment: Centos7...

JS Canvas interface and animation effects

Table of contents Overview Canvas API: Drawing Gr...

How to solve the phantom read problem in MySQL

Table of contents Preface 1. What is phantom read...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...

Detailed explanation of how to use the vue verification code component

This article example shares the specific implemen...