无法将docker nginx与docker ubuntu连接起来

我已经注意到docker nginx的一个问题,当主机上运行nginx时,情况并非如此( apt-get install )。 这里是如何重现我的问题:

解决schemeA:容器1上的'nc',容器2上的'nginx',主机上的'curl'

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

'nc'在容器1上

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

容器2上的'nginx'

 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”

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

'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" "-" 

解决schemeB:主机上的'nginx',主机上的'nc',主机上的'curl'

主机上的'nginx'

 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'在主机上

 nc -l 4646 

在主机上“curl”

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

来自'nc'的成功回应:

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

简而言之:使用-v $LOLPATH/mple.conf:/etc/nginx/conf.d/default.conf运行nginx容器-v $LOLPATH/mple.conf:/etc/nginx/conf.d/default.conf

nginx:1.9 docker image 目前使用的是 nginx自己的仓库中的nginx包,而不是官方的debian仓库。 如果你检查这个包 ,你会发现/etc/nginx/nginx.conf确实只include/etc/nginx/conf.d/*.conf ,并且这个包提供了预先包含的/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 # ... } 

所以你的configuration根本不用,这就解释了open() "/usr/share/nginx/html/roz" failed错误。

当你直接在主机上安装nginx的时候,你可能会使用官方的debian仓库,它有不同的主要configuration文件,其中include /etc/nginx/sites-available/* ,并且你的configuration文件被实际使用。