在生产环境中,往往都会使用官方的系统镜像在加上一些环境的依赖制作出一个环境的基础镜像,后面在把相关的代码放到制作的基础镜像中,从而完一个项目镜像的构建工作,一般制作好的基础镜像后很少去修改,除非需要添加一些新的依赖项才会去修改一下基础镜像。
1、Dockerfile介绍
Dockerfile是一种可以被docker程序读取指令的一个脚本,是有一条条Dockerfile的指令加命令组成的,docker程序在将这些Dockerfile文件里的命令解析成真正的Linux命令。Dockerfile可以分为四部分:基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令。
指令 | 说明 |
---|---|
FROM | 设置镜像使用的基础镜像 |
MAINTAINER | 设置镜像的作者 |
RUN | 编译镜像是运行shell命令,是要非交互式的命令 |
CMD | 设置容器的启动命令 |
LABEL | 设置镜像的标签 |
EXPOSE | 声明要把容器的哪些端口暴露出来 |
ENV | 设置容器的环境变量 |
ADD | 编译镜像时复制到镜像中,这个可以复制压缩文件会自动解压,但是不会自动解压zip的压缩包,可以解压tar包 |
COPY | 编译镜像时复制到镜像中,这个指令就只是复制 |
ENTRYPOINT | 指定容器启动时的命令或脚本,如果和CMD同时使用,会将CMD的命令当做参数传递给ENTRYPOINT后面的脚本 |
VOLUME | 设置容器的挂载卷 |
USER | 指定执行后续命令的用户和用户组,这边只是切换后续命令执行的用户(用户和用户组必须提前已经存在) |
WORKDIR | 指定工作目录 |
ARG | 设置编译镜像时加入的参数 |
ONBUILD | 设置镜像的ONBUILD指令 |
STOPSIGNAL | 设置容器的退出信号量 |
2、Docker镜像分层
利用Dockerfile文件来制作时,可以使用一步到位的方式,将官方的镜像和最后的业务镜像整理到一起,也是可以利用分层的技术来制作镜像,建议大家使用分层制作镜像。
2.1、为什么要镜像分层
2.1.1、直接使用官方镜像在构建业务镜像
如果在生产中使用官方镜像直接构建成业务镜像的话,到后面会出现比较严重的弊端:
- 工作量大:每次构建业务镜像时会涉及到操作会很多,会感觉到很繁琐;
- 后续镜像需要变动是需要重新建镜像:一旦后期镜像需要改动的话,是要重新开始要一步步构建镜像,所耗费的时间成本比较大。
2.1.2、镜像分层后的优势
- 基本上每个软件都是基于某个镜像去运行的,因此一旦某个底层环境出了问题,就不需要去修改全部基于该镜像的软件的镜像,只需要修改底层环境的镜像。
- 这个好处也是最大好处,就是可以共享资源,其他相同环境的软件镜像都共同去享用同一个环境镜像,而不需要每个软件镜像要去创建一个底层环境。
- 可以复用,节省磁盘空间,相同的内容只需加载一份到内存。 修改dockerfile之后,再次构建速度快
2.2、镜像分层原理
镜像分层的说明:
- 在制作镜像时,我们可以通过docker官方的ubuntu、centos、alpine等系统镜像来作底层来启动容器,并进行一些系统的初始化工作,来实现基本的系统基础镜像;
- 可以在已经初始化工作的基础镜像上,后面我们会安装一些基础服务来部署业务需要的环境,会构成基础镜像上的第二层、第三层等等镜像;
- 通过一层层的镜像的叠加后,最终形成符合企业中业务需要的业务基础镜像,后面的话最上面一层镜像只需添加或者修改具体业务要使用的代码即可。
3、使用Dockerfile创建nginx镜像
3.1、制作底层基础镜像
3.1.1、创建好Dockerfile文件的存放目录
创建这些目录是为了后面的操作更方便,也对Dockerfile文件的管理方面也方便挺多
root@node2:~# mkdir -pv /data/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{ubuntu,centos,alpine}}
mkdir: created directory '/data'
mkdir: created directory '/data/dockerfile'
mkdir: created directory '/data/dockerfile/web'
mkdir: created directory '/data/dockerfile/web/nginx'
mkdir: created directory '/data/dockerfile/web/tomcat'
mkdir: created directory '/data/dockerfile/web/jdk'
mkdir: created directory '/data/dockerfile/web/apache'
mkdir: created directory '/data/dockerfile/system'
mkdir: created directory '/data/dockerfile/system/ubuntu'
mkdir: created directory '/data/dockerfile/system/centos'
mkdir: created directory '/data/dockerfile/system/alpine'
3.1.2、下载镜像
底层镜像的话是可以使用官方的ubuntu、alpine、centos等等一些镜像,具体还是得看业务的需求,这里我就是使用ubuntu:18.04的镜像了
root@node2:~# docker pull ubuntu:18.04
root@node2:~# docker images #有些镜像是之前下载的
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 7 months ago 141MB
alpine latest c059bfaa849c 8 months ago 5.59MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
centos 7.9.2009 eeb6ee3f44bd 10 months ago 204MB
3.1.3、编写Dockerfile文件制作底层镜像并构建镜像
注意: 构建镜像时会在执行命令所处的目录寻找Dockerfile文件,因此构建是要进都相应的目录去创建Dockerfile文件,而且Dockerfile文件名只能是Dockerfile或者dockerfile,不过一般都是写的Dockerfile这个名字。
root@node2:~# cd /data/dockerfile/system/ubuntu/
root@node2:/data/dockerfile/system/ubuntu# vim Dockerfile
# My Ubuntu Base images ubuntu:18.04
#
FROM ubuntu:18.04
MAINTAINER "Stars.Zhang eternallywm@qq.com"
ADD sources.list /etc/apt/sources.list
RUN apt update && apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev vim lrzsz tree iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common openssh-server iotop unzip zip
root@node2:/data/dockerfile/system/ubuntu# cat sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
root@node2:/data/dockerfile/system/ubuntu# cat ubuntu1804-build-command.sh
#!/bin/bash
#
TAG=$1
docker build -t harbor.stars.org/zg-test/ubuntu-base:${TAG} .
root@node2:/data/dockerfile/system/ubuntu# chmod +x ubuntu1804-build-command.sh
root@node2:/data/dockerfile/system/ubuntu# ls
Dockerfile sources.list ubuntu1804-build-command.sh
root@node2:/data/dockerfile/system/ubuntu# ./ubuntu1804-build-command.sh v1
3.1.4、验证镜像是否正常
root@node2:/data/dockerfile/system/ubuntu# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.stars.org/zg-test/ubuntu-base v1 ff7be0679685 About a minute ago 426MB
nginx latest 605c77e624dd 7 months ago 141MB
alpine latest c059bfaa849c 8 months ago 5.59MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
centos 7.9.2009 eeb6ee3f44bd 10 months ago 204MB
root@node2:/data/dockerfile/system/ubuntu# docker run -it --rm harbor.stars.org/zg-test/ubuntu-base:v1 bash
root@37b48839b663:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@37b48839b663:/# gcc
gcc gcc-7 gcc-ar gcc-ar-7 gcc-nm gcc-nm-7 gcc-ranlib gcc-ranlib-7
root@37b48839b663:/# vim a.txt
root@37b48839b663:/# vi
vi view vigr vim vim.basic vimdiff vimtutor vipw
root@37b48839b663:/# tree
.dockerenv boot/ etc/ lib/ media/ opt/ root/ sbin/ sys/ usr/
bin/ dev/ home/ lib64/ mnt/ proc/ run/ srv/ tmp/ var/
root@37b48839b663:/# exit
exit
3.2、制作nginx镜像
3.2.1、准备nginx的源码包及配置文件
root@node2:/data/dockerfile/system/ubuntu# cd /data/dockerfile/web/nginx/
root@node2:/data/dockerfile/web/nginx# ls
root@node2:/data/dockerfile/web/nginx# mkdir nginx-1.18.0 && cd $_
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# wget https://nginx.org/download/nginx-1.18.0.tar.gz
--2022-07-30 16:39:45-- https://nginx.org/download/nginx-1.18.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1039530 (1015K) [application/octet-stream]
Saving to: ‘nginx-1.18.0.tar.gz’
nginx-1.18.0.tar.gz 100%[===================================================================================================================>] 1015K 771KB/s in 1.3s
2022-07-30 16:39:48 (771 KB/s) - ‘nginx-1.18.0.tar.gz’ saved [1039530/1039530]
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# ls
nginx-1.18.0.tar.gz
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# vim nginx.conf
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# grep -Ev "^$|^\s*#" nginx.conf #^$是匹配空行,^\s*#与原来的^#相比,增加了使用\s*来匹配#前面可能会出现的空格。
user nginx;
worker_processes auto;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens off;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.2.2、编写Dockerfile文件
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# vim Dockerfile
#My Nginx-Dockerfile from nginx-1.18.0
#
FROM harbor.stars.org/zg-test/ubuntu-base:v1
MAINTAINER "Stars.Zhang eternallywm@qq.com"
ADD nginx-1.18.0.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
RUN useradd nginx -s /sbin/nologin
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
RUN echo "The is Docker test page" > /usr/local/nginx/html/index.html
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# vim nginx-1.18.0-build-command.sh
#!/bin/bash
#
TAG=$1
docker build -t harbor.stars.org/zg-test/nginx-1.18.0-base:${TAG} .
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# chmod +x nginx-1.18.0-build-command.sh
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# ls
Dockerfile nginx-1.18.0-build-command.sh nginx-1.18.0.tar.gz nginx.conf
3.2.3、构建镜像并验证镜像的可用性
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.stars.org/zg-test/nginx-1.18.0-base v1 30a83f4f2a50 49 seconds ago 464MB
harbor.stars.org/zg-test/ubuntu-base v1 ff7be0679685 58 minutes ago 426MB
nginx latest 605c77e624dd 7 months ago 141MB
alpine latest c059bfaa849c 8 months ago 5.59MB
ubuntu 18.04 5a214d77f5d7 10 months ago 63.1MB
centos 7.9.2009 eeb6ee3f44bd 10 months ago 204MB
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# docker run -d --name test-nginx -p 8080:80 harbor.stars.org/zg-test/nginx-1.18.0-base:v1
607493aea8f81be1f34d10b1e5367acae4bfb9112d17abc50056c8d3a1417508
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
607493aea8f8 harbor.stars.org/zg-test/nginx-1.18.0-base:v1 "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 443/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp test-nginx
root@node2:/data/dockerfile/web/nginx/nginx-1.18.0# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 13122 root 4u IPv4 86167 0t0 TCP *:http-alt (LISTEN)
docker-pr 13129 root 4u IPv6 86175 0t0 TCP *:http-alt (LISTEN)