9 Docker部署Nginx

1 下载镜像

[root@localhost gp6]# docker pull nginx

2 创建容器

# 创建容器
[root@localhost gp6]# docker run -di --name gp6-nginx -p 80:80 nginx:latest
12e86069753156552ffde329883a8b080f9b1a22eb32fc4dcac33d0be33e1c82


# 查看运行容器列表
[root@localhost gp6]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                    NAMES
12e860697531        nginx:latest            "nginx -g 'daemon of…"   20 seconds ago      Up 19 seconds       0.0.0.0:80->80/tcp       gp6-nginx
f5e1bc5f3acc        redis:latest            "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:6379->6379/tcp   gp6-redis
c4735852267d        tomcat:8-jdk8-openjdk   "catalina.sh run"        30 minutes ago      Up 30 minutes       0.0.0.0:8080->8080/tcp   gp6-tomcat-8
 

3 测试

直接在浏览器输入 宿主机ip
9 Docker部署Nginx_第1张图片
测试nginx是否部署成功

4 将静态页面部署到nginx

# 进入nginx容器
[root@localhost gp6]# docker exec -it gp6-nginx /bin/bash

# 查看目录结构
[root@12e860697531:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 进入nginx
[root@12e860697531:/# cd etc/nginx/

# 查看nginx配置
[root@12e860697531:/etc/nginx# 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;

    # 将conf.d目录下的*.conf引入
    include /etc/nginx/conf.d/*.conf;
}

# 文件中定义html所放置位置
[root@12e860697531:/etc/nginx# cat conf.d/default.conf 
server {
    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;
    #}
}

# 退出nginx容器
root@12e860697531:/etc/nginx# exit
exit

# 自定义首页
[root@localhost gp6]# vi index.html

Nginx Run In Docker

GP6 Study Docker



# 将自己的首页替换nginx容器中的首页
[root@localhost gp6]# docker mv index.html gp6-nginx:usr/share/nginx/html/


# 再次访问宿主机ip,即可看到首页为自定义首页

你可能感兴趣的:(9 Docker部署Nginx)