TJB
TJB

Reputation: 3836

Trouble starting docker container with Nginx

I created a Docker CentOS image exposing ports 80, 8000, and 443. When it was done being built, I ran the container using ...

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

Installed on the image is Nginx. There are some issues using the service command to start nginx, so I simply started it by going to /usr/bin/nginx. I can see that nginx is running by using ps aux | grep 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

I get the ip address for the container at /etc/hosts, 172.17.0.11, but when I go to that address in a web browser it just loads for a long time, and eventually times out. I am admittedly pretty new to Nginx, and nginx configurations, so I'm not sure if theres something I'm missing in the default configuration file. I checked the access and error logs for nginx, and both are empty.

/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 {
#        }
#    }

}

Upvotes: 0

Views: 953

Answers (2)

Ivonet
Ivonet

Reputation: 2731

The IP you searched for was the ip of the container from it's point of view. you must access the docker container from the external location as your browser lives on your local machine and not in the container.

The local machine and the container are are completely separate worlds and can only be accessed through the ports you exposed. think (as an analogue) about it as the ip of your internal network at home and the ip you get from your internet provider.

if you are using docker-machine you can find the ip you need to use with the following command:

 docker-machine ip default

if you are using docker 'native' you can go to http://localhost:8080

but as your nginx listens to port 80 and 443 you need to start the container with a command like:

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

Note that ports don't just exist if you want them to. If ngnix is running on port 80 and 443 in the container it will not suddenly start running on port 8000 if you add it to the docker run command. within the container it will still run on port 80 and 443. so if you want to map the outside world (read your own computer) to the container than you have to map a port (e.g. 8000) to the actual port 80 in the container. Nothing else will work. any other mapping than 80 or 443 within the container will in this case fail.

Upvotes: 1

Chun-Yen Wang
Chun-Yen Wang

Reputation: 578

Your docker run command should map some host ports for container ports 80 and 443. Looks like you only map 8000.

Also you should use host IP, (assuming you map all required container ports to host ports), such as http://{host ipv4}:8080

Upvotes: 1

Related Questions