1、安装docker环境

# 安装yum工具
yum -y install yum-utils
# 安装docker官方yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker及docker-compose服务
yum -y install docker-ce docker-compose
# 设置docker服务开机自启
systemctl enable docker
# 启动docker服务
systemctl start docker
# 查看docker服务启动状态
systemctl enable docker
# 出现以下信息说明docker服务启动成功(running)
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-04-25 16:13:33 CST; 36s ago
     Docs: https://docs.docker.com
 Main PID: 7251 (dockerd)
    Tasks: 7
   Memory: 32.0M
   CGroup: /system.slice/docker.service
           └─7251 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Apr 25 16:13:30 bogon dockerd[7251]: time="2022-04-25T16:13:30.475298218+08:00" level=info msg="ccResolverWrappe...e=grpc
Apr 25 16:13:30 bogon dockerd[7251]: time="2022-04-25T16:13:30.475317990+08:00" level=info msg="ClientConn switc...e=grpc
Apr 25 16:13:30 bogon dockerd[7251]: time="2022-04-25T16:13:30.544388360+08:00" level=info msg="Loading containe...tart."
Apr 25 16:13:32 bogon dockerd[7251]: time="2022-04-25T16:13:32.758458101+08:00" level=info msg="Default bridge (...dress"
Apr 25 16:13:33 bogon dockerd[7251]: time="2022-04-25T16:13:33.103100128+08:00" level=info msg="Firewalld: inter...rning"
Apr 25 16:13:33 bogon dockerd[7251]: time="2022-04-25T16:13:33.381699279+08:00" level=info msg="Loading containe...done."
Apr 25 16:13:33 bogon dockerd[7251]: time="2022-04-25T16:13:33.444238431+08:00" level=info msg="Docker daemon" c....10.14
Apr 25 16:13:33 bogon dockerd[7251]: time="2022-04-25T16:13:33.446875064+08:00" level=info msg="Daemon has compl...ation"
Apr 25 16:13:33 bogon systemd[1]: Started Docker Application Container Engine.
Apr 25 16:13:33 bogon dockerd[7251]: time="2022-04-25T16:13:33.541653668+08:00" level=info msg="API listen on /v....sock"
Hint: Some lines were ellipsized, use -l to show in full.

2、Dockerfile构建nginx

# 编写Dockerfile文件
vim Dockerfile

FROM centos:centos7
MAINTAINER Nginx Dockerfile
RUN yum -y install luajit gcc prce-devel openssl-devel zlib-devel libxm12-devel libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel perl-devel perl-ExtUtils-Embed luajit-devel
RUN cd /tmp && wget https://openresty.org/download/openresty-1.15.8.2.tar.gz && \
    tar zxmf openresty-1.15.8.2.tar.gz && cd openresty-1.15.8.2 && \
    ./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_modul \
        --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-libatomic \
        --with-pcre-jit \
        --with-stream_ssl_preread_module && \
    gmake && gmake install
ENV PATH $PATH:/usr/local/nginx/sbin
RUN ln -s /usr/local/openresty/nginx /usr/local/nginx
RUN ln -sf /dev/stdout /usr/local/nginx/logs/access.log && ln -sf /dev/stderr /usr/local/nginx/logs/error.log
EXPOSE 80
ENTRYPOINT ["nginx","-g","daemon off;"]

在Dockerfile同一级目录下执行如下构建命令

docker build -t nginx:v1.0 

出现报错:

No gmake nor make found in PATH.

解决:
在RUN 命令后添加yum -y install make

出现如下语句,则表示构建成功

Successfully built 308a2a3a8b4e
Successfully tagged nginx:v1.0

查看镜像

[root@bogon source]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
nginx        v1.0      503c027782db   5 minutes ago       746MB
centos       centos7   eeb6ee3f44bd   7 months ago        204MB

3、nginx docker服务运行

使用docker run命令运行容器

[root@bogon source]# docker run -itd --name nginx -p 80:80 -d nginx:v1.0
264abc9193d2499ed88e3bc66d63fe620118efaa375710bd79a2fd17ae428891
[root@bogon source]# docker ps -a 
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS                           PORTS                               NAMES
264abc9193d2   nginx:v1.0     "nginx -g 'daemon of…"   19 seconds ago      Up 14 seconds                    0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

使用curl本地80端口,得到如下结果,则说明部署成功

[root@bogon source]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to OpenResty!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="https://openresty.org/">openresty.org</a>.<br/>
Commercial support is available at
<a href="https://openresty.com/">openresty.com</a>.</p>

<p><em>Thank you for flying OpenResty.</em></p>
</body>
</html>

4、使用卷挂载实现配置的维护和使用

mkdir -p /opt/data/apps/nginx/
docker cp nginx:/usr/local/nginx/conf /opt/data/apps/nginx/
docker stop nginx
docker rm nginx
docker run -itd --name nginx -h nginx -p 80:80 -v /opt/data/apps/nginx/conf:/usr/local/nginx/conf -d nginx:v1.0

查看是否挂载成功,出现如下内容表示本地目录成功挂载至容器中

[root@bogon ~]# docker inspect nginx | jq -r .[].Mounts
[
  {
    "Type": "bind",
    "Source": "/opt/data/apps/nginx",
    "Destination": "/usr/local/openrestry/nginx/conf",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
  }
]

若执行上条命令请安装jq命令

yum -y install jq
Logo

更多推荐