Docker 安装 Nginx

查找Docker Hub上的nginx镜像

$ docker search nginx

拉取官方的镜像

$ docker pull nginx

运行容器

$ docker run --name docker-nginx -d -p 80:80 -v /Users/ct/project:/usr/share/nginx/html -v /Users/ct/etc/docker/nginx/logs:/var/log/nginx -v /Users/ct/etc/docker/nginx/conf.d:/etc/nginx/conf.d -v /Users/ct/etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf nginx

命令说明:

-d :分离模式: 在后台运行

-p 80:80:将容器的80端口映射到主机的80端口

--name docker-nginx:将容器命名为docker-nginx

-v /Users/ct/etc/docker/nginx/html:/usr/share/nginx/html:将主机中当前目录下的html挂载到容器的/html

-v /Users/ct/etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:将主机中当前目录下的nginx.conf挂载到容器的/etc/nginx/nginx.conf

-v /Users/ct/etc/docker/nginx/logs:/var/log/nginx:将主机中当前目录下的logs挂载到容器的/var/log/nginx

-v /Users/ct/etc/docker/nginx/conf.d:/etc/nginx/conf.d:将主机中当前目录下的vhost挂载到容器的/etc/nginx/conf.d

命令说明:

-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;

-d: 后台运行容器,并返回容器ID;

-i: 以交互模式运行容器,通常与 -t 同时使用;

-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

--name="nginx-lb": 为容器指定一个名称;

--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;

--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;

-h "mars": 指定容器的hostname;

-e username="ritchie": 设置环境变量;

--env-file=[]: 从指定文件读入环境变量;

--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;

-m :设置容器使用内存最大值;

--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;

--link=[]: 添加链接到另一个容器;

--expose=[]: 开放一个端口或一组端口;

注意:

1、宿主机nginx.conf配置文件中的user要和NGINX容器中的user保持一致

2、宿主机配置文件中的各个路径(日志,root等)要和NGINX容器中的保持一致

示例配置:

server {

    listen       80;

    server_name  share.mis.com;

    root /usr/share/nginx/html/mis/public;

    index index.php index.html index.htm;

    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {

        try_files $uri $uri/ /index.php?$query_string;

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #

    location ~ \.php$ {

        root           /var/www/html/mis/public;

        fastcgi_pass   docker-php-fpm:9000;

        fastcgi_index  index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include        fastcgi_params;

    }

}

猜你喜欢

转载自tzhennan.iteye.com/blog/2415018