dockerfile编写nginx镜像

一、安装yum组件

yum install -y yum-utils

二、安装docker源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

三、查看docker源中有哪些版本

yum list docker-ce --showduplicates | sort -r
Last metadata expiration check: 5:31:16 ago on Mon 11 Oct 2021 10:09:10 AM CST.
Installed Packages
docker-ce.x86_64               3:20.10.9-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.8-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.7-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.6-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.5-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.4-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.3-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.2-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.1-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:20.10.0-3.el8                 docker-ce-stable 
docker-ce.x86_64               3:19.03.15-3.el8                docker-ce-stable 
docker-ce.x86_64               3:19.03.15-3.el8                @docker-ce-stable
docker-ce.x86_64               3:19.03.14-3.el8                docker-ce-stable 
docker-ce.x86_64               3:19.03.13-3.el8                docker-ce-stable 
Available Packages

 四、指定安装版本

yum install docker-ce-3:19.03.15-3.el8

五、配置国内镜像源

mkdir /etc/docker/

cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF

 六、启动docker并设置开机自启

systemctl enable --now docker

七、安装nginx依赖包

yum -y install gcc gcc-c++ pcre-devel openssl-devel wget

八、下载nginx软件包

wget http://nginx.org/download/nginx-1.18.0.tar.gz

九、编译安装nginx

id nginx || {(groupadd nginx; useradd -r -s /sbin/nologin -g nginx nginx )}
mkdir -p /data/software
./configure  \
--user=nginx \
--group=nginx \
--prefix=/data/software/nginx \
--sbin-path=/data/software/nginx/sbin/nginx \
--conf-path=/data/software/nginx/etc/nginx.conf \
--error-log-path=/data/software/nginx/logs/error.log  \
--http-log-path=/data/software/nginx/logs/access.log  \
--pid-path=/data/software/nginx/run/nginx.pid \
--lock-path=/data/software/nginx/run/nginx.lock  \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/data/software/nginx/tmp/client/ \
--http-proxy-temp-path=/data/software/nginx/tmp/proxy/ \
--http-fastcgi-temp-path=/data/software/nginx/tmp/fcgi/ \
--http-uwsgi-temp-path=/data/software/nginx/tmp/uwsgi \
--http-scgi-temp-path=/data/software/nginx/tmp/scgi \
--with-pcre
make
make install
mkdir /data/software/nginx/tmp

十、测试nginx启动是否有问题

/data/software/nginx/sbin/nginx -t
nginx: the configuration file /data/software/nginx/etc/nginx.conf syntax is ok
nginx: configuration file /data/software/nginx/etc/nginx.conf test is successful

十一、配置nginx文件并复制nginx

mkdir /data/software/docker

[root@centos8 15:50:25/data/software/docker]# cat zabbix.conf 
server {
    listen 80;
    server_name demo.domain.com;
    access_log /data/software/nginx/logs/demo.domain.com.log;
    index index.html index.htm index.php;
 
    location  / {
        proxy_pass http://172.17.0.5;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_redirect off;
        proxy_set_header Host demo.domain.com;
    }
}
[root@centos8 15:50:32/data/software/docker]#

[root@centos8 15:51:05/data/software/docker]# cp -rf /data/software/nginx .

十二、配置nginx守护进程 

cat > supervisord.conf <<EOF
[supervisord]
nodaemon=true
[program:crond]
command=crond -n 
[program:nginx]
command=/data/software/nginx/sbin/nginx
EOF

十三、编写dockerfile文件

vim Dockerfile
FROM  centos:latest
MAINTAINER moyuanbo@yeah.net

ADD nginx /data/software/nginx

RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    yum -y install epel-release && \
    yum -y install supervisor crontabs && \
    echo Asia/Shanghai > /etc/timezone && \
    ln -sf /data/software/nginx/sbin/nginx /bin/ && \
    echo '0 3 * * * /data/software/nginx/sbin/nginx -s reload > /dev/null 2>&1' > crontab.list && \
    groupadd nginx && \
    useradd -r -s /sbin/nologin -g nginx nginx && \
    echo 'daemon off;' >> /data/software/nginx/etc/nginx.conf && \
    crontab crontab.list
 
ADD supervisord.conf /etc/supervisord.conf

CMD ["/usr/bin/supervisord"]

十四、打包镜像:build

docker build -t nginx:v1 .

十五、启动容器

mkdir -p /data/docker/logs
docker run -d --name nginx -v /data/docker/logs:/data/software/nginx/logs -p 8082:8082 
 nginx:v1

十六、验证结果

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值