Write Dockerfile to build Docker image

What is DockerFile

Dockerfile is a text file used to build a mirror, and the text content contains instructions and instructions for building a mirror.

Write DockerFile

1. First install the docker service

sudo apt install docker.io 

2. Write DockerFile
Create a file named Dockerfile in the directory to define the building rules of the Docker image.
The following is an example to illustrate the basic syntax of Dockerfile:

# 设置基础镜像, 以这个镜像为基础构建新的镜像
# FROM ubuntu:16.04 基于ubuntu:16.04  FROM centos:7 As build  基于Centos7
FROM ubuntu:latest

# AS build 是给基础镜像取了一个别名build, 方便在后续调用
FROM ubuntu:16.04 AS build

# 拷贝镜像文件到当前镜像中的指定路径
FROM existing-image:tag
COPY --from=existing-image:tag /path/to/source/file /path/to/destination/file

# 添加本地文件或目录到构建的镜像中
# 这里添加的是一个镜像源文件
ADD sources.list /etc/apt/

# 指定镜像需要需要映射出来的端口号  
# docker run -P 时,会自动随机映射EXPOSE的端口
EXPOSE 443/tcp
EXPOSE 10000/udp

# 在镜像中指令命令,这里安装了依赖库 
# Dockerfile的指令每执行一次都会在docker上新建一层
# 以&&符号组合命令, 可以降低构建的层数
RUN apt-get update && apt-get install -y build-essential libssl-dev

# 修改时区, 确保docker镜像内的时间和宿主机器保持一致
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 通过源码在镜像中安装依赖库
RUN cd /opt \
    && git clone https://code.videolan.org/videolan/x264.git \
    && cd x264 \
    && git checkout -b stable origin/stable \
    && git pull --rebase \
    && ./configure --enable-pic --enable-shared --disable-asm \
    && make -j8 \ 
    && make install \
    && export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

# 在容器中添加环境变量
ENV PATH /usr/local/bin:$PATH
ENV LANG C.UTF-8
ENV TERM xterm
ENV PYTHON_VERSION 3.5.3
ENV name1=ping name2=on_ip

# 启动的时候调用对应的环境变量
CMD $name1 $name2    

# WORKDIR 指令可以来指定工作目录(或者称为当前目录)
# WORKDIR 指令使用的相对路径,那么所切换的路径与之前的WORKDIR有关
WORKDIR /opt/media/ZLMediaKit/build

# COPY将文件从构建环境拷贝到镜像中
COPY package.json /usr/src/app/

# COPY从另一个镜像中拷贝数据到当前镜像
COPY --from=build /opt/media/Release/MediaServer /opt/media/bin/MediaServer

# CMD 容器启动命令/CMD 指令的格式和 RUN 相似
# CMD 指令就是用于指定默认的容器主进程的启动命令
CMD app argument1  argument2 argument3 


# USER指令和WORKDIR相似, 都是改变环境状态并影响以后的层 
# WORKDIR 是改变工作目录,USER 则是改变之后层的执行RUN, CMD 以及ENTRYPOINT这类命令的身份

# LABEL 指令用来给镜像以键值对的形式添加一些元数据(metadata)
LABEL <key>=<value> <key>=<value> <key>=<value> ...

# ARG 构建参数
# ARG 设置的环境变量仅对Dockerfile内有效, 构建好的镜像内不存在此环境变量
# 构建命令 docker build 中可以用 --build-arg <参数名>=<值> 来指定
ARG DOCKER_USERNAME=library
RUN set -x ; echo ${DOCKER_USERNAME}


# 用于指定某个程序或者指令来监控docker容器服务的运行状态
HEALTHCHECK [选项] CMD <命令>

# 以服务启动某个程序
RUN service redis-server start

# 修改镜像源的地址
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

# 通过VOLUME指令指定了两个挂载点 /data1 和 /data2
# 通过VOLUME指令创建的挂载点, 无法指定主机上对应的目录, 是自动生成的
VOLUME ["/data1","/data2"]

Build and run the image

Change to the project directory containing the Dockerfile and execute the following command to build the Docker image

# 指令最后一个 . 是上下文路径
# 如果未说明最后一个参数,那么默认上下文路径就是 Dockerfile 所在的位置
# 上下文路径下不要放无用的文件, 因为会一起打包发送给docker引擎,如果文件过多会造成过程缓慢
docker build -t your-image-name .

# 查看镜像是否创建成功
docker images

run mirror

docker run -it your-image-name

Modify the mirror source

In many cases, it will be slow to install some dependent libraries in Docker using the official mirror source. At this time, we can modify the mirror source to domestic: domestic
mirror source file sources.list

deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-proposed main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb-src http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe

Just copy the corresponding file to the corresponding location in the image in DockerFile.

#DockerFile
# 添加镜像源到镜像
ADD sources.list /etc/apt/

Guess you like

Origin blog.csdn.net/yang1fei2/article/details/132081351