一、拉取镜像

​docker pull nginx

查询镜像:

docker images

二、执行镜像

2.1 配置目录

创建nginx目录

mkdir  /usr/local/nginx

在nginx下创建conf 、logs和html目录

 mkdir conf logs html

2.2 拉取配置

先执行一个nginx镜像

docker run -d --name nginx nginx

执行成功后,拷贝配置

cd /usr/local/nginx/conf

拷贝配置到conf目录

docker cp nginx:/etc/nginx/ .

如果把nginx文件夹拷贝过来了,那么把nginx下面的文件拷贝到conf里面。删除nginx文件夹

cd /usr/local/nginx/nginx

拷贝文件到conf里面

mv * ../conf
cd ..

删除nginx目录

rm -rf nginx

2.3 修改配置

修改

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

在location 下面添加,允许跨域

add_header 'Access-Control-Allow-Origin' '*';

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        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;
    #}
}

修改:

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

把user nginx 改为user root

执行权限


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;

    #gzip  on;

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

然后停止运行nginx容器,并且删除容器

docker stop nginx
docker rm nginx

2.4 执行新容器

把相应的html项目放入到/usr/local/nginx/html下面,然后进行映射

外部端口80,还有把日志和配置隐射出去方便后续好修改,时间改为上海格式保持统一

运行

2.4.1 docker执行

docker run -d --name nginx \
-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:/etc/nginx/ \
-e TZ="Asia/Shanghai" \
--restart=always  nginx

2.4.2 docker-compose执行

或者采用 docker-compose

创建docker-compose.yml

version: '3.8'
services:
  nginxser:
    restart: always
    image: nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /usr/local/nginx/html/:/usr/share/nginx/html
      - /usr/local/nginx/logs:/var/log/nginx
      - /usr/local/nginx/conf:/etc/nginx
    environment:
      - TZ:"Asia/Shanghai"

在docker-compose.yml的目录下启动执行 

 docker-compose up

停止执行

 docker-compose dowm

宿主机:80容器80

数据存储/usr/local/nginx/html/ 映射容器:/usr/share/nginx/html

日志映射和配置映射

--restart=always 重启

访问:http://宿主机ip

比如你的ip是192.168.20.2  那么就是 http://192.168.20.2/

如果出现如下就ok了

更多推荐