dockerfile developing curricula and relevant principles

Dockerfile written tutorials

Most of the contents of this article comes from: https: //yeasy.gitbooks.io/docker_practice/image/build.html

  1. Create a directory and into the directory file created Dockerfile

    vim /root/test/Dockerfile
  2. You need to install the image file

    Here we installed the latest version of centos dependent and python3.7.4 and installation required, open port 8000 port

    • Primary writing
    FROM    centos:latest
    MAINTAINER  ryan.liu "[email protected]"
    
    RUN     yum install -y gcc zlib-devel openssl-devel libffi-devel make
    WORKDIR /tmp
    RUN     curl -O https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
    RUN     tar -zxvf Python-3.7.4.tgz
    
    WORKDIR Python-3.7.4
    RUN     mkdir -p /usr/local/python3
    RUN     ./configure --prefix=/usr/local/python3
    RUN     make && make install
    RUN     echo export PATH="/usr/local/python3/bin:$PATH" >> ~/.bashrc
    RUN     source ~/.bashrc
    RUN     /usr/local/python3/bin/pip3 install Django==2.2.3
    RUN     /usr/local/python3/bin/pip3 install pymysql
    WORKDIR /
    EXPOSE  8000

    Each command will be established in one layer Dockerfile, RUNis no exception. Every RUNact, just the process we have just manually create mirror the same: to establish a new layer, on which these commands are executed, after the execution commitof this layer changes constitute a new image.

    The wording of the above, the mirror layer 7 is created. This is completely pointless, and does not require a lot of running things, they have been put into a mirror image, such as compiler environment, updated packages, and so on. The result is a very bloated, very multi-layered mirror, not only increases the build time to deploy, and very prone to error. This is a mistake many novice Docker people often make.

    Union FS is the maximum limit of the number of layers, such as AUFS, once the largest not more than 42 layers, now is not more than 127 layers.

    • Correct wording
    FROM    centos:latest
    MAINTAINER  ryan.liu "[email protected]"
    
    RUN yum install -y gcc zlib-devel openssl-devel libffi-devel make \
         && cd /tmp \
         && curl -O https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz \
        && tar -zxvf Python-3.7.4.tgz \
        && rm -rf Python-3.7.4.tgz \
        && mkdir -p /usr/local/python3 \
        && cd Python-3.7.4 \
        && ./configure --prefix=/usr/local/python3 \
        && make && make install \
        && echo export PATH="/usr/local/python3/bin:$PATH" >> ~/.bashrc \
        && source ~/.bashrc \
        && /usr/local/python3/bin/pip3 install Django==2.2.3 \
        && /usr/local/python3/bin/pip3 install pymysql \
        && cd /
    EXPOSE  8000
  3. carried out

    In Dockerfileexecution directory

    docker build -t centos7:latest .

    Mirror building context (Context)

    If you pay attention, you will see docker buildthe last command a .. .It represents the current directory, but Dockerfilein the current directory, so many beginners think that this is the path specified in Dockerfilethe path, so understanding is actually inaccurate. If the command corresponding to the above format, you may find that this is in the specified context path . So what is the context it?

    First we have to understand the docker buildworks. Docker Docker divided into at runtime engine (that is, the server daemon) and client tools. Docker engine provides a set of the REST API, is called Docker the Remote API , and if dockera command such as client tool, is set by this API Docker interact with the engine, and thus perform various functions. Therefore, although on the surface it seems we are in the machine to perform a variety of dockerfunctions, but in fact, everything is in the form of long-distance calls used to complete the service side (Docker engine). Because of this C / S is designed to allow us to operate the remote server Docker engines a breeze.

    When we build a mirror, and not all will be customized by the RUNinstruction is completed, often you need some local files copied into a mirror, for example, by COPYinstruction, ADDinstruction and so on. The docker buildcommand to build the mirror, it is not built locally, but in the service side, that is, Docker engine build. So in this client / server architecture, how to get the server to get local file it?

    This introduces the concept of context. When built, the user specifies the context of the path to build the mirror, docker buildcommand that this path, all the contents of the package will be the path, and then uploaded to the Docker engine. So after getting this context Docker engine package, expand the building will get all necessary papers image.

    If Dockerfileso write in:

    COPY ./package.json /app/

    This is not to copy execution docker builddirectory where the command package.json, nor is copied Dockerfileunder the directory package.json, but replication context (context) under the directory package.json.

    Thus, COPYthe path of such instructions in the source files are relative path . Often ask why this is a beginner COPY ../package.json /appor COPY /opt/xxxx /appreasons can not work, because these paths beyond the scope of context, Docker engine can not file these locations available. If you really need those files, you should copy them into the context of the directory.

    Now you can understand just order docker build -t nginx:v3 .in this ., in fact, in the context of the specified directory, docker buildthe command will pack the contents of the directory to Docker engine to help build a mirror.

    If you look at docker buildthe output, in fact, we've seen this sends a context of process:

    $ docker build -t nginx:v3 .
    Sending build context to Docker daemon 2.048 kB
    ...

    Building context for understanding mirrored building is very important, should not avoid making some of the mistakes. For example, some beginners find COPY /opt/xxxx /appafter not working, so simply be Dockerfileplaced in the root directory of the hard disk to build, and found that docker buildafter the implementation of, something to send a few dozen GB of very slow and very easy to build fails. That is because this practice is letting docker buildpackage an entire hard disk, which is obviously the wrong.

    In general, it would be Dockerfileplaced in an empty directory, or the project root directory. If the required files in that directory, you should copy the required paper over. If the directory does not want to pass something Docker engine build, you can .gitignorewrite a different syntax .dockerignore, the file is used to weed out uninteresting as passing the context to Docker engine.

    So why would anyone mistake .is to specify Dockerfilethe directory where it? This is because by default, if you do not specify additional Dockerfilewords, will be called in the context of directory Dockerfilefiles as Dockerfile.

    This is just the default behavior, in fact, Dockerfilethe file name is not required to Dockerfile, and does not require must be in the context of the directory, for example, can be used -f ../Dockerfile.phpto specify a file as a parameter Dockerfile.

    Of course, generally it habitually uses the default file name Dockerfile, and will be placed in the context of the mirrored building directory.

  4. View Mirror

    docker images
  5. Run container

    docker run -it --name=centos7 centos7:latest /bin/bash

Other related commands

  • Stop running container

    docker stop centos7
  • To delete a stopped container

    docker rm centos7
  • View all downloaded image

    docker images

Guess you like

Origin www.cnblogs.com/liuhuan086/p/11223290.html