docker可以支持把一个宿主机上的目录挂载到镜像里。docker容器启动的时候,如果要挂载宿主机的一个目录,可以用-v参数指定。比如启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过以下方式指定:docker run -it -v /test:/soft centos /bin/bash
这样在容器启动后,容器内会自动创建/soft的目录。本文将介绍Docker中运行nginx并挂载本地目录到镜像中的方法
Nginx配置文件路径
(关键)查看nginx镜像里面配置文件、日志等文件的具体位置,只有找到镜像配置文件的路径,后面挂载文件和文件夹才能覆盖这些路径
[root@www ~]# docker run -itd nginx
09dffcad3d3d48c9d9e26ee551bb549f655c38f2c656fe9bab333bd8f56ffbc7
[root@www ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09dffcad3d3d nginx "/docker-entrypoint.?? 3 seconds ago Up 2 seconds 80/tcp
#找到镜像中nginx.conf配置文件路径/etc/nginx/nginx.conf
[root@www ~]# docker exec -it 09dffcad3d3d ls -l /etc/nginx/nginx.conf
-rw-r--r-- 1 root root 643 Jul 7 15:52 /etc/nginx/nginx.conf
#找到default.conf配置文件的路径/etc/nginx/conf.d/default.conf
[root@www ~]# docker exec -it 09dffcad3d3d ls -l /etc/nginx/conf.d/default.conf
-rw-r--r-- 1 root root 1114 Sep 14 13:21 /etc/nginx/conf.d/default.conf
#找到默认首页文件夹html路径/usr/share/nginx/html
[root@www ~]# docker exec -it 09dffcad3d3d ls -l /usr/share/nginx/html
total 8
-rw-r--r-- 1 root root 494 Jul 7 15:52 50x.html
-rw-r--r-- 1 root root 612 Jul 7 15:52 index.html
#找到日志文件路径/var/log/nginx
[root@www ~]# docker exec -it 09dffcad3d3d ls -l /var/log/nginx
total 0
lrwxrwxrwx 1 root root 11 Jul 10 20:26 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jul 10 20:26 error.log -> /dev/stderr
创建目录和配置文件
这里说明一下为什么我要挂载配置文件和文件夹,如果你部署应用并且很轻易地修改nginx的配置文件,如果挂载了文件或者文件夹那么你只需要修改挂载源的文件或者文件夹里面的文件就可以了,而不用每次都要使用docker run -i -t nginx /bin/bash命令进入到镜像终端中去修改配置文件,下面我将演示修改自己的nginx首页,并且将其挂载上去容器中覆盖掉原来的默认的首页
在linux系统中创建挂载源文件和文件夹(我的是centos7)
mkdir -p /data/nginx/conf
mkdir -p /data/nginx/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs
然后创建在conf文件夹里面创建一个nginx.conf配置文件,并且输入一下内容,建议大家不要照抄我的配置,用我上面介绍的第一步的方法进入到nginx容器的终端中复制nginx.conf配置文件的内容到linux系统中这个新创建的nginx.conf文件中进行修改,这样子就保证了配置文件中的路径与镜像中配置文件的路径能保持一致。
[root@www ~]# cd /data/nginx/conf
[root@www conf]# cat nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
在conf.d里面创建一个default.conf文件,并且输入一下内容,同样这个内容也是我从镜像中default.conf默认的配置文件中复制过来的。
[root@www ~]# cd /data/nginx/conf.d/
[root@www conf.d]# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
修改index.html内容
[root@www ~]# echo "this is docker nginx" > /data/nginx/html/index.html
测试结果
[root@www ~]# docker run --name nginx -d -p 88:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/logs:/var/log/nginx nginx
760ebc9e63c19c3e06f0aeffac8621ef180ccc95fddcd85877fde96b543f34b7
[root@www ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
760ebc9e63c1 nginx "/docker-entrypoint.?? 4 seconds ago Up 3 seconds 0.0.0.0:88->80/tcp nginx
看看目录有没有挂载
[root@www ~]# ll /data/nginx/logs
total 8
-rw-r--r-- 1 root root 431 Sep 14 21:42 access.log
-rw-r--r-- 1 root root 267 Sep 14 21:42 error.log
[root@www ~]# docker exec -it 760ebc9e63c1 cat /usr/share/nginx/html/index.html
this is docker nginx
访问一下
[root@www ~]# curl 192.168.179.100:88
this is docker nginx