个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/9328763.html
Nginx
Dockerfile 内容如下:
FROM centos
RUN yum install -y
ncurses
ncurses-devel
zlib
zlib-devel
openssl
openssl-devel
pcre
pcre-devel
libxml2
libxml2-dev
libxslt
libxslt-devel
make
gcc
gcc-c++
autoconf
&& yum clean all ;
groupadd -g 1002 www
&& useradd -u 1002 -g www -s /sbin/nologin www
RUN curl --silent -o /root/nginx-1.14.0.tar.gz http://nginx.org/download/nginx-1.14.0.tar.gz
&& tar xf /root/nginx-1.14.0.tar.gz
&& cd nginx-1.14.0
&& ./configure
--user=www
--group=www
--prefix=/usr/local/nginx
--with-http_stub_status_module
--with-http_ssl_module
--with-http_secure_link_module
&& make
&& make install ;
cd ..
&& rm -rf nginx-1.14.0 /root/nginx-1.14.0.tar.gz /usr/local/nginx/conf ;
echo "/usr/local/nginx/sbin/nginx -c /data/conf/nginx/nginx.conf -t && kill -HUP \`cat /data/logs/nginx.pid\`" > /root/nginx_reload ;
chmod 700 /root/nginx_reload
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;","-c","/data/conf/nginx/nginx.conf"]
本地目录规划
/data/dokcer/docker_local_volume/nginx_test1_80/
├── conf
│ ├── deny_file.conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── fcgi.conf
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── nginx_status.conf
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── ssl_example.conf
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ ├── vhost
│ │ └── temp.conf
│ └── win-utf
├── logs
└── web
└── webclose
通过volume,将日志数据落地到本地,也可以直接在本地修改发布web代码,并使用本地配置文件。
这里注意:有容器内的进程采用redis账号运行,考虑到权限问题,统一设置为uid:gid 1002:1002,本地文件也同样授权
制作镜像:
[root@docker-machine_192.168.31.129 docker]# docker build -t volumes/my_nginx_server:v1.14.0 .
启动容器:
[root@Docker_Machine_192.168.31.130 nginx]# docker run -tid --name=nginx_test1_80 -v /data/dokcer/docker_local_volume/nginx_test1_80/conf/:/data/conf/nginx/:ro -v /data/dokcer/docker_local_volume/nginx_test1_80/web/:/data/web/ -v /data/dokcer/docker_local_volume/nginx_test1_80/logs/:/data/logs/ volumes/my_nginx_server:v1.14.0
085298fc89927068212755e94b9f5cb2f6e1553bef6edb8f105fa71a2d15e31f
可以用docker exec 进容器看看:
[root@docker-machine_192.168.31.129 docker]# docker exec -it 085298fc8992 /bin/bsh
[root@085298fc8992 /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 45932 3300 ? Ss+ 03:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off; -c /data/conf/nginx/nginx.c
www 6 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 7 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 8 0.0 0.0 46068 1872 ? S+ 03:12 0:00 nginx: worker process
www 9 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 10 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 11 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 12 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 13 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 14 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
www 15 0.0 0.0 46068 1628 ? S+ 03:12 0:00 nginx: worker process
redis
Dockerfile 内容如下:
FROM centos
ADD ["redis-3.2.7.tar.gz","/dist/src/"]
#Redis用户组统一uid:gid为1001:1001
RUN yum -y install tcl-8.5* make gcc gcc-c++
&& yum clean all ;
groupadd -g 1001 redis
&& useradd -u 1001 -g redis -s /sbin/nologin -d /usr/local/redis redis ;
mkdir -p /data/redis /usr/local/redis/bin/
&& chown redis:redis /data/redis -R ;
cd /dist/src/redis-3.2.7/ ;
make MALLOC=libc
&& cd src
&& cp redis-benchmark redis-check-aof redis-cli redis-sentinel redis-server /usr/local/redis/bin/
&& ln -sv /usr/local/redis/bin/* /usr/local/bin/ ;
chown redis:redis -R /usr/local/redis ;
cd /usr/local/redis
&& rm -rf /dist
EXPOSE 6001
#以Redis用户跑Redis进程
USER redis
CMD ["/usr/local/redis/bin/redis-server","/usr/local/redis/etc/redis6001.conf"]
USER 指令可以让镜像中的程序与指定的用户跑,
镜像构建的每一层,都要及时清除多余的中间文件,让镜像尽可能的精简
本地目录规划如下:
└── redis_test1_6001
├── data
└── etc
└── redis6001.conf
通过volume,将数据落地到本地,并使用本地配置文件。
这里注意:有容器内的进程采用redis账号运行,考虑到权限问题,统一设置为uid:gid 1001:1001,本地文件也同样授权
启动:
docker run -tid -v /data/dokcer/docker_local_volume/redis_test1_6001/data/:/data/redis/ -v /data/dokcer/docker_local_volume/redis_test1_6001/etc/:/usr/local/redis/etc/:ro --name=redis_test1_6001 my_redis_server:v3.2.7
其他,略。