docker学习案例:用docker部署nginx服务


docker实际案例之在centOS7.4上用docker部署nginx服务
[简书地址]](https://www.jianshu.com/p/1aa87bf713d5)

一、使用docker run命令直接启动一个nginx容器

直接上结果
运行脚本:
1、下载最新版nginx的docker镜像

$ docker pull nginx

2、启动容器

docker run \
--name centos_nginx \
-d -p 80:80 \
-v /usr/local/nginx/html:/usr/share/nginx/html \
-v /usr/local/nginx/logs:/var/log/nginx \
-v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /usr/local/nginx/conf/conf.d:/etc/nginx/conf.d \
nginx

这里有几个注意事项:
(1)第一个“-v”,是项目位置,把项目放到挂载到的目录下即可;
(2)第二个“-v”,是挂载的主配置文件”nginx.conf”,注意”nginx.conf”文件内有一行”include /etc/nginx/conf.d/*.conf;”,这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
(3)第三个“-v”,把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致
(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录

再看宿主机配置:

[root@dev nginx]# ll
total 12
drwxr-xr-x 3 root root 4096 Jul 19 11:15 conf
drwxr-xr-x 3 root root 4096 Jul 19 10:34 html
drwxr-xr-x 2 root root 4096 Jul 19 10:41 logs
[root@dev nginx]# 

/usr/local/nginx/conf/nginx.conf

user  root;
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;
    autoindex  on;
    #gzip  on;  
    client_max_body_size         100M;
    client_header_buffer_size     128k; 
    large_client_header_buffers  4  128k;  


    include /etc/nginx/conf.d/*.conf;
 
}

/usr/local/nginx/conf/conf.d/default.conf

server {  
    listen       80;  
    server_name  localhost;  
  
    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        root   /usr/share/nginx/html/default;  
        index  index.html index.htm;  
        autoindex  on;  
        #try_files $uri /index/index/page.html;  
    }  
  
    #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;  
    #}  
}

/usr/local/nginx/html/default/index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx! This is a demo!</title>
<style>
    body &#123;
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    &#125;
</style>
</head>
<body>
<h1>****************************</h1>
<h1 align="center" style="color:red;" >Welcome to nginx! </h1>
<h1>****************************</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
 
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
 
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

/usr/local/nginx/logs/access.log

194.105.136.181 - - [19/Jul/2018:04:29:59 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" "-"
94.102.52.77 - - [19/Jul/2018:04:41:30 +0000] "GET / HTTP/1.1" 200 742 "-" "libwww-perl/6.34" "-"
187.74.100.192 - - [19/Jul/2018:04:59:21 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" "-"
106.11.225.139 - - [19/Jul/2018:05:17:41 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
103.3.76.194 - - [19/Jul/2018:05:33:48 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" "-"
106.11.226.243 - - [19/Jul/2018:05:42:05 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1" "-"
177.137.62.62 - - [19/Jul/2018:06:18:32 +0000] "GET / HTTP/1.1" 200 742 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" "-"

启动效果:
default nginx


二、 使用docker file启动nginx容器

doeker常用命令:
docker file

1、编写docker file

在任意目录创建一个nginx.Dockerfile 文件,写入如下内容:

# Base images 基础镜像
FROM nginx 
MAINTAINER marvin
ENV RUN_USER nginx 
ENV RUN_GROUP nginx 
ENV DATA_DIR /usr/share/nginx/html
ENV LOG_DIR /var/log/nginx
RUN mkdir /var/log/nginx -p 
RUN chown nginx.nginx -R /var/log/nginx
ADD html  /usr/share/nginx/html
ADD logs  /var/log/nginx
ADD conf/nginx.conf /etc/nginx/nginx.conf
ADD conf/conf.d /etc/nginx/conf.d
EXPOSE 80
ENTRYPOINT nginx -g "daemon off;"

做了这么几件事:

1、拉取一个nginx镜像。
2、设置了几个变量。
3、创建了几个需要的目录。
4、把当前目录下的web程序复制到镜像的/data/web目录。
5、把nginx.conf配置文件和default.conf配置文件复制到镜像中。
6、设置一个默认端口。
7、最后设置了容器启动时执行的命令,我用来启动nginx程序,注意这个命令不能错,不然容器启动不了。这样设置后,当你docker run运行此镜像时不需要在后面再次执行需要执行的命令了。
8、ADD后面src的路径不要是绝对路径,最好是dockerfile和这些添加的目录放在一个目录下,否则build时会提示lstat usr/local/nginx/html: no such file or directory的错误,别问为什么,经验之谈!

2、用dockerfile 构建出镜nginx镜像
[root@dev nginx]# ls
conf  dockerfile  html  logs
[root@dev nginx]# docker build -t nginx_image1 . 
Sending build context to Docker daemon 27.65 kB
Step 1/14 : FROM nginx
 ---> 8b89e48b5f15
Step 2/14 : MAINTAINER marvin
 ---> Using cache
 ---> fc935dc63140
Step 3/14 : ENV RUN_USER nginx
 ---> Using cache
 ---> 7ca3b90ddc2a
Step 4/14 : ENV RUN_GROUP nginx
 ---> Using cache
 ---> a2e2674d4c9f
Step 5/14 : ENV DATA_DIR /usr/share/nginx/html
 ---> Using cache
 ---> f031dd5f8ce0
Step 6/14 : ENV LOG_DIR /var/log/nginx
 ---> Using cache
 ---> e57a42b5bea0
Step 7/14 : RUN mkdir /var/log/nginx -p
 ---> Using cache
 ---> 0cbaaac3d55b
Step 8/14 : RUN chown nginx.nginx -R /var/log/nginx
 ---> Using cache
 ---> 714919c0618b
Step 9/14 : ADD html /usr/share/nginx/html
 ---> 1c75fe9cdd3f
Removing intermediate container 702f4e3ace2e
Step 10/14 : ADD logs /var/log/nginx
 ---> 545ba7cc270b
Removing intermediate container 123bb6c3703c
Step 11/14 : ADD conf/nginx.conf /etc/nginx/nginx.conf
 ---> d96a6dfa8ba9
Removing intermediate container db2394df7691
Step 12/14 : ADD conf/conf.d /etc/nginx/conf.d
 ---> d02f5acc99de
Removing intermediate container fdc7c2542990
Step 13/14 : EXPOSE 80
 ---> Running in 679ecad9d883
 ---> 9ee05d0ab66f
Removing intermediate container 679ecad9d883
Step 14/14 : ENTRYPOINT nginx -g "daemon off;"
 ---> Running in 774ac2a000ac
 ---> c4d5f5accc06
Removing intermediate container 774ac2a000ac
Successfully built c4d5f5accc06
[root@dev nginx]# 

注意最后面的点 (“.”),表示dockerfile所在当前目录。

从执行过程可以看出一共十三个步骤,都完成了。再查看目前的镜像:

[root@dev nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx_image1        latest              c4d5f5accc06        47 seconds ago      109 MB
docker.io/nginx     latest              8b89e48b5f15        6 days ago          109 MB
[root@dev nginx]# 

可以看到从官方拉取了一个nginx镜像,然后在此镜像的基础上构建了nginx_image1镜像。注意,由于nginx_image1是在nginx镜像的基础上构建出来的,所以如果你要删除nginx镜像是不允许的,只有先删除nginx_image1镜像后才可以删除nginx镜像。

3、 运行新构建的镜像
[root@dev nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx_image1        latest              c4d5f5accc06        47 seconds ago      109 MB
docker.io/nginx     latest              8b89e48b5f15        6 days ago          109 MB
[root@dev nginx]# docker run --name nginx -d -p 80:80 nginx_image1
00a88a12a6f4037c406a64f12e8c664cd1f1cbe1eb8c1c29371aa091a01ca161
[root@dev nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
00a88a12a6f4        nginx_image1        "/bin/sh -c 'nginx..."   15 seconds ago      Up 14 seconds       0.0.0.0:80->80/tcp   nginx
[root@dev nginx]# 

其他配置与上面情况一下的配置,浏览器查看效果
nginx:80

三、 备注

3.1 docker run 执行完没有出错,但是docker ps时没有运行的容器。
方案:用docker ps -a 能看到没有启动容器。
用docker logs 查看容器启动时的log

3.2 删除docker 镜像中REPOSITORY 和 TAG 为 None的镜像

docker images|grep none|awk ‘{print $3}’|xargs docker rmi

使用如下命令给某个镜像指定REPOSITORY 和TAG:
docker tag [image id] [name]:[版本]

例如:

docker tag b03b74b01d97 docker-redis:0.0.1

参考文章

https://my.oschina.net/u/3375733/blog/1591091
http://www.ywnds.com/?p=7611


评论
  目录