In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Use Dockerfile to allow users to create custom images.
Basic structure
Dockerfile consists of one line of command statements and supports comment lines that begin with #. Generally speaking, Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and instructions to execute when the container starts.
For example:
/ / basic image information FROM daocloud.io/node:7 / / maintainer information MAINTAINER abel.yang LABEL Descripttion= "This image is build for web" / / image operation instruction RUN mkdir-p / opt/apps/eppCOPY. / opt/apps/eppWORKDIR / opt/apps/epp/epp-web/serverENV LANG C.UTF-8ENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo/Asia/Shanghai / etc/localtime & & echo Asia/Shanghai > / etc/timezoneEXPOSE 3001ram / execute the instruction CMD when the container starts ["npm", "start"]
At first, the name of the image on which it is based must be specified, and then the maintainer information is recommended. Then there is the mirror operation instruction, such as the RUN instruction, and the RUN instruction will execute the following command on the mirror.
Each time you run a RUN instruction, the mirror adds a new layer and commits. Finally, there is the CMD directive, which specifies the operation command when the container is run.
Instruction
INSTRUCTION arguments, instructions include FROM, MAINTAINER, RUN, etc.
The format is FROM or FROM:.
The first instruction must be a FROM instruction. Also, if you create multiple mirrors in the same Dockerfile, you can use multiple FROM instructions (one for each mirror).
MAINTAINER
Format is MAINTAINER, specifying maintainer information.
RUN
The format is RUN or RUN ["executable", "param1", "param2"].
The former will run the command / bin/sh-c in the shell terminal; the latter will be executed using exec. Specifying the use of other terminals can be done in a second way, such as RUN ["/ bin/bash", "- c", "echo hello"].
Each RUN instruction executes the specified command based on the current mirror and is submitted as a new mirror. You can use\ to wrap lines when the command is long.
CMD
Three formats are supported
CMD ["executable", "param1", "param2"] is executed using exec, recommended; CMD command param1 param2 is executed in / bin/sh and provided to applications that require interaction; CMD ["param1", "param2"] provides default parameters to ENTRYPOINT
Specifies the command to be executed when the container is started, and there can be only one CMD command per Dockerfile. If multiple commands are specified, only the last one will be executed.
If the user starts the container and specifies the command to run, the command specified by CMD will be overwritten.
EXPOSE
The format is EXPOSE [...].
Tell the port number exposed by the Docker server container for use by the interconnected system. When starting the container, the host will automatically assign a port to be forwarded to the specified port through-P _ ~ ~ Docker.
ENV
The format is ENV. Specify an environment variable that will be used by subsequent RUN instructions and maintained while the container is running.
ADD
The format is ADD.
This command copies the specified to the container. It can be a relative path to the directory where Dockerfile is located, a URL; or a tar file (which is automatically extracted to a directory).
COPY
The format is COPY.
Copy the local host's (the relative path of the directory where the Dockerfile is located) to the container.
COPY is recommended when using the local directory as the source directory.
ENTRYPOINT
There are two formats:
ENTRYPOINT ["executable", "param1", "param2"] ENTRYPOINT command param1 param2 (executed in shell).
Configure the commands that are executed after the container starts and cannot be overridden by the parameters provided by docker run.
There can be only one ENTRYPOINT per Dockerfile, and when more than one is specified, only the last one takes effect.
VOLUME
The format is VOLUME ["/ data"].
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.
USER
The format is USER daemon.
Specify the user name or UID when the container is run, and subsequent RUN will also use the specified user.
When the service does not require administrator privileges, you can specify the running user through this command. And you can create the required users before.
For example: RUN groupadd-r postgres & & useradd-r-g postgres postgres. To obtain administrator privileges temporarily, you can use gosu instead of recommended sudo.
WORKDIR
The format is WORKDIR / path/to/workdir.
Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT instructions.
Multiple WORKDIR instructions can be used, and subsequent commands, if the argument is a relative path, are based on the path specified by the previous command. For example
The final path of WORKDIR / a WORKDIR b WORKDIR c RUN pwd is / a/b/c.
ONBUILD
The format is ONBUILD [INSTRUCTION].
Configure the operation instructions that are performed when the created mirror is used as the base mirror for other newly created mirrors.
For example, Dockerfile creates a mirror image-A with the following content.
[...] ONBUILD ADD. / app/srcONBUILD RUN / usr/local/bin/python-build-- dir / app/src [...]
If you create a new image based on image-A, when you specify the base image using FROM image-An in the new Dockerfile, the content of the ONBUILD instruction will be executed automatically, which is equivalent to adding two instructions later.
FROM image-A
# Automatically run the followingADD. / app/srcRUN / usr/local/bin/python-build-- dir / app/src
Use the image of the ONBUILD directive. It is recommended to indicate it in the tag, such as ruby:1.9-onbuild.
Create a mirror
After you have written the Dockerfile, you can use the docker build command to create the mirror.
Docker build-t image name. / / Note. Don't forget.
Here are two examples of Dockerfile on Dockerhub.
# Nginx## VERSION 0.0.1FROM ubuntuMAINTAINER Victor Vieux RUN apt-get update & & apt-get install-y inotify-tools nginx apache2 openssh-server# installs inotify-tools,nginx,apache2,openssh-server based on the parent image of ubuntu to create a new Nginx image. # Firefox over VNC##VERSION 0.3FROM ubuntu# Install vnc,xvfb in order to create a 'fake' display and firefoxRUN apt-get update & & apt-get install-y x11vnc firefoxRUN mkdir / .vnc# Setup a psswordRUN x11vnc-storepasswd 1234 ~ / .vnc / passwd#Autostart firefoxRUN bash-c' echo "firefox" > > / .bashrc'EXPOSE 5900CMD ["x11vnc", "- forever", "- usepw", "- create"] # install firefox and vnc software based on the ubuntu parent image, and after startup Users can use firefox through port 5900 through vnc.
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.
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.