准备一台CentOS7-1908虚拟机
虚拟机中没有docker下载源
[root@C7-Docker ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@C7-Docker ~]# yum install docker-ce
国内下载国外的镜像有时候会很慢,因此可以更改 docker 配置文件添加一个加 速器,可以通过加速器达到加速下载镜像的目的
浏览器打开 http://cr.console.aliyun.com 注册或登录阿里云账号,点击左侧的 镜像加速器,将会得到一个专属
的加速地址
1
推荐使用 nsenter 命令进入容器,该命令需要通过容器的 PID 进入到容器内部,比较麻烦。
不过可以使用docker inspect 命令获取到容器的 PID。
安装 nsenter 工具,包名(不区分CentOS/Ubuntu):util-linux
[root@C7-Docker ~]# yum install -y util-linux
[root@C7-Docker ~]# systemctl start docker
镜像的构建与使用
基于Dockerfile构建编译版nginx镜像
[root@C7-Docker ~]# docker pull centos:7.7.1908
[root@C7-Docker ~]# mkdir -p /opt/dockerfile/nginx
[root@C7-Docker ~]# vim /opt/dockerfile/nginx/Dockerfile
# Docekr image for compile installation of nginx
#
FROM centos:7.7.1908
LABEL maintainer="dushan <[email protected]>"
ENV password 123456
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.16.1.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.16.1 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make -j 4 && make install
RUN cd /usr/local/nginx
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 "Hello dockerfile ..." > /usr/local/nginx/html/index.html
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
[root@C7-Docker ~]# yum -y install nginx &> /dev/null
[root@C7-Docker ~]# cp /etc/nginx/nginx.conf /opt/dockerfile/nginx/
[root@C7-Docker ~]# ll /opt/dockerfile/nginx/
total 1020
-rw-r--r-- 1 root root 811 Jun 11 17:39 Dockerfile
-rw-r--r-- 1 root root 1032630 Jun 11 17:41 nginx-1.16.1.tar.gz
-rw-r--r-- 1 root root 2471 Jun 11 17:43 nginx.conf
[root@C7-Docker ~]# cd /opt/dockerfile/nginx/
[root@C7-Docker nginx]# docker build -t nginx:compiled_V1 .
[root@C7-Docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx compiled_V1 d1966b9ed396 2 minutes ago 535MB
centos 7.7.1908 08d05d1d5859 7 months ago 204MB
使用ARG声明变量,在构建时指定变量值
[root@C7-Docker nginx]# vim Dockerfile
# Docekr image for compile installation of nginx
#
#FROM centos:7.7.1908
ARG base_version
FROM ${base_version}
LABEL maintainer=