Dockerfile usage details

There are two ways to make a Docker image: one is to use a Docker container, build the container directly, and then export it as an image for use; the other is to use a Dockerfile, write all actions in a file, and then build it into an image. Dockerfile is very flexible and recommended.

1. Dockerfile basic structure

Generally, a Dockerfile is divided into four parts: basic image information, maintainer information, image operation instructions, and execution instructions when the container starts. '#' is a comment in the Dockerfile. Let's take a look at the following small example:

# This my first nginx Dockerfile
# Version 1.0

# Base images 基础镜像
FROM centos

#MAINTAINER 维护者信息
MAINTAINER tianfeiyu 

#ENV 设置环境变量
ENV PATH /usr/local/nginx/sbin:$PATH

#ADD  文件放在当前目录下,拷过去会自动解压
ADD nginx-1.8.0.tar.gz /usr/local/  
ADD epel-release-latest-7.noarch.rpm /usr/local/  

#RUN 执行以下命令 
RUN rpm -ivh /usr/local/epel-release-latest-7.noarch.rpm
RUN yum install -y wget lftp gcc gcc-c++ make openssl-devel pcre-devel pcre && yum clean all
RUN useradd -s /sbin/nologin -M www

#WORKDIR 相当于cd
WORKDIR /usr/local/nginx-1.8.0 

RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-pcre && make && make install

RUN echo "daemon off;" >> /etc/nginx.conf

#EXPOSE 映射端口
EXPOSE 80

#CMD 运行以下命令
CMD ["nginx"]

FROM : Specify the base image, in which image to build

The format is FROM <image> 或FROM <image>:<tag> 。

The first instruction must be a FROM instruction.

MAINTAINER: Specify maintainer information

The format is MAINTAINER <name>

RUN: command to be executed in the mirror

The format is RUN <command> 或 RUN ["executable", "param1", "param2"]

The former will run the command in the shell terminal, ie /bin/bash -c; the latter will be executed using exec. Specifying the use of other terminals can be achieved in the second way, eg  RUN [“/bin/bash”, “-c”,”echo hello”] .

WORKDIR: Specifies the current working directory, equivalent to cd

The format is WORKDIR /path/to/workdir

Configure the working directory for subsequent RUN , CMD , ENTRYPOINT instructions.
Multiple WORKDIR directives can be used, and subsequent commands, if the argument is a relative path, will be based on the path specified by the previous command. E.g

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

Then the final path is /a/b/c.

EXPOSE: Specify the port to be opened by the container

The format is EXPOSE <port> [<port>...]

Tell the Docker server the port number exposed by the container for use by the interconnected system. You need to pass -P when starting the container, and the Docker host will automatically assign a port forwarding to the specified port.

ENV: define environment variables

The format is  ENV <key> <value> . Specify an environment variable that will be used by subsequent RUN commands and persisted when the container is running.
E.g

ENV PATH /usr/local/nginx/sbin:$PATH

COPY : Copy the local host's (relative path to the directory where the Dockerfile is located) to the container's

The format is COPY.

ADD: Equivalent to COPY, but more powerful than COPY

The format is ADD <src> <dest>

该命令将复制指定的 到容器中的 。 其中 可以是Dockerfile所在目录的一个相对路径;也可以是一个 URL;还可以是一个 tar 文件,复制进容器会自动解压。

VOLUME:挂载目录

格式为VOLUME ["/data"]

创建一个可以从本地主机或其他容器挂载的挂载点,一般用来存放数据库和需要保持的数据等。

USER

格式为 USER daemon

指定运行容器时的用户名或 UID,后续的 RUN 也会使用指定用户。当服务不需要管理员权限时,可以通过该命令指定运行用户。并且可以在之前创建所需要的用户,例如: RUN useradd -s /sbin/nologin -M www。

ENTRYPOINT

两种格式:

ENTRYPOINT ["executable", "param1", "param2"]

ENTRYPOINT command param1 param2 (shell中执行)

配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖。每个 Dockerfile 中只能有一个 ENTRYPOINT ,当指定多个时,只有最后一个起效。

CMD

支持三种格式

CMD ["executable","param1","param2"] 使用 exec 执行,推荐方式;
CMD command param1 param2 在 /bin/bash 中执行,提供给需要交互的应用;
CMD ["param1","param2"] 提供给 ENTRYPOINT 的默认参数;

指定启动容器时执行的命令,每个 Dockerfile 只能有一条 CMD 命令。如果指定了多条命令,只有最后一条会被执行。如果用户启动容器时候指定了运行的命令,则会覆盖掉 CMD 指定的命令。

ONBUILD:在构建本镜像时不生效,在基于此镜像构建镜像时生效

格式为 ONBUILD [INSTRUCTION]

配置当所创建的镜像作为其它新创建镜像的基础镜像时,所执行的操作指令。

ENTRYPOINT 和 CMD 的区别:ENTRYPOINT 指定了该镜像启动时的入口,CMD 则指定了容器启动时的命令,当两者共用时,完整的启动命令像是 ENTRYPOINT + CMD 这样。使用 ENTRYPOINT 的好处是在我们启动镜像就像是启动了一个可执行程序,在 CMD 上仅需要指定参数;另外在我们需要自定义 CMD 时不容易出错。

使用 CMD 的 Dockerfile:

[root@sta2 test]# cat Dockerfile 
FROM mysql

CMD ["echo","test"]

使用 ENTRYPOINT 的 Dockerfile:

[root@sta2 entrypoint]#  cat  Dockerfile 
FROM mysql

ENTRYPOINT ["echo","test"]

结论:ENTRYPOINT 不能覆盖掉执行时的参数,CMD 可以掉覆盖默认的参数。

可以使用以下命令覆盖默认的参数,方便调试 Dockerfile 中的 bug:
[root@sta2 entrypoint]# docker run -it --entrypoint=/bin/bash feiyu/entrypoint:1

二.创建镜像

构建镜像时,需要将要使用的包及 Dockerfile 文件放在一个目录中,像下面这样:

使用以下命令来构建一个镜像:
# docker build -t feiyu/nginx:1.8 .
Docker 镜像采用分层的技术,所以创建过程中每一个命令都相当于一层:

# docker run -d -p 8080:80 --name mynginx feiyu/nginx:1.8 #启动容器


转注:https://blog.csdn.net/mozf881/article/details/55798811

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933731&siteId=291194637