使用Dockerfile 创建基于Ubuntu2204的Nginx镜像

Dockerfile

#基础镜像名称
FROM ubuntu:22.04

#更新并添加依赖
RUN apt update && \
apt install tar wget make build-essential libtool openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev libgd-dev libxml2-dev libxslt-dev libgeoip-dev -y

#下载并编译
RUN cd /tmp && \
wget https://nginx.org/download/nginx-1.26.1.tar.gz && \
tar zvxf nginx-1.26.1.tar.gz && \
cd nginx-1.26.1/ && \
./configure \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre-jit &&\
make && make install 

#设置启动后的环境变量
ENV PATH=$PATH:/usr/local/nginx/sbin

#开放端口
EXPOSE 80
EXPOSE 443

#启动Nginx
ENTRYPOINT ["nginx","-g", "daemon off;"]

 

创建镜像

# 创建
docker build -t nginx:v4 .
[+] Building 158.0s (7/7) FINISHED                                                                                                                                          docker:default
 => [internal] load build definition from Dockerfile                                                                                                                                  0.0s
 => => transferring dockerfile: 1.28kB                                                                                                                                                0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                                                                                                       1.6s
 => [internal] load .dockerignore                                                                                                                                                     0.0s
 => => transferring context: 2B                                                                                                                                                       0.0s
 => CACHED [1/3] FROM docker.io/library/ubuntu:22.04@sha256:340d9b015b194dc6e2a13938944e0d016e57b9679963fdeb9ce021daac430221                                                          0.0s
 => [2/3] RUN apt update && apt install tar wget make build-essential libtool openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev libgd-dev libxml2-dev libxslt-dev libgeoip-dev -y  69.7s
 => [3/3] RUN cd /tmp && wget https://nginx.org/download/nginx-1.26.1.tar.gz && tar zvxf nginx-1.26.1.tar.gz && cd nginx-1.26.1/ && ./configure --with-threads --with-file-aio --wi  81.9s 
 => exporting to image                                                                                                                                                                4.4s 
 => => exporting layers                                                                                                                                                               4.4s 
 => => writing image sha256:1a5298f576bce919effaf4159ab47d3edab7edf10cf2fabd4756feb3cd84f5cb                                                                                          0.0s 
 => => naming to docker.io/library/nginx:v4     

# 查看
docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        v4        1a5298f576bc   5 minutes ago   614MB

# 启动
docker run --name nginx -p 80:80 -d nginx:v4
0098ee99c48f6e37702acb28dede8999a89011ea0dd9dac3930816c457229362

# 查看状态
docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                               NAMES
0098ee99c48f   nginx:v4   "nginx -g 'daemon of…"   8 seconds ago   Up 8 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

 

将conf和html路径从Docker中复制到本机

mkdir docker_nginx
cd docker_nginx/

# 复制conf
docker_nginx# docker cp nginx:/usr/local/nginx/conf ~/docker_nginx/

# 复制html
docker_nginx# docker cp nginx:/usr/local/nginx/html ~/docker_nginx/

停止并删除容器

docker stop nginx
docker rm nginx

 

重新启动

docker run --name nginx -h nginx -p 80:80 -p 443:443 \
-v ~/docker_nginx/conf:/usr/local/nginx/conf \
-v ~/docker_nginx/html:/usr/local/nginx/html -d nginx:v4

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd54296bc35f nginx:v4 "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx

 

 

正文完
 0
nekoda
版权声明:本站原创文章,由 nekoda 于2024-08-06发表,共计2977字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)