美文网首页Docker lnmpDockerDocker compose
Docker基于CentOS创建nginx镜像

Docker基于CentOS创建nginx镜像

作者: suxl | 来源:发表于2019-07-24 21:11 被阅读0次

      本来想着直接用docker pull nginx下载nginx镜像,但由于项目中使用fastdfs作为文件服务器,需要在nginx中添加fastdfs-nginx-module模块,所以就使用默认安装的方式创建nginx镜像。

1.下载CentOS镜像

  docker pull centos

  [root@file nginx]# docker images

  REPOSITORY                      TAG                IMAGE ID            CREATED            SIZE

  centos                          latest              9f38484d220f        4 months ago        202MB

2.使用centos镜像启动容器

  docker run -itd --name nginx centos:centos7 /bin/bash

3.进入nginx容器

  docker exec -it 69a3991a5fb6 /bin/bash    --69a3991a5fb6为容器id

4.在容器中安装nginx

  安装环境:yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

  下载nginx:wget -c https://nginx.org/download/nginx-1.12.1.tar.gz

  解压:tar -zxvf nginx-1.12.1.tar.gz

  进入目录:cd nginx-1.12.1

  使用默认配置:./configure

  编译:make

  安装:make install

  设置nginx以非daemon启动:echo "daemon off;">>/usr/local/nginx/conf/nginx.conf

5.编写nginx启动脚本

  cd /usr/local/sbin

  vi run.sh

  添加内容

  #!/bin/bash

  /usr/local/nginx/sbin/nginx

6.改变脚本权限,使其可以运行:

  chmod 755 run.sh

exit退出容器

7.根据容器id生成镜像:

  docker commit -a "suxlin" -m "nginx images" 1b212636e9b6 nginx:latest  --1b212636e9b6容器id

[root@file nginx]# docker images

  REPOSITORY                      TAG                IMAGE ID            CREATED            SIZE

  nginx                          latest              b2d8591975ac        About an hour ago  435MB

8.通过nginx镜像启动容器

  docker run -di --name nginx -p 8080:80 nginx /usr/local/sbin/run.sh

相关文章

网友评论

    本文标题:Docker基于CentOS创建nginx镜像

    本文链接:https://www.haomeiwen.com/subject/mkafrctx.html