Use Dockerfile create a mirror

Brief introduction

  Dockerfile is a text configuration file format, you can use Dockerfile quickly create a custom image.
  This paper describes the basic structure of a typical Dockerfile and support of numerous instructions, and explain the specific Dockerfile to write custom mirrored by these instructions.
  Finally, we will introduce the use Dockerfile creation process image.

basic structure

  Dockerfile statements by the command line by line, and supports comment lines beginning with #.
  Generally, Dockerfile divided into four parts: the base image information, perform a maintainer information, instructions and image containers start command. E.g:

# The first line must be specified based on the base image of 
the FROM Ubuntu 

# maintainer information 
MAINTAINER docker_user [email protected] 

operation instruction # mirroring 
the RUN echo " the deb http://archive.ubuntu.com/ubuntu/ raring to main Universe " >> / etc / APT / sources.list 
RUN APT - GET Update && APT- GET install - the y-nginx 
RUN echo " \ ndaemon OFF; " >> / etc / nginx / nginx.conf 

# container executes a command to start 
CMD / usr / sbin / nginx

  Wherein, must specify a start image name is based, will be described next generally maintainer information.
  It is a mirror behind the operation instruction, instruction RUN e.g., image will follow the instruction RUN command. Each run a RUN command, add a new layer mirror and submit. Finally CMD command to specify the operation command when running the container.

instruction

  The general format of the instruction INSTRUCTION arguments, including the instruction FROM, MAINTAINER, RUN like. The following were introduced.

FROM

  Format FROM <image> or FROM <image>: <tag> .
  Must first instruction FROM instruction. Further, when creating a plurality of mirrors in the same Dockerfile may be used a plurality FROM instruction (once per image).

MAINTAINER

  Format MAINTAINER <name>, specify maintainer information.

RUN

  Format RUN <command> or RUN [ "executable", "param1 ", "param2"].
  The former will be run in a shell command terminal, i.e., / bin / sh -c; the latter is performed using the exec. Other terminal may specify a second manner, e.g. RUN [ "/ bin / bash" , "- c", "echo hel1o"].
  Each RUN command to execute the specified command on the basis of the current mirror, and submit a new image. When the command is longer can use \ to wrap.

CMD

  It supports three formats:

  1. CMD [ "executable", "param1", "param2"] use exec execution, the recommended way.
  2. CMD command paraml param2 executed / bin / sh, the application needs to provide interaction.
  3. CMD [ "param1", "param2"] to provide the default parameters ENTRYPOINT.

  Commands specified to run at startup containers, each Dockerfile only a CMD command. If you specify multiple commands, only the last one will be executed.
  If the user starts when the container is specified to run the command, CMD specified command will be overwritten.

EXPOSE

  Format EXPOSE <port> [<port> ...].
  For example:
  EXPOSE 2280 8443
  told the server port number Docker containers exposed, interconnect systems for use. Need -P, Docker host will automatically assign a port forwarded to the specified port when the vessel; use -p, you can specify which local port mapping over.

ENV

  Format ENV <key> <value>. Specifies an environment variable, subsequent RUN command is used, and the container is maintained at runtime. For example:
  ENV MAJOR the PG 9.3
  ENV the PG VERSION 9.3.4
  the RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz the tar -xJC the I / usr /
the src / && ... from postgress
  ENV the PATH / usr / local / postgres- $ PG_MAJOR / bin: SPATH

ADD

  Format ADD <src> <dest>.
  This command copies the specified <src> into the container <dest>. Wherein <src> may be a relative path of the directory Dockerfile (file or directory); can also be a the URL; tar may also be a document (automatically extracting the directory).

COPY

  Format COPY <src> <dest>.
  Copy the local host <src> (as a relative path, the directory of the file or directory is located Dockerfile) of container <dest>. When the target path does not exist, it is created automatically.
  When using a local directory as the source directory, it is recommended to use COPY.

ENTRYPOINT

  There are two formats:
  EntryPoint [ "Executable", "paraml", "param2"]
  EntryPoint Command paraml param2 (performed in the shell).
  Configuration of the container after start command, and is not covered by the parameter supplied docker run.
  Each Dockerfile can have only one ENTRYPOINT, when specifying multiple ENTRYPOINT, only the last one will work.

VOLUME

  Format VOLUME [ "/ data"].
  Create a local host can mount from the mount point or other containers, generally used to store databases and data needs to be retained.

USER

  Format USER daemon.
  User name or UID run at specified container, subsequent RUN will use the specified user.
  When the service does not require administrator privileges, you can specify a user to run this command. And you can create a user needs before, for example: RUN groupadd -r postgres && useradd -r -g postgres postgres. To temporarily obtain administrator privileges can use gosu, not recommended sudo.

WORKDIR

  Format WORKDIR / path / to / workdir.
  For subsequent RUN, CMD, ENTRYPOINT instructions to configure the working directory.
  WORKDIR plurality of instructions may be used, if the subsequent command parameter is a relative path, before the command will be specified based on the path. For example:
  the WORKDIR / A
  the WORKDIR B
  the WORKDIR C
  the RUN pwd
  the final path / a / b / c.

ONBUILD

  Format ONBUILD [INSTRUCTION].
  When configured as a mirror image created by another operation instruction to create a new base image mirrored performed. For example, Dockerfile creates a mirrored image-A using the following content.
  [...]
  ONBUILD ADD./app/src
  ONBUILD RUN / usr / 1ocal / bin / Python-Build - dir / App / SRE
  [...]
  If you create a new image based on image-A, the new Dockerfile when using FROM image-a specifies the base image, is performed automatically ONBUILD instruction contents, it is equivalent to adding two instructions later.
  Image-A the EROM
  #Automatically RUN The following
  ADD./app/src
  the RUN / usr / local / bin / Python-Build - the dir / App / the src
  using ONBUILD instruction image, indicated in the label recommended, e.g. ruby: 1.9 -onbuild.

Create a mirror

  After their completion Dockerfile, you can create mirrored by docker build command.
  The basic form is Dockerfile docker build [option] path, this command will read the specified path (includes subdirectories), and all content sent to the server at the Docker path, to create a mirrored by the server. It is generally recommended to place an empty directory Dockerfile directory.
  In addition, you can make directories and files in the path Docker ignored by .dockerignore file (each line to add a matching pattern).
  To specify the label information mirrored by the -t option.
  For example, to specify the path where Dockerfile / tmp / docker_builder /, and generates a desired image label build_repo / first_image, can use the following commands:

  & sudo docker build -t build_repo/first_image /tmp/docker_builder/

 

This article is my record Docker learning, content reference from "Docker technology introduction and practical"

 

Guess you like

Origin www.cnblogs.com/gavin-guo/p/11442742.html