In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Dockerfile grammar in Docker". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Dockfile is a script interpreted by a Docker program. Dockerfile consists of one instruction, each corresponding to a command under the Linux. The Docker program translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and supported commands, and the Docker program solves the dependencies between these commands, similar to Makefile. The Docker program will read the Dockerfile and generate a custom image according to the instructions. An obvious script like Dockerfile is more acceptable to users than a black box like image, which clearly shows how image is created. With Dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on Dockerfile to regenerate image, saving us the trouble of typing commands.
The commands used in Dockerfile are
FROM
FROM specifies a basic image. In general, an available Dockerfile must be FROM as the first instruction. Image can be any reasonably existing image image.
FROM must be the first non-annotated instruction Dockerfile.
FROM can appear multiple times in a Dockerfile to make it easier to create a mixed images.
If tag is not specified, latest will be specified as the underlying image version to be used.
MAINTAINER
Here is the information used to specify the maker of the mirror
RUN
The RUN command executes any legitimate command in the current image and submits the execution result. After the command execution is submitted, the next instruction in the Dockerfile is automatically executed.
Hierarchical RUN instructions and generating submissions are in line with the core philosophy of Docker. It allows customized construction of image images at any point, just like versioning.
The RUN instruction cache does not automatically expire when the next command is executed. For example, the cache of RUN apt-get dist-upgrade-y may be used for the next instruction. The no-cache flag can be used to force the cancellation of cache usage.
ENV
The ENV directive can be used to set environment variables for the docker container
The environment variables set by ENV can be viewed using the docker inspect command. You can also use docker run-- env = to modify the environment variable.
USER
USER is used to switch and run the master identity. Docker uses root by default, but if it is not necessary, it is recommended to change the user identity. After all, the permission of root is too large, and there is a security risk in use.
WORKDIR
WORKDIR is used to switch working directories. The default working directory of Docker is /, and only RUN can execute the cd command to change directories, and it only works on the current RUN, that is, each RUN is carried out independently. If you want other instructions to be executed in the specified directory, you have to rely on WORKDIR. The directory change of the WORKDIR action is persistent, and you don't have to use WORKDIR once before each instruction.
COPY
COPY copies and adds files from the path to the container's internal path.
Must be a file or directory for the source folder, or it can be a remote url
Is the absolute path in the target container.
All new files and folders create UID and GID. In fact, if it is a remote file URL, then the permissions of the target file will be 600.
ADD
ADD copies and adds files from the path to the container's internal path.
Must be a file or directory for the source folder, or it can be a remote url. Is the absolute path in the target container.
All new files and folders create UID and GID. In fact, if it is a remote file URL, then the permissions of the target file will be 600.
VOLUME
Create a mount point that can be mounted from a local host or other container, which is generally used to store databases, data that needs to be maintained, and so on.
EXPOSE
The EXPOSE directive specifies the port specified for forwarding when docker allows it.
CMD
Dockerfile. There can be only one CMD instruction in. If you specify more than one, the last CMD instruction takes effect.
The main purpose of the CMD directive is to provide a default execution container. These default values can include or omit executables.
When you use shell or exec format, CMD
This command will be executed automatically.
ONBUILD
The function of ONBUILD is to delay the execution of instructions until the next Dockerfile that uses FROM is only delayed once when the image is established.
The usage scenario of ONBUILD is to get the latest source code (with RUN) and qualify the system framework when setting up the image.
ARG
ARG is a new directive added to the Docker1.9 version.
The variables defined by ARG are valid only when the image is established, and the variables disappear after the establishment is completed.
LABEL
Define an image tag Owner and assign it to the value of the variable Name. (LABEL Owner=$Name)
ENTRYPOINT
Is to specify the command or file to be executed when Docker image runs as instance (that is, Docker container).
Note:
Both CMD and ENTRYPOINT can be used to specify the program to start running, and both commands have two unused syntax:
CMD ls-l
Or
CMD ["ls",''- l "]
For the first syntax, docker automatically adds "/ bin/sh-c" to the command, which can lead to unexpected behavior. To avoid this behavior, we recommend that all CMD and ENTRYPOINT should use the second syntax.
If both are used at the same time, make sure there is nothing wrong with their meaning. Generally speaking, when you need to use two cases at the same time, only the binary,CMD specified by ENTRYPOINT gives the default parameters for running.
Merge commands as much as possible
Each command in Dockerfile creates a new layer, and there is a limit to the number of layer a container can have. So combining logically coherent commands as much as possible can reduce the number of layers of layer. The method of merging commands can include merging multiple commands that can be merged (EXPOSE, ENV,VOLUME,COPY).
Each command in Dockerfile creates a new layer, and there is a limit to the number of layer a container can have. So combining logically coherent commands as much as possible can reduce the number of layers of layer, which can also speed up compilation.
Merge multiple commands (RUN,EXPOSE, ENV,VOLUME,COPY) that can be merged, such as:
EXOISE 80EXOISE 8080CMD cd / tmpCMD ls
= = >
EXOISE 80 8080CMD cd / tmp & & ls
The ADD command and the COPY command function to a large extent the same. But COPY semantics are more straightforward, so we recommend using the COPY command as much as possible. The only exception is that the ADD command comes with the decompression function. If you need to copy and extract a file into the image, we can use the ADD command. In addition, we all recommend using the COPY command.
ADD 1.1.1.100:1234/jdk-8u74-linux-x64.tar.gz / usr/local/
The use of USER
Docker defaults that all applications will run under the root user of the container, but this will cause some potential security risks. Container running in a production environment is best run under an unprivileged user through the USER command.
This is the end of the content of "how to use Dockerfile grammar in Docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.