Dockerfile operation

Reference Address: https://blog.csdn.net/deng624796905/article/details/86493330

 

What is Dockerfile

We can go to use  Dockerfile the definition of a mirror, mirror rely on to run the container, you can go to simulate a real loophole scene. Therefore, there is no doubt that  Dockerfile is the key to the mirror and container, and  Dockerfile also very easy to define the image content, he said so much, so  Dockerfile in the end is what is it?

Dockerfile Is built automatically  docker mirrored configuration files, users can  Dockerfile quickly create a custom image. Dockerfile The command is very similar  linux in  shell command.

FIG painting below we can intuitively feel Docker relationship between the lower mirror, the three containers and Dockerfile.

We can see from the image above,  Dockerfile you can customize the image by  Docker the command to run a mirror, so as to achieve the purpose of starting the container.

Dockerfile It is composed of a command statement line by line, and support have  # comment lines that begin with.

In general, we can be  Dockerfile divided into four parts:

  • Base image (parent image) instruction information FROM
  • Maintainer information, instruction MAINTAINER
  • An operation instruction image  RUN ,  EVN ,  ADD and  WORKDIR the like
  • Containers start command  CMD ,  ENTRYPOINT and  USER the like

Here is a simple example of Dockerfile:

  1.  
    FROM python:2.7
  2.  
    MAINTAINER Angel_Kitty <angelkitty6698@gmail.com>
  3.  
    COPY . /app
  4.  
    WORKDIR /app
  5.  
    RUN pip install -r requirements.txt
  6.  
    EXPOSE 5000
  7.  
    ENTRYPOINT [ "python"]
  8.  
    CMD [ "app.py"]

We can analyze the above process:

  • 1, from  Docker Hub the  pull lower  python 2.7 base of the mirror
  • 2, the display information defenders
  • 3, copy the current directory into the container  /app copy of the local host directory  <src> (  Dockerfile relative path of the directory) to the container <dest>
  • 4, specify the directory to /app
  • 5, the installation dependencies
  • 6, the exposed  5000 port
  • 7. Start app

This example is a start  python flask app of  Dockerfile (  flask is  python a lightweight  web framework), I believe we can slightly from this example to understand the composition and the preparation process of the instruction Dockfile.

Dockerfile commonly used commands

According to the above example, we already know almost as well as the composition of the process of writing instruction Dockerfile, we come to understand how these commonly used commands will be handy.

Because  Dockerfile all commands are in the following format: INSTRUCTION argument instructions  (INSTRUCTION) are not case sensitive, but it is recommended capital and sql statement is not very similar to it? Here we explain these to formal instruction set it.

FROM

FROM It is used to specify the basis of  images the general format  FROM <image> or  FORM <image>:<tag> , all  Dockerfile are using this to  FROM begin with, FROM specified in the order  Dockerfile created a mirror image file on what basis, FROM all instruction will in future  FROM be created on the basis of a mirror. The same can  Dockerfile be used multiple times in  FROM order to create multiple images. For example, we want to specify  python 2.7 the base image, we can be like the following wording:

FROM python:2.7

MAINTAINER

MAINTAINER is used to specify the image creator and Information, the general format  MAINTAINER <name> . Here I set my  ID and email:

MAINTAINER Angel_Kitty <angelkitty6698@gmail.com>

COPY

COPY Is the local host for replication  <src> (Dockerfile directory located relative path) into the container  <dest>.

When using a local directory as the source directory is recommended  COPY . The general format is  COPY <src><dest> . For example, we want to copy the current directory to the container  /app directory, we can do this:

COPY . /app

WORKDIR

WORKDIR Used in conjunction with  RUN, CMD, ENTRYPOINT command sets the current working directory. May be provided a plurality of times, if the path is relative, the former is relatively  WORKDIR command. The default path is /. The general format is  WORKDIR /path/to/work/dir . For example, we set the /app path, we can proceed as follows:

WORKDIR /app

RUN

RUN Run for the inner container. Each  RUN command is equivalent to adding the original image on the basis of a level change, the original image will not change. The general format is  RUN <command> . For example, we want to install  python dependencies, our approach is as follows:

RUN pip install -r requirements.txt

EXPOSE

EXPOSE Command to specify the port opening. The general format is EXPOSE <port> [<port>...]

Such as the example above, open port 5000:

EXPOSE 5000

ENTRYPOINT

ENTRYPOINT It can make you act like a container as an executable program. Dockerfile can have only one  ENTRYPOINT, if there is more, the last one will work.

ENTRYPOINT Command is also available in two formats:

  • ENTRYPOINT ["executable", "param1", "param2"] : Recommended  execform
  • ENTRYPOINT command param1 param2 : shell Form

The following example, we want to  python mirror becomes executable program, we can do so:

ENTRYPOINT ["python"]

CMD

CMD Command default command to execute when starting container CMD command can contain executable files may not contain executable files. Without that contains executable files necessary to use  ENTRYPOINT designated a, then  CMD the parameters will command as a ENTRYPOINTparameter.

CMD Command has three formats:

  • CMD ["executable","param1","param2"]: Recommended  exec form.
  • CMD ["param1","param2"]: No executable form
  • CMD command param1 param2: Shell form.

Dockerfile can have only one CMD, if there is more, the last one will work. And  CMD the  shell form of the default calling  /bin/sh -c execute the command.

CMD Command is  Docker the command-line parameters passed to cover: docker run busybox /bin/echo Hello Docker put  CMD in command of coverage.

For example, we want to start  /app , we can use the following command to achieve:

CMD ["app.py"]

Of course, there are some other commands, when we used to go to explain one by one look.

Construction of Dockerfile

We have put Dockerfile general wording about completed, we can begin to write your own examples:

  1.  
    mkdir static_web
  2.  
    cd static_web
  3.  
    touch Dockerfile
  4.  
    Vi Dockerfile then begin editing the file
  5.  
    Enter i start editing
  6.  
     
  7.  
    The following is the content we build Dockerfile
  8.  
    ``````````
  9.  
    FROM nginx
  10.  
    MAINTAINER Angel_Kitty <[email protected]>
  11.  
    RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
  12.  
    ``````````
  13.  
     
  14.  
    After editing Press esc to exit edit
  15.  
    Then: wq write quit

We  Dockerfile perform file directory:

docker build -t angelkitty/nginx_web:v1 .

We explain,  -t is to set the name for the new warehouse and a mirror, which  angelkitty is the repository name,  nginx_web as the image name,  :v1the label (do not add to the default  latest )

After we build is complete, use the  docker images command to view all the mirrors, if there  REPOSITORY was  nginx , and  TAG is  v1 information, it means successfully constructed.

Next, use the  docker run command to start a container

docker run --name nginx_web -d -p 8080:80   angelkitty/nginx_web:v1

这条命令会用 nginx 镜像启动一个容器,命名为 nginx_web ,并且映射了 8080 端口,这样我们可以用浏览器去访问这个 nginx 服务器:http://localhost:8080/ 或者 http://本机的IP地址:8080/,页面返回信息:

hello-docker

这样一个简单使用 Dockerfile 构建镜像,运行容器的示例就完成了!

Guess you like

Origin www.cnblogs.com/peng-lan/p/11237094.html