In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use dockerfile grammar in docker. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
FROM keyword
Specifies the base image and must be the first instruction. If it is not based on any image, it is written as: FROM scratch. It also means that the next instruction will start as the first layer of the mirror, syntax:
FROM FROM: FROM:
There are three ways to write, in which sum is optional. If not, the default value is latest. For security, try to use official image as base image.
Example:
FROM scratch # make base imageFROM centos # use centos as the base imageLABEL keyword
Specify a label for the image, syntax:
LABEL =...
A Dockerfile species can have multiple LABEL, as follows:
LABEL "com.example.vendor" = "ACME Incorporated" LABEL com.example.label-with-value= "foo" LABEL version= "1.0" LABEL description= "This text illustrates\ that label-values can span multiple lines."
However, it is not recommended to write in this way. it is best to write on a single line, and use the\ symbol if it is too long to break a line.
As follows:
LABEL multi.label1= "value1"\ multi.label2= "value2"\ other= "value3"
Note: LABEL will inherit the LABEL of the underlying image. If the same key is encountered, the value will be overridden.
Example:
LABEL maintainer= "asd@163.com" # maintainer Information LABEL version= "1.0" # version LABEL description= "this is the description" # Image description Information RUN keyword
The function is to run the specified command. Every time you run RUN, a new layer is generated for image. The RUN command has two formats.
1. RUN 2.RUN ["executable", "param1", "param2"]
The first one is followed directly by the shell command.
Default / bin/sh-c on linux operating system
Default cmd / S / C on windows operating system
The second is similar to function calls. Executable can be understood as an executable file, followed by two parameters.
Compare the two writing methods:
RUN / bin/bash-c 'source $HOME/.bashrc; echo $HOME
RUN ["/ bin/bash", "- c", "echo hello"]
Note: multiline commands do not write multiple RUN because each instruction in Dockerfile creates a layer. How many layers of images are built by how many RUN, which will result in bloated and multi-tiered images, which not only increases the time for component deployment, but also is prone to errors.
When there are more or longer commands, it is recommended to wrap the commands. The newline character for RUN writing is\
Example:
RUN yum update & & yum install- y vim\ python-devRUN apt-get update & & apt-get install- y perl\ pwgen-- no-install-recommends & & rm-rf\ / var/lib/apt/list/*WORKDIR keyword
Set the working directory, which is effective for RUN,CMD,ENTRYPOINT,COPY,ADD. Equivalent to cd, it will be created if there is no directory to open, and can be set multiple times. Syntax:
WORKDIR / path/to/workdir
Example:
WORKDIR / ROOT # change the working directory to root WORKDIR / test # change the working directory to the test directory otherwise create WORKDIR demo # combined with the previous sentence the working directory is changed to the / test/demo directory
Try to use WORKDIR instead of RUN cd, and try to use office-to-directory.
ADD keyword
A copy command to copy the file to the mirror. If you think of the host and the container as two linux servers, then this command is similar to scp, except that scp requires permission verification with a user name and password, but ADD does not. The syntax is as follows:
1. ADD. 2. ADD [",..."]
The path can be an absolute path within the container or a relative path relative to the working directory
It can be a local file or a local compressed file, or a url. If you write it as a url, then ADD is similar to the wget command. ADD can not only add a file to a specified directory, but also unzip the added compressed file.
It can be written in the following ways:
ADD test relativeDir/ # copy the test into the container, relative to the relativeDir directory under the current working directory
ADD test / relativeDir # copy the test to the relativeDir directory under the root directory in the container
ADD http://example.com/foobar / # downloads network files to the root directory
Try not to write it into a folder. If it is a folder, it will copy the contents of the entire directory, including file system metadata.
There are the following considerations:
1. If the source path is a file and the destination path ends with /, docker will treat the destination path as a directory and copy the source files to that directory. If the destination path does not exist, the destination path is automatically created.
2. If the source path is a file and the destination path ends with /, then docker will treat the destination path as a file. If the destination path does not exist, a file with the same source file is created under the name of the destination path; if the target file is an existing file, it is overwritten with the source file, of course, only the content is overwritten, and the file name is still the target file name. If the destination file is actually an existing directory, the source file is copied to that directory. Note that in this case, it is best to end with / to avoid confusion.
3. If the source path is a directory and the destination path does not exist, docker will automatically create a directory with the destination path and copy the files under the source path. If the destination path is an existing directory, docker copies the files in the source path directory to that directory.
4. If the source file is an archive file (compressed file), docker will automatically decompress it.
COPY keyword
If you look at this name, you can see that it is another copy command, which is basically the same as ADD. COPY can only be a local file. The syntax is as follows:
1. COPY. 2. COPY [",..."]
Example:
ADD hello / # copy the hello file to the root directory in the container ADD test.tar.gz / # add the compressed file to the root directory in the container and extract the WORKDIR / root # change the working directory to the root directory ADD hello test/ # add the hello file to the / root/test directory WORKDIR / root # change the working directory to the root directory copy hello test/ # copy the hello file to the / root/testENV keyword
The function sets constants for setting environment variables, and there are two kinds of syntax:
1. ENV 2. ENV =
The difference between the two is that the first is to set one at a time, and the second is to set more than one at a time.
Example:
ENV MYSQL_VERSION 5.6 # set constant RUN apt-get install-y mysql-server= "${MYSQL_VERSION}"\ # use the constant & & rm-rf / var/lib/apt/list/*CMD keyword
Function is the command to be run when the container starts, and the syntax can be written in three ways.
1. CMD ["executable", "param1", "param2"] 2. CMD ["param1", "param2"] 3. CMD command param1 param2
The third method is easy to understand. The first and second methods of shell execution and writing are actually executable files plus parameters, with examples to illustrate the two writing methods:
CMD ["sh", "- c", "echo $HOME"
CMD ["echo", "$HOME"]
Additional details: this includes parameters must be in double quotes, that is, ", cannot be single quotation marks. Never write as single quotation marks, because after the parameter is passed, docker parses a JSON array.
Note:
Commands that are executed by default when the container starts
If docker run specifies another command, the CMD command is ignored
If more than one CMD is defined, only the last one will execute
RUN & CMD
Don't confuse RUN with CMD. RUN is the command that runs when the component container and submits the running result. CMD is the command that is executed when the container starts, but does not run when the component is constructed. The component tightly specifies what this command looks like.
ENTRYPOINT keyword
Function is the default command at startup, and the syntax is as follows:
1. ENTRYPOINT ["executable", "param1", "param2"] 2.ENTRYPOINT command param1 param2
Compared with CMD (these two commands are too similar and can be used together):
1. Similarities:
Only one article can be written. If more than one is written, only the last one will take effect.
The container does not run until it is started, and the timing is the same.
two。 Differences:
ENTRYPOINT will not be overwritten by the running command, while CMD will be overwritten
If we write both ENTRYPOINT and CMD in Dockerfile, and the CMD instruction is not a complete executable command, then the content specified by CMD will be used as an argument to ENTRYPOINT
Shell format: execute RUN apt-get install-y vimCMD echo "hello docker" ENTRYPOINT echo "hello docker" Exec format as shell: command, parameter format RUN ["apt-get", "install", "- y", "vim"] CMD ["/ bin/echo", "hello docker"] ENTRYPOINT ["/ bin/echo", "hello docker"]
Example: the following two dockerfile results are the same
FROM centos # specifies that the basic image is centosEVN name Docker # set the constant name value to DockerENTRYPOINT echo "hello $name" # execute the acho command
Output hello Docker when the image generated by the above dockerfile runs the container
FROM centos # specify the basic image as centosEVN name Docker # set the constant name value to DockerENTRYPOINT ["/ bin/echo", "hello $name"] # execute the acho command
The image generated by the above dockerfile outputs hello $name when running the container, because ENTRYPOINT ["/ bin/echo", "hello $name"] specifies that the container will run when it starts. The echo command will not recognize $as a variable. Make the following modifications:
FROM centos # specify the basic image as centosEVN name Docker # set the constant name value to DockerENTRYPOINT ["/ bin/bash", "- c", "echo", "hello $name"] # execute the acho command in shell
Output hello Docker when the container is started
On how to use dockerfile syntax in docker to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.