Dockerfile parameters Introduction

 

Dockerfile parameters Description: https: //docs.docker.com/engine/reference/builder/

Explain the meaning of Directive

FROM: FROM debian: stretch represented debian: stretch as the base image build

MAINTAINER: maintainer information
RUN: RUN can be seen with the reasons behind the fact that some shell commands, connected by && these scripts execution in a row, doing so is to reduce the number of layers of the mirror, each additional line will give mirrored RUN an additional layer, so here choose to execute all commands linked together to reduce the number of layers
ARG: will specifically explain later in the RUN command, this command can make some macro definitions, for example, I define ENV JAVA_HOME = / opt / jdk, after shell behind the RUN command $ {JAVA_HOME} will be replaced / opt / JDK
ENV: this effect can be seen that the instruction is to set some environmental variables (actually export) in the shell
FROM ... AS ...: this is Docker 17.05 and the new version out of the above instructions, in fact, to this stage of the image from the individual names: FROM ...(基础镜像) AS ...(别名)directly using an alias when referring to this stage of the mirror behind it

COPY:. (. Represents a current folder, the folder i.e. Dockerfile) the name implies, is used to copy files back and forth, COPY / root / workspace / agent shows a current folder so that files are copied to the container / root / workspace / agent folder. --From parameter may also come from the front stage by mirror copy files, such as --from = builder ratio indicates that the file is not a source of the local file system, but before builder alias container

WORKDIR: performing RUNin front of the rear shell commands will first cdinto WORKDIRthe back of the catalog

ENTRYPOINT: This parameter represents the image of the "portal", after the mirror package is complete, execute the image using the docker run command is actually executed behind this ENTRYPOINT executable file (usually a shell script file), or by [ "can execute file "," parameter 1 "and" parameter 2 "] this gives way to the execution parameters of the executable file, the" entrance "of the implementation of the working directory is the directory that behind WORKDIR

 

Each command Detailed

FROM

Designated as a new kind of mirror image of the base image, such as:

FROM ubuntu:14.04

MAINTAINER

The authors indicate mirror and its e-mail, such as:

MAINTAINER vector4wang "[email protected]"

RUN

In the new command execution inside mirror, such as to install some software, some basic configuration environment can be used \ to wrap, such as:

RUN echo 'hello docker!' \
    > /usr/local/file.txt

 

Format may be used exec RUN ["executable", "param1", "param2"]command, such as:

RUN ["apt-get","install","-y","nginx"]

Note that, executablea command, followed by the parameter param

COPY

Copy files to the host in the mirror, if the destination does not exist, Docker will automatically create all the required directory structure, but it's just a simple copy and extract and decompress files do not work. Such as:

COPY application.yml /etc/springboot/hello-service/src/resources

 

Note: You need to copy the directory must be on file in the same directory Dockerfile next
reason:

Because the build environment will be uploaded to Docker daemon, and copying is carried out in Docker daemon. Any building situated outside environment things are not available. COPY instruction of the position of the object must be an absolute path inside the container.
--- "THE DOCKER BOOK"

ADD

Copy the host's file to the mirror, like COPY, limiting conditions and usage patterns are the same, such as:

ADD application.yml /etc/springboot/hello-service/src/resources

 

However ADD Compressed files (tar, gzip, bzip2, etc) do the extraction and decompression operations.

EXPOSE

Exposure image for the host port mapping done when boot image, in terms of a random parameter -P port mirrored host port mapping doing. Use (to specify s):

EXPOSE 8080 
EXPOSE 8081
...

 

WORKDIR

When building image, specify the working directory mirroring, after the commands are based on this working directory, if does not exist, create the directory. Such as

WORKDIR /usr/local
WORKDIR webservice
RUN echo 'hello docker' > text.txt
...

 

The final will /usr/local/webservice/generate a file in the directory text.txt

ONBUILD

When a mirror is used as the command contains ONBUILD base image of the other mirror (such as the user's needs to a mirror position ready to add to the source code, or the user needs to perform the specific image build script from the build environment), the command it will be executed.
The create a mirror image-A

FROM ubuntu
...
ONBUILD ADD . /var/www
...

 

Then create a mirror image-B, as the image-A specifies the base image, such as

FROM image-A
...

 

Then, when the image-B constructed, displayed on the log follows:

Step 0 : FROM image-A
# Execting 1 build triggers
Step onbuild-0 : ADD . /var/www
...

 

USER

The specified what kind of image the user to perform, such as:

USER mongo

 

VOLUME

To add the volume-based containers for image creation. For example, you can file mongodb image data stored in the data file to specify a host. (Interior of the vessel is not recommended to store any data)
such as:

VOLUME /data/db /data/configdb

 

note:VOLUME 主机目录 容器目录

CMD

When you start a command vessel to be performed, such as:

CMD /bin/bash

 

Also you can use exec syntax, such as

CMD ["/bin/bash"]

 

When there are multiple CMD, only the last one will work.

ENTRYPOINT

Exactly the same role and usage and CMD

The difference between the CMD and ENTRYPOINT

Knock blackboard! ! ! Very important
we must pay attention!

We must pay attention!

We must pay attention!
CMD and ENTRYPOINT same as the command to execute when the container is started, the difference between the following points:

  • CMD command will be overwritten docker run the command does not ENTRYPOINT

The use CMD ["/bin/bash"]or ENTRYPOINT ["/bin/bash"]after use re- docker run -ti imagestarting container, it automatically enters the interior of the container interactive terminal, as used
docker run -ti image /bin/bash.

But if the mirror command to start docker run -ti image /bin/ps, use the back of the CMD command will be executed instead covered by bin/psthe command, and ENTRYPOINT is not, but will docker behind the run command to execute the command as a parameter ENTRYPOINT .
The following examples easier to understand
Dockerfile for

...
ENTRYPOINT ["/user/sbin/nginx"]

 

Then after by starting build container

docker run -ti image -g "daemon off"

 

At this point -g "daemon off"will be passed as a parameter to ENTRYPOINT, eventually turned into a command

/user/sbin/nginx -g "daemon off"

 

  • CMD and ENTRYPOINT exist when

When there ENTRYPOINT and CMD, CMD command parameters into a ENTRYPOINT, and this parameter CMD is provided behind the cover docker run command, such as:

...
ENTRYPOINT ["echo","hello","i am"]
CMD ["docker"]

 

After the start of construction of the container after

  • usedocker run -ti image

    Output "hello i am docker"

  • usedocker run -ti image world

    Output "hello i am world"

Command more, you can go by memory classification (such as tables beginning) way

Examples

Himself wrote a simple example, a very simple

FROM ubuntu
MAINTAINER vector4wang [email protected]

WORKDIR /usr/local/docker
ADD temp.zip ./add/
COPY temp.zip ./copy/
EXPOSE 22
RUN groupadd -r vector4wang && useradd -r -g vector4wang vector4wang
USER vector4wang

ENTRYPOINT ["/bin/bash"]

 

Here is the process of running

1.png

 
2.png

(Dynamic map can not be too large to upload)

ADD attention username and login after, COPY into files

postscript

These are just writing your own demo and browse other people by looking at the blog summed up, after some practical problems in the process of actual combat will encounter additional room.


Link: https: //www.jianshu.com/p/10ed530766af

 

 

Guess you like

Origin www.cnblogs.com/ling-yu-amen/p/10955361.html