In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the basic structure of Dockerfile". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the basic structure of Dockerfile".
# # basic structure
Oockerfile consists of one statement line after another, and supports comment lines that begin with _ _.
Usually an Dockerfile file consists of four components: basic image information, maintainer information, mirror operation instructions, and commands executed when the container starts.
For example, here is a simple example:
# basic information of image # This dockerfile uses the ubuntu image# VERSION 2-EDITION image Author: docker_user# Command format: Instruction [arguments / command].. # Base image to use This must be set as the first lineFROM ubuntu# maintainer Information # Maintainer: docker_user (@ docker_user) MAINTAINER docker_user docker_user@email.com# mirroring instructions # Commands to update the imageRUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" > > / etc/apt/sources.listRUN apt-get update & & apt-get install-y nginxRUN echo "\ ndaemon off Execute # Commands when creating a new containerCMD / usr/sbin/nginx when the etc/nginx/nginx.conf# container starts
Each Dockerifle file must first specify the basic image information, and then recommend maintainer information, which is not required.
# # instruction
# Notes:
Use "#" to comment
Example:
# Memcached## VERSION 1.The use the ubuntu base image provided by dotCloudFROM ubuntu# make sure the package repository is up to dateRUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > / etc/apt/sources.listRUN apt-get update# install memcachedRUN apt-get install-y memcached
# FROM:
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).
Example:
FROM ubuntu
If the specified image does not exist locally, Dokcer automatically downloads the specified image from the repository registration server; if you do not specify _ _ tag__, the default tag is _ _ latest__,. If there is none, an error will be reported.
# MAINTAINER:
MAINTAINER
This directive is used to specify maintainer information. Such as name, contact information and so on.
Example:
MAINTAINER Anoxia windorman@gmaillcom
# RUN:
RUN (the command is run in a shell-`/ bin/sh-c`)
Or
RUN ["executable", "param1", "param2"...] (exec form)
The RUN command is equivalent to:
Docker run image commanddocker commit container_id
Example:
RUN apt-get updateRUN apt-get-y install apache2
# CMD:
CMD ["executable", "param1", "param2"]
Use exec execution, recommended method
Or
CMD command param1 param2
Executed in / bin/sh, provided to applications that need to interact
Or
CMD ["param1", "param2"]
Default parameters provided 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.
Example:
CMD ["/ usr/sbin/apachectl", "- D", "FOREGROUND"]
# EXPOSE:
EXPOSE [...]
Tell the port number exposed by the Docker server container for use by the common interconnected system. When starting the container, the host will automatically assign a port to be forwarded to the specified port through-P _ ~ ~ Docker.
Example:
EXPOSE 11211
# ENV:
ENV
Specify an environment variable that will be used by subsequent RUN instructions and maintained while the container is running.
Example:
ENV PG_MAJOR 9.3ENV PG_VERSION 9.3.4RUN curl-SL http://example.com/postgres-$PG_VERSION.tar.xz | tar-xJC / usr/src/postgress & & … ENV PATH / usr/local/postgres-$PG_MAJOR/bin:$PATH
Create a new container using the image generated by this ENV, and you can see this environment variable through docker inspect:
Root@tankywoo-docker:~# docker inspect 49bfc7a9817f... "Env": ["name=tanky", "HOME=/", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],.
The name=tanky inside is set. You can also set or modify environment variables during docker run:
Docker run-I-t-env name= "tanky" ubuntu:newtest / bin/bash
# ADD:
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).
Example:
ADD. / scripts/start.sh / start.sh
# COPY:
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.
Example:
ADD. / scripts/start.sh / start.sh
# ENTRYPOINT:
ENTRYPOINT ["executable", "param1", "param2"]
Or:
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.
Example:
ENTRYPOINT ["echo", "Whale you be my container"]
# VOLUME:
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.
Example:
VOLUME ["/ data"]
# USER:
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 desired users before, such as
RUN groupadd-r postgres & & useradd-r-g postgres postgresUSER postgres
To obtain administrator privileges temporarily, you can use gosu instead of recommended sudo.
# WORKDIR
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.
Example:
WORKDIR / aWORKDIR bWORKDIR cRUN pwd
The final path is / a/b/c.
# ONBUILD
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.
Thank you for your reading, the above is the content of "what is the basic structure of Dockerfile". After the study of this article, I believe you have a deeper understanding of what the basic structure of Dockerfile is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.