首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法用Nginx启动码头集装箱

问无法用Nginx启动码头集装箱
EN

Stack Overflow用户
提问于 2018-02-21 19:02:32
回答 2查看 650关注 0票数 0

我创建了一个显示端口80、8000和443的Docker CentOS映像。当它建成后,我用.

代码语言:javascript
复制
docker run -it -p 8080:8000 -u root <image_id>

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

代码语言:javascript
复制
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/host获得容器的ip地址,172.17.0.11,但当我在web浏览器中访问该地址时,它只是加载了很长时间,并最终超时。诚然,我对Nginx和nginx配置非常陌生,所以我不确定在默认配置文件中是否遗漏了什么。我检查了nginx的访问和错误日志,两者都是空的。

/etc/主机

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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 {
#        }
#    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-21 19:35:44

从它的角度来看,您搜索的IP是容器的ip。当浏览器驻留在本地计算机上而不是容器中时,必须从外部位置访问码头容器。

本地机器和容器是完全独立的世界,只能通过您公开的端口访问。把它看作是你家庭内部网络的ip,以及你从互联网提供商那里得到的ip。

如果您使用的是停靠机,您可以通过以下命令找到需要使用的ip:

代码语言:javascript
复制
 docker-machine ip default

如果您使用的是码头“本地”,您可以转到http://localhost:8080

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

代码语言:javascript
复制
 docker run -it -p 8080:80 -p 8443:443 imagename

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

票数 1
EN

Stack Overflow用户

发布于 2018-02-21 19:36:09

您的docker命令应该映射容器端口80和443的一些主机端口。看来你只画了8000张地图。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48913537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档