Dockerfile基于alpine 制作nginx1.18.0镜像

Alpine Linux是基于musl libc和busybox的面向安全的轻量级Linux发行版。有些命令用法和包名和centos/ubuntu 之类的不一样

包管理工具apk

apk add 包名 #安装 xx包

apk del 包名 #卸载 xx包

apk update 更新源

apk 的源

使用阿里云

a. 编辑 /etc/apk/repositories
b. 将里面 dl-cdn.alpinelinux.org 的 改成 mirrors.aliyun.com ; 保存退出即可

寻找apk 的包名

在阿里云的镜像地址中搜索相关的包名即可

http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community

添加用户

使用adduser 而不是useradd

1.首先在本地目录下创建一个测试页,如下:

vim index.html

<h1> nginx test !!! </h1>

2.设置alpine apk的源,创建repositories文件,如下:

vim repositories

http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community

3.编写Dockerfile

FROM alpine:latest
MAINTAINER yang <yang@163.com>
ENV NG_VERSION nginx-1.18.0
COPY repositories /etc/apk/repositories
RUN apk update && apk add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools wget openssl openssl-dev
ADD http://nginx.org/download/$NG_VERSION.tar.gz /usr/local/
WORKDIR /usr/local/nginx-1.18.0
RUN adduser -D -s /sbin/nologin nginx \
&& cd /usr/local/ \
&& tar xzvf $NG_VERSION.tar.gz -C /usr/local/ \
&& cd /usr/local/$NG_VERSION \
&& ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module \
&& make && make install \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ADD index.html /usr/local/nginx/html
VOLUME /mnt/acs_mnt/nas/nasfiles/kb
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80/tcp
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]

 4.构建nginx镜像,如下:

docker build -t nginx:v101 .

 

5.查看制作好的镜像

docker images

6.启动一个容器

dokcer run -it -d --name nginx -p 8989:80 nginx:v101

 7.使用启动容器的主机IP+映射端口,访问测试:

测试之前要看主机的防火墙是否开放了端口:

永久开放8989端口:firewall-cmd --zone=public --add-port=8989/tcp --permanent

使  其  生  效:firewall-cmd --reload

查看端口是否开启:firewall-cmd --list-port

 

posted @ 2021-01-06 22:41  西瓜君~  阅读(1076)  评论(0编辑  收藏  举报