创建一个nginx容器(提供配置文件和网页文件)
创建一个nginx容器
[root@localhost ~]# docker run -tid --name nginx centos
81cd1d33c50f836b71caf4ed4477dc93411e7fc6dd90274e9d1a3cb2f68bd2b2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81cd1d33c50f centos "/bin/bash" 4 seconds ago Up 2 seconds nginx
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@81cd1d33c50f /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@81cd1d33c50f /]# cd /usr/src/
[root@81cd1d33c50f src]# ls
debug kernels nginx-1.20.2.tar.gz
[root@81cd1d33c50f src]# tar xf nginx-1.20.2.tar.gz
[root@81cd1d33c50f src]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@81cd1d33c50f src]# ls
debug kernels nginx-1.20.2 nginx-1.20.2.tar.gz
[root@81cd1d33c50f src]# useradd -r -M -s /sbin/nologin nginx
[root@81cd1d33c50f src]# mkdir -p /var/log/nginx
[root@81cd1d33c50f src]# chown -R nginx.nginx /var/log/nginx/
[root@81cd1d33c50f src]# cd nginx-1.20.2
[root@81cd1d33c50f nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@81cd1d33c50f nginx]# make && make install
[root@81cd1d33c50f nginx-1.20.2]# cd /usr/local/nginx/
[root@81cd1d33c50f nginx]# ls
conf html logs sbin
[root@81cd1d33c50f nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@81cd1d33c50f nginx]# source /etc/profile.d/nginx.sh
[root@81cd1d33c50f nginx]# nginx
[root@81cd1d33c50f nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
[root@81cd1d33c50f nginx]# cd /
[root@81cd1d33c50f /]# vi start.sh
[root@81cd1d33c50f /]# cat start.sh
#! /bin/sh
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/bin/bash
[root@81cd1d33c50f /]# chmod +x start.sh
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81cd1d33c50f centos "/bin/bash" 17 minutes ago Up 17 minutes nginx
[root@localhost ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' 81cd1d33c50f xxkk/nginx:v4.0
sha256:373fa7d6e7fdc4cc513696b8eb71a86296e40f7bd2fae66bbd5ba6b5a81008ad
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xxkk/nginx v4.0 373fa7d6e7fd 2 minutes ago 549MB
提供配置文件和网页文件
提供配置文件
首先,创建一个数据卷容器dbdata,并在其中创建一个/container/conf挂载到/usr/local/nginx/conf:
[root@localhost ~]# cd /container/conf/
[root@localhost conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@localhost conf]# cd ..
[root@localhost container]# ls html/
50x.html index.html
[root@localhost container]# docker run -tid --name dbdata -v /container/conf/:/usr/local/nginx/conf/ busybox
5a98f66b78f2281e393f0c09bd35eb79e42f977e50c9cfaa97260fded22b7f02
提供网页文件
然后可以在其他容器中使用–volumes-from来挂载dbdata容器中的数据卷
[root@localhost container]# docker run -tid --name db1 -v /container/html/:/usr/local/nginx/html/ --volumes-from dbdata busybox
93234a1ba3c06c70aa6ef854ce6c9bfc0a9445165a65d3e33ed0e2ac4dd3d92f
创建容器
[root@localhost container]# docker run -tid --name nginx -p 80:80 --volumes-from db1 373fa7d6e7fd
65b583a16662d576475284963cf53d4a530253cc45da3beb1cd561fb3350c244
[root@localhost container]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
修改网页文件
[root@localhost ~]# cd /container/
[root@localhost container]# ls
conf html
[root@localhost container]# cd html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# mv index.html a.html
[root@localhost html]# echo 'sb' > index.html
[root@localhost html]#