About docker -- writing Dockerfile

preface

Dockerfile is simply a file to build an image. It describes one instruction after another, and each instruction will build a new layer. When docker build is executed, docker will build layer by layer according to the pre written instructions in our Dockefile, so as to build a new image.

1, Dockerfile create image

  1. Create directory
mkdir any_name
cd any_name
  1. Write Dockerfile file
vim Dockerfile

# This is a comment 
FROM daocloud.io/library/nginx
MAINTAINER liang liang@localhost.localdomain
RUN mkdir /test

*Note: if there are instructions that fail to execute in the file, the created image name and tag All for none
  1. Build mirror
docker build -t liang/nginx:v1 .     # .  Be sure to indicate that Dockerfile is in the current directory. If the file name is not Dockerfile, use - f to specify the file

2, Dockerfile common instructions

1. Pull basic image FROM

The function specifies the base image and must be the first instruction. If it is not based on any image, it is written as follows: FROM scratch. 

Syntax: FROM <image>:<tag>   among<tag>Optional. The default is latest

2. RUN executes commands when building containers

RUN The command has two formats

Ⅰ. RUN <command>                # shell mode

Ⅱ. RUN ["executable", "param1", "param2"]     # Similar to a function call, executable can be understood as an executable file, followed by two parameters

*Note: do not write multiple lines of commands RUN,Write in a RUN Inside, the reason is Dockerfile Each instruction in establishes a layer

3,MAINTAINER

MAINTAINER <name>     # Designated author

4. Command to run when CMD container starts

Syntax:Ⅰ. CMD ["executable","param1","param2"]    # CMD [ "sh", "-c", "echo $HOME" ]

       Ⅱ. CMD ["param1","param2"]          # CMD [ "echo", "$HOME" ]

       Ⅲ. CMD command param1 param2         # shell form

*Note:ⅠandⅡThe two methods must be in double quotation marks. Never write in single quotation marks

5. ENV setting environment variables

Syntax: ENV <key>=<value> ...    # You can set more than one at a time

6. The ADD copy command is similar to scp, except that scp requires user name and password authentication, while ADD does not

Syntax:Ⅰ. ADD <src>... <dest>

       Ⅱ. ADD ["<src>",... "<dest>"]

<dest>The path can be either an absolute path in the container or a relative path relative to the working directory

<src>It can be a local file, a local compressed file, or a url

7. COPY copy command

Syntax:Ⅰ. COPY <src>... <dest>

        Ⅱ. COPY ["<src>",... "<dest>"]

8. EXPOSE the listening port to the outside

*Note: EXPOSE The container will not access the port of the host. If you want to map the container to the port of the host, you must add it when the container is started-p parameter

9. WORKDIR set working directory

Syntax: WORKDIR /path/to/workdir

yes RUN,CMD,ENTRYPOINT,COPY,ADD take effect. If it does not exist, it will be created, or it can be set multiple times.

10. VOLUME implements the mount function. You can hang local folders or folders in other containers to this container

Syntax: VOLUME ["/data"]

*Note: generally not in Dockerfile It is more commonly used in docker run When specified-v Data volume

3, Example

1. Constructing nginx image from Dockerfile

FROM centos:7.2.1511
ENV TZ=Asia/Shanghai
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8

RUN yum -y install epel*  gcc openssl openssl-devel  pcre-devel zlib-devel

ADD nginx-1.14.0.tar.gz /opt/

WORKDIR /opt/nginx-1.14.0

RUN ./configure --prefix=/opt/nginx  --http-log-path=/opt/nginx/logs/access.log --error-log-path=/opt/nginx/logs/error.log --http-client-body-temp-path=/opt/nginx/client/  --http-proxy-temp-path=/opt/nginx/proxy/  --with-http_stub_status_module --with-file-aio --with-http_flv_module --with-http_gzip_static_module --with-stream --with-threads --user=www --group=www
RUN make && make install
RUN groupadd www && useradd -g www www
WORKDIR /opt/nginx
RUN rm -rf /opt/nginx-1.14.0

ENV NGINX_HOME=/opt/nginx
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/nginx/sbin

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

*Note: This is just an example. In practice, if there is no one to meet the demand base image,as java Program use java:8 As base image,have access to Alpine Mirror as base image,Lightweight and command rich, better than centos Mirroring is more suitable base image

2. Dockerfile to build microservices

#cat Dockerfile
FROM java:8
MAINTAINER liang_20210511
#ENV TZ=Asia/Shanghai
#ENV LANG=en_US.UTF-8
#ENV LANGUAGE=en_US:en
#ENV LC_ALL=en_US.UTF-8
ENV JAVA_OPTS="-Xms512m -Xmx512m"
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ADD cmdb-server.tar /
WORKDIR /cmdb-server
EXPOSE 10013

#CMD ["java","-jar","cmdb-server.jar","-Dfile.encoding=utf-8"]
ENTRYPOINT ["/cmdb-server/entrypoint.sh"]

#cat entrypoint.sh 
#!/bin/sh
nohup java -server $JAVA_OPTS -jar cmdb-server.jar >/dev/null 2>&1 &
sleep 1
if [ $? -eq 0 ];then
tail -f logs/cmdb-server.log
fi

4, Comparison between CMD and ENTRYPOINT

The   cmd and entrypoint commands are commands used to specify the container to run when it starts. There are two modes: exec and shell. It is recommended to use exec mode, which can enable the container to receive the KILL signal and exit gracefully. This involves the daemon process of the container to manage the life cycle of the container. We will discuss it in the future.

  cmd command

The purpose of the   cmd instruction is to provide a default execution command for the container.

The   cmd instruction can be used in three ways, one of which is to provide default parameters for entrypoint:

cmd ["param1","param2"]

  the other two modes of use are exec mode and shell mode:

cmd ["executable","param1","param2"]

cmd command param1 param2

   note that the command line parameters can override the settings of the cmd instruction, but they can only be rewritten, but they cannot pass parameters to the commands in cmd through the command line. We can override the default command provided by the cmd instruction by using the docker run parameter.

  entrypoint instruction

The purpose of the   entrypoint instruction is also to specify the default task for the container.

There are two ways to use the   entrypoint instruction, exec mode and shell mode:

entrypoint ["executable", "param1", "param2"] 

entrypoint command param1 param2

  here, entrypoint has a special usage, passing parameters.

   1. When we use the exec mode, the instruction of docker run can be passed to the entrypoint command as a parameter. For example:

from centos
entrypoint [ "top", "-n" ]

When we execute docker run -- rm centos:test 1
 Commands executed in the container: top -n 1

   2. The cmd command passes parameters to the entrypoint, such as:

from centos
entrypoint [ "top", "-n" ]
cmd [ "1" ]

When we execute docker run -- rm centos:test
 Commands executed in the container: top -n -1

  3. The docker run instruction overrides the cmd instruction and passes the parameter to the entrypoint

from centos
entrypoint [ "top", "-n" ]
cmd [ "1" ]

When we execute docker run -- rm centos:test 3
 Commands executed in the container: top -n -3

  in fact, the cmd instruction and the entrypoint instruction are much more complex than these:


  of course, you can also refer to the official explanation for in-depth understanding. Compare the time: Official link

Keywords: Docker Container

Added by vandana on Wed, 29 Dec 2021 23:43:20 +0200