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--
What are the Dockerfile build commands, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
1) FROM (specify base image)
Build instructions, which must be specified and need to precede other Dockerfile instructions. Subsequent instructions depend on the image specified by the instruction. The underlying image specified by the FROM directive can be either in the official remote warehouse or in the local warehouse.
The FROM command tells docker which (release) image we are based on. The first instruction must be a FROM instruction. Also, if you create multiple mirrors in the same Dockerfile, you can use multiple FROM instructions.
The instruction is available in two formats:
FROM
Specifies that the underlying image is the last modified version of the image. Or:
FROM:
Specifies that the underlying image is a tag version of the image.
RUN is followed by the command to be executed. For example, if we want to install vim in the image, simply write RUN yum install-y vim in Dockfile.
2) MAINTAINER (used to specify the information of the image creator)
The build instruction is used to write information about the maker of the image to the image. When we execute the docker inspect command on the image, there are corresponding fields in the output to record this information.
Format:
MAINTAINER
3) RUN (for installing software)
Build directive, RUN can run any command supported by the underlying image. If the basic image chooses ubuntu, then the software management part can only use the commands of ubuntu.
The instruction is available in two formats:
RUN
RUN ["executable", "param1", "param2"...]
4) CMD (sets the action to be performed when container starts)
Set directive for the operation specified when container starts. This can be done by executing a custom script or by executing a system command. The directive can only exist once in the file, and if there is more than one, only the last one is executed.
The instruction is available in three formats:
CMD ["executable", "param1", "param2"]
CMD command param1 param2
When Dockerfile specifies ENTRYPOINT, the following format is used:
CMD ["param1", "param2"]
Where:
ENTRYPOINT specifies the path to an executable script or program that will be executed with param1 and param2 as parameters.
So if the CMD instruction uses the above form, then there must be a matching ENTRYPOINT in the Dockerfile.
5) ENTRYPOINT (sets the action to be performed when container starts)
The setting instruction, which specifies the command to be executed when the container starts, can be set multiple times, but only the last one is valid.
There are two formats:
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
The use of this instruction is divided into two cases, one is used alone, and the other is used in conjunction with the CMD instruction.
When used alone, if you also use the CMD command and CMD is a complete executable command, then the CMD instruction and ENTRYPOINT will override each other, and only the last CMD or ENTRYPOINT is valid.
# CMD instruction will not be executed, only ENTRYPOINT instruction will be executed
CMD echo "Hello, World!"
ENTRYPOINT ls-l
Another usage is used in conjunction with the CMD instruction to specify the default parameters of ENTRYPOINT. In this case, the CMD instruction is not a complete executable command, only the parameter part.
The ENTRYPOINT instruction can only specify the execution of the command in JSON mode, not parameters.
FROM ubuntu
CMD ["- l"]
ENTRYPOINT ["/ usr/bin/ls"]
6) USER (the user who sets the container container)
Set the directive to set the user who starts the container. The default is the root user.
# specify the user who is running memcached
ENTRYPOINT ["memcached"]
USER daemon
Or
ENTRYPOINT ["memcached", "- u", "daemon"]
7) EXPOSE (specify the port that the container needs to map to the host machine)
Sets an instruction that maps a port in the container to a port in the host machine. When you need to access the container, you can use the host machine's IP address and the mapped port instead of the container's IP address.
It takes two steps to complete the whole operation. First, use EXPOSE to set the container port to be mapped in Dockerfile, and then specify the-p option plus the port set by EXPOSE when running the container, so that the port number set by EXPOSE will be randomly mapped to a port number in the host machine.
You can also specify the port that needs to be mapped to the host machine, making sure that the port number on the host machine is not used. The EXPOSE instruction can set multiple port numbers at a time, and the-p option can be used multiple times when the corresponding container is run.
Format:
EXPOSE [...]
# Mapping a port
EXPOSE port1
# corresponding commands used to run the container
Docker run-p port1 image
# Mapping multiple ports
EXPOSE port1 port2 port3
# corresponding commands used to run the container
Docker run-p port1-p port2-p port3 image
# you can also specify a port number that needs to be mapped to the host machine
Docker run-p host_port1:port1-p host_port2:port2-p host_port3:port3 image
Port mapping is an important function of docker because the IP address of the container cannot be specified every time we run the container, but is randomly generated within the address range of the bridged Nic.
The IP address of the host machine is fixed. We can map the port of the container to a port on the host machine, eliminating the need to check the IP address of the container every time we access a service in the container.
For a running container, you can use docker port plus the port that needs to be mapped in the container and the ID of the container to see the port number mapped on the host machine.
8) ENV (used to set environment variables)
Mainly used to set the environment variables of the container runtime
Format:
ENV
Once set, subsequent RUN commands can be used, and after container starts, you can view the environment variable through docker inspect, or you can set or modify the environment variable when docker run-- env key=value.
If you have installed the JAVA program and need to set up JAVA_HOME, you can write this in Dockerfile:
ENV JAVA_HOME / path/to/java/dirent
9) ADD (dest path to copy files from src to container)
It is mainly used to add files from the host to the image.
Build directive, the permissions of all files and folders copied to container are 0755 and gid is 0; if it is a directory, then all files in that directory will be added to the container, excluding directories
If the file is in a recognizable compressed format, docker will help unzip it (note the compressed format); if it is a file and ends without a slash, it will be treated as a file and its contents will be written to
If it is a file and ends with a slash, the file is copied to the directory.
Format:
ADD
Is the relative path relative to the source directory being built, which can be the path of a file or directory, or a remote file url
Is the absolute path in container
10) VOLUME (specify mount point)
Set the instruction so that a directory in the container can store data persistently, which can be used by the container itself or shared with other containers. We know that the container uses AUFS
This file system cannot persist data, and when the container is closed, all changes are lost. This directive can be used in Dockerfile when the application in the container has a need to persist data.
Format:
VOLUME ["]
For example:
FROM base
VOLUME ["/ tmp/data"]
Run the container that generates the image through the Dockerfile, and the data in the / tmp/data directory still exists after the container is closed.
For example, another container also needs to persist data, and if you want to use the / tmp/data directory shared by the container above, you can run the following command to start a container:
Docker run-t-I-rm-volumes-from container1 image2 bash
Where: container1 is the name of the ID,image2 of the first container that runs image for the second container.
11) WORKDIR (switch directories)
Setting instruction, which can be switched multiple times (equivalent to the cd command), takes effect on RUN,CMD,ENTRYPOINT.
Format:
WORKDIR / path/to/workdir
# execute vim a.txt under / p1/p2
WORKDIR / p1 WORKDIR p2 RUN vim a.txt
12) ONBUILD (executed in child images)
Format:
ONBUILD
The command specified by ONBUILD is not executed when the mirror is built, but in its child mirror.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.