Docker 基础docker学习

Dockerfile 文件介绍 (整理)

2021-07-18  本文已影响0人  yjtuuige

概述

一、基本结构:

# This dockerfile uses the ubuntu image            
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] .. 

# Base image to use, this must be set as the first line
FROM ubuntu        # 基础镜像信息

# Maintainer: docker_user <docker_user at email.com> (@docker_user)
MAINTAINER docker_user docker_user@email.com        # 维护者信息

# Commands to update the image            # 镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# Commands when creating a new container
CMD /usr/sbin/nginx        # 容器启动时执行指令
# Nginx
#
# VERSION               0.0.1

FROM      ubuntu
MAINTAINER Victor Vieux <victor@docker.com>

RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server

# Firefox over VNC
#
# VERSION               0.3

FROM ubuntu

# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir /.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'

EXPOSE 5900
CMD    ["x11vnc", "-forever", "-usepw", "-create"]

# Multiple images example
#
# VERSION               0.1

FROM ubuntu
RUN echo foo > bar
# Will output something like ===> 907ad6c2736f

FROM ubuntu
RUN echo moo > oink
# Will output something like ===> 695d7793cbe4

# You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
# /oink.

二、指令

  1. FROM               # 基础镜像,一切从这里开始构建
  2. MAINTAINER         # 镜像作者:姓名+邮箱
  3. RUN                # 镜像构建的时候需要运行的命令
  4. ADD                # 步骤。(tomcat镜像的压缩包就是一种添加内容)
  5. WORKDIR            # 镜像的工作目录
  6. VOLUME             # 挂载的目录
  7. EXPOSE             # 暴露端口配置
  8. CMD                # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代 (替换)
  9. ENTRYPOINT         # 指定这个容器启动的时候要运行的命令,可以直接追加命令 (追加)
  10. ONBUILD           # 当构建一个被继承 DockerFile ,这个时候就会运行 ONBUILD 的指令,是一种触发指令
  11. COPY              # 类似ADD命令,将我们的文件拷贝到镜像中
  12. ENV               # 构建的时候设置环境变量
2.1 FROM
2.2 MAINTAINER
2.3 RUN
2.4 CMD
CMD ["executable","param1","param2"]    # 使用 exec 执行,推荐方式;
CMD command param1 param2    # 在 /bin/sh 中执行,提供给需要交互的应用;
CMD ["param1","param2"]     # 提供给 ENTRYPOINT 的默认参数;

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

CMD 会在启动容器的时候执行,build 时不执行,而 RUN 只是在构建镜像的时候执行,后续镜像构建完成之后,启动容器就与 RUN 无关了,这个初学者容易弄混这个概念,这里简单注解一下。

2.5 EXPOSE
2.6 ENV
ENV <key> <value>       # 只能设置一个变量
ENV <key>=<value> ...   # 允许一次设置多个变量
ENV myName="John Doe" myDog=Rex\ The\ Dog \
    myCat=fluffy
# 等同于
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy
ENV PG_MAJOR 9.3
ENV PG_VERSION 9.3.4
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
2.7 ADD
ADD hom* /mydir/        # adds all files starting with "hom"
ADD hom?.txt /mydir/    # ? is replaced with any single character
2.8 COPY
2.9 ENTRYPOINT
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2(shell中执行)。
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
FROM ubuntu
ENTRYPOINT exec top -b
FROM ubuntu
ENTRYPOINT top -b
CMD --ignored-param1 # --ignored-param2 ... --ignored-param3 ... 依此类推
2.10 VOLUME
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
2.11 USER
RUN groupadd -r postgres && useradd -r -g postgres postgres
2.12 WORKDIR
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
ENV DIRPATH /path
ENV DIRNAME test
WORKDIR $DIRPATH/$DIRNAME
2.13 ONBUILD
[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]
FROM image-A

#Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src

3、创建镜像

[root@VM-0-6-centos ~]# docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])
[root@VM-0-6-centos ~]# docker build -t nginx:v3 .        # 未尾的 . 不能省略

注:最后的 .代表本次执行的上下文路径,不能省略

  1. 补充
RUN apt-get update && apt-get install -y \
  bzr \
  cvs \
  git \
  mercurial \
  subversion
上一篇 下一篇

猜你喜欢

热点阅读