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