首页 文章

无法使用Nginx启动docker容器

提问于
浏览
0

我创建了一个Docker CentOS镜像,显示端口80,8000和443.当它构建完成后,我使用...运行容器

docker run -it -p 8080:8000 -u root <image_id>

安装在图像上的是Nginx . 使用service命令启动nginx有一些问题,所以我只需通过转到/ usr / bin / nginx启动它 . 我可以通过使用 ps aux | grep nginx 看到nginx正在运行

bash-4.2# ps aux | grep nginx
root         8  0.0  0.1 122892  2172 ?        Ss   18:57   0:00 nginx: master process nginx
nginx        9  0.0  0.3 123356  6972 ?        S    18:57   0:00 nginx: worker process
nginx       10  0.0  0.3 123356  6972 ?        S    18:57   0:00 nginx: worker process
nginx       11  0.0  0.3 123356  6972 ?        S    18:57   0:00 nginx: worker process
nginx       12  0.0  0.3 123356  6972 ?        S    18:57   0:00 nginx: worker process

我在/ etc / hosts,172.17.0.11获取容器的ip地址,但是当我在Web浏览器中访问该地址时,它只会加载很长时间,并最终超时 . 我不可否认是Nginx和nginx配置的新手,所以我不确定我在默认配置文件中是否缺少这些东西 . 我检查了nginx的访问和错误日志,两者都是空的 .

/etc/hosts

127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.12 85a91e447fca

/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

2 回答

  • 1

    从它的角度来看,你搜索的IP是容器的ip . 您必须从外部位置访问docker容器,因为您的浏览器位于本地计算机上而不是容器中 .

    本地计算机和容器是完全独立的世界,只能通过您公开的端口访问 . 认为(作为一个模拟)将其视为您家中内部网络的IP以及您从互联网提供商处获得的IP .

    如果您使用的是docker-machine,您可以使用以下命令找到需要使用的ip:

    docker-machine ip default
    

    如果你正在使用docker 'native'你可以去http://localhost:8080

    但是当你的nginx监听端口80和443时,你需要使用如下命令启动容器:

    docker run -it -p 8080:80 -p 8443:443 imagename
    

    请注意,如果您需要端口,则不仅存在端口 . 如果ngnix在容器中的端口80和443上运行,则如果将其添加到 docker run 命令,它将不会突然开始在端口8000上运行 . 在容器内,它仍将在端口80和443上运行 . 因此,如果要将外部世界(读取您自己的计算机)映射到容器,则必须将端口(例如8000)映射到容器中的实际端口80 . 没有别的办法 . 在这种情况下,容器内除80或443之外的任何其他映射都将失败 .

  • 1

    您的docker run命令应该映射容器端口80和443的一些主机端口 . 看起来您只映射8000 .

    您还应该使用主机IP(假设您将所有必需的容器端口映射到主机端口),例如http:// :8080

相关问题