Dockerfile written tutorials
Most of the contents of this article comes from: https: //yeasy.gitbooks.io/docker_practice/image/build.html
Create a directory and into the directory file created Dockerfile
vim /root/test/Dockerfile
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,
RUN
is no exception. EveryRUN
act, just the process we have just manually create mirror the same: to establish a new layer, on which these commands are executed, after the executioncommit
of 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
carried out
In
Dockerfile
execution directorydocker build -t centos7:latest .
Mirror building context (Context)
If you pay attention, you will see
docker build
the last command a.
..
It represents the current directory, butDockerfile
in the current directory, so many beginners think that this is the path specified inDockerfile
the 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 build
works. 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 ifdocker
a 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 ofdocker
functions, 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
RUN
instruction is completed, often you need some local files copied into a mirror, for example, byCOPY
instruction,ADD
instruction and so on. Thedocker build
command 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 build
command 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
Dockerfile
so write in:COPY ./package.json /app/
This is not to copy execution
docker build
directory where the commandpackage.json
, nor is copiedDockerfile
under the directorypackage.json
, but replication context (context) under the directorypackage.json
.Thus,
COPY
the path of such instructions in the source files are relative path . Often ask why this is a beginnerCOPY ../package.json /app
orCOPY /opt/xxxx /app
reasons 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 build
the command will pack the contents of the directory to Docker engine to help build a mirror.If you look at
docker build
the 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 /app
after not working, so simply beDockerfile
placed in the root directory of the hard disk to build, and found thatdocker build
after 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 lettingdocker build
package an entire hard disk, which is obviously the wrong.In general, it would be
Dockerfile
placed 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.gitignore
write 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 specifyDockerfile
the directory where it? This is because by default, if you do not specify additionalDockerfile
words, will be called in the context of directoryDockerfile
files as Dockerfile.This is just the default behavior, in fact,
Dockerfile
the file name is not required toDockerfile
, and does not require must be in the context of the directory, for example, can be used-f ../Dockerfile.php
to specify a file as a parameterDockerfile
.Of course, generally it habitually uses the default file name
Dockerfile
, and will be placed in the context of the mirrored building directory.View Mirror
docker images
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