user706838
user706838

Reputation: 5380

Unable to connect docker nginx with docker ubuntu

I have noticed an issue with docker nginx which is not the case when nginx is running on the host machine (apt-get install). Here is how to reproduce my issue:

solution A: 'nc' on container 1, 'nginx' on container 2, 'curl' on host

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)

'nc' on container 1

docker run -ti --name agitated_stallman ubuntu:14.04 bash
nc -l 4545

'nginx' on container 2

LOLPATH=$HOME/testdocker
echo $LOLPATH

mkdir -p $LOLPATH
cd $LOLPATH

subl mple.conf

.

server {

    listen 80;
    root /var/www/html;

    location /roz {
        proxy_pass http://neocontainer:4545;
        proxy_set_header Host $host;
    }

}

.

docker run --link agitated_stallman:neocontainer -v $LOLPATH/mple.conf:/etc/nginx/sites-available/default -p 12345:80 nginx:1.9

'curl' on host

sudo apt-get install curl
curl http://localhost:12345/roz

ERROR response from 'nginx':

2016/03/04 19:59:18 [error] 8#8: *3 open() "/usr/share/nginx/html/roz" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /roz HTTP/1.1", host: "localhost:12345"
172.17.0.1 - - [04/Mar/2016:19:59:18 +0000] "GET /roz HTTP/1.1" 404 169 "-" "curl/7.45.0" "-"

solution B: 'nginx' on host, 'nc' on host, 'curl' on host

'nginx' on host

sudo apt-get install nginx
sudo subl /etc/nginx/sites-available/default

.

server {

    listen 80;
    root /var/www/html;

    location /roz {
        proxy_pass http://localhost:4646;
        proxy_set_header Host $host;
    }

}

.

 sudo service nginx restart

'nc' on host

nc -l 4646

'curl' on host

sudo apt-get install curl
curl http://localhost:80/roz

SUCCESS response from 'nc':

GET /roz HTTP/1.0
Host: localhost
Connection: close
User-Agent: curl/7.45.0
Accept: */*

Upvotes: 0

Views: 937

Answers (1)

Roman
Roman

Reputation: 6657

In short: run nginx container with -v $LOLPATH/mple.conf:/etc/nginx/conf.d/default.conf

nginx:1.9 docker image currently uses nginx package from nginx's own repository, not from official debian repository. If you examine that package, you'll find that /etc/nginx/nginx.conf does include only from /etc/nginx/conf.d/*.conf, and that package ships with pre-included /etc/nginx/conf.d/default.conf:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    # other not important stuff
    # ...
}

So your config is not used at all, which explains the open() "/usr/share/nginx/html/roz" failed error.

When you install nginx directly on host, you probably use official debian repository, which has different main config file, which in turn does include /etc/nginx/sites-available/*, and your config is actually used.

Upvotes: 1

Related Questions