75-CentOS-Docker构建nginx镜像


准备一台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=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值