Docker 3.2.6:以 Alpine 为基础镜像制作 Nginx 镜像

3.2.6:以 Alpine 为基础镜像制作 Nginx 镜像

准备基础镜像

以前面制作号的 alpine-init:3.12 作为基础镜像;

更改 Dockerfile

将基础镜像设为制作好的 alpine-init:3.12,并更改添加组和用户的命令,其它指令不用更改:

root@ubuntu:~# cd /Dockerfile/Services/nginx/
root@ubuntu:/Dockerfile/Services/nginx# vim Dockerfile
# Nginx Dockerfile
#
FROM alpine-init:3.12
LABEL maintainer="yqc<20251839@qq.com>"

ADD nginx-1.16.1.tar.gz /usr/local/src
ADD nginx.conf /apps/nginx/conf/nginx.conf
RUN cd /usr/local/src/nginx-1.16.1 \
    && ./configure --prefix=/apps/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 \
    && addgroup -g 2000 -S www \
    && adduser -u 2000 -S -D -G www www \
    && mkdir -p /data/nginx/html \
    && chown -R www:www /apps/nginx \
    && ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx \
    && echo "nginx page(Dockerfile build based on alpine-init:3.12)" > /data/nginx/html/index.html

EXPOSE 80 443

CMD nginx -g "daemon off;"

addgroup 命令帮助:

/ # addgroup --help
BusyBox v1.32.1 () multi-call binary.

Usage: addgroup [-g GID] [-S] [USER] GROUP

Add a group or add a user to a group

        -g GID  Group id
        -S      Create a system group

adduser 命令帮助:

/ # adduser --help
BusyBox v1.32.1 () multi-call binary.

Usage: adduser [OPTIONS] USER [GROUP]

Create new user, or add USER to GROUP

        -h DIR          Home directory
        -g GECOS        GECOS field
        -s SHELL        Login shell
        -G GRP          Group
        -S              Create a system user
        -D              Don't assign a password
        -H              Don't create home directory
        -u UID          User id
        -k SKEL         Skeleton directory (/etc/skel)
更改镜像构建脚本

改一下镜像名称:

root@ubuntu:/Dockerfile/Services/nginx# vim docker-build.sh 
#!/bin/bash
docker build -t alpine-nginx:1.16.1 .
构建镜像
root@ubuntu:/Dockerfile/Services/nginx# bash docker-build.sh 
验证镜像

查看镜像:

root@ubuntu:~# docker images -f reference=alpine-nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine-nginx        1.16.1              99ce226de3e1        2 minutes ago       238MB

基于此镜像启动容器,访问验证:

root@ubuntu:/Dockerfile/Services/nginx# docker run -itd -p 80:80 alpine-nginx:1.16.1

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值