[Docker] Dockerfile basic commands and operations

[Docker] Dockerfile basic commands and operations

1. Dockerfile reserved commands

reserved word effect
FROM Which mirror is the current mirror based on
RUN Commands that need to be run when building an image
EXPOSE The port number exposed by the current container
WORKDIR Specify the working directory where the terminal logs in by default after the container is created, a foothold
ENV Used to set environment variables during image building
ADD Copy the files in the host directory into the mirror and the ADD command will automatically process the URL and decompress the tar package
COPY Similar to ADD, copy files and commands to the mirror
will copy the file/directory from the <original path> in the build context directory to the <target path> location in the mirror of the new layer
VOLUME Container data volumes for data storage and persistence
CMD Specify the command to run when a container starts.
There can be multiple CMD commands in the Dockerfile, but only the last one takes effect. CMD will be replaced by the parameters after docker run
ENTRYPOINT The purpose of specifying the command ENTRYPOINT to run when a container starts is the same as that of CMD, which is to specify the container launcher and its parameters

1.1 From command

The image based on which image is built will automatically pull the base image from when building docker hub, and From must appear as the first instruction of the Dockerfile.

The syntax is as follows:

FROM <image>
FROM <IMAGE>[:<tag>]	//使用版本不写则为latest
FROM <image>[@<digest>]	//使用摘要

1.2 RUN command

The RUN command will execute arbitrary commands in a new layer above the current image and commit the results. The resulting committed image will be used in the next step in the Dockerfile.

The syntax is as follows:

RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN ["executable", "param1", "param2"] (exec form)

RUN /bin/bash -c 'source $HOME/.bashrc && echo $HOME'
RUN ["/bin/bash", "-c", "echo hello"]

1.3 EXPOSE command

It is used to specify the port exposed to the outside world when the built image is run as a container.

The syntax is as follows:

EXPOSE 80/tcp	#如果没有显示指定则默认暴露都是tcp
EXPOSE 80/udp

1.4 CMD command

It is used to specify the command to be executed for the started container. There can only be one CMDcommand in the Dockerfile . If more than one command is listed, only the last command takes effect.

The syntax is as follows:

CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)

example:

  • CMD java -jar a.jar
  • CMD ["java", "-jar", "a.jar"] (recommended)

1.5 ENTRYPOINT command

Specify a command to run when a container starts. The purpose of ENTRYPOINT is the same as that of CMD. It is to specify the container launcher and its parameters.

The syntax is as follows:

ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2

1.6 WORKDIR command

Used to set the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions in the Dockerfile. If WORKDIR does not exist, it will be created even if it is not used in any subsequent Dockerfile instructions.

The syntax is as follows:

#使用绝对路径
WORKDIR /path/to/workdir

#先使用绝对路径,再使用相对路径
WORKDIR /a
WORKDIR b
WORKDIR c
# /a/b/c
RUN pwd

ENV DIRPATH=/path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd

1.7 ENV command

Used to set environment variables for building images, this value will appear in the environment of all subsequent instructions in the build phase.

The syntax is as follows:

ENV <key> <value>
ENV <key>=<value>

1.8 ADD command

Used to copy new files, directories or remote file urls from context and add them to the mirrored file system at the specified path.

The syntax is as follows:

ADD hom* /mydir/
ADD hom?.txt /mydir/
ADD test.txt relativeDir/
ADD test.txt /absoluteDir/

ADD url

1.9 COPY command

It is used to copy the specified file in the context (context) directory to the specified directory of the image.

The syntax is as follows:

COPY hom* /mydir/
COPY hom?.txt /mydir/
COPY test.txt relativeDir/
COPY test.txt /absoluteDir/

2. Practical experience

2.1 What is the difference between ADD and COPY?

Both ADD and COPY can add host files to the container, but ADD has a more powerful function. ADD can also be followed by url parameters, which means that ADD can download files specified by your url online, while COPY can only Add files in the context directory.


2.2 The difference between CMD and ENTRYPOINT

  • How to override the commands in CMD when running the container?

docker run image:version overrides its own definition command

  • How do the commands in ENTRYPOINT be overwritten when running the container?

docker run --entrypoint=override instruction image:version pass parameters


2.3 Combination of CMD and ENTRYPOINT

The image we create generally does not allow others to see the internal structure of the image, so we should fix a command with random parameters . This should allow CMD to be used in conjunction with ENTRYPOINT.

  • ENTRYPOINT : used to write a container fixed instruction
  • CMD : used to pass parameters to entrypoint

Note: JSON array syntax must be used when used together


2.4 Write Dockerfile to build jar package image

A simple Dockerfile example:

#指定基础镜像
FROM java:8-apline
#设置环境变量
ENV APP_PATH=/app
#设置工作目录
WORKDIR $APP_PATH
#将jar包导入新的镜像
ADD demo.jar $APP_PATH/apps.jar
#暴露端口
EXPOSE 8989
ENTRYPOINT ["java","-jar"]
CMD ["apps.jar"]

After writing, execute the following command in the Dockerfile directory:

docker build -t apps:1.0 .

Run the container:

docker run -p 8989:8989 apps:1.0

Guess you like

Origin blog.csdn.net/Decade_Faiz/article/details/131738182