Nginx – Access to a docker instance – Best Practice

dockernginx

What are the best practice to access a docker instances from the outside.

I'm planning to deploy several docker instances (node.js, php, mysql), either all packaged in one or through docker links between containers.

It is possible to control port on the host machine for each docker instances, but then what should be the best practice to access those instance from the outside. I would rather have subdomain only, and get rid of the forwarded port of the instance.

example:

user@server:~/sandbox/docker-nginx$ sudo docker ps
CONTAINER ID    IMAGE   COMMAND    CREATED    STATUS    PORTS    NAMES

3fc7c57ed66c    docker-wordpress-nginx:latest   /bin/bash /start.sh   3 days ago          Up 3 days    0.0.0.0:49153->80/tcp   evil_poincare       

Expected behavior :
Request wordpress.domain.com access the wordpress inside that docker instance, through the port 49153, but browser do not rewrite the Location with that port.

I had a look/tried several approaches, but none gave me that functionality :

  • bouncy with routes.json files
  • reverse proxying with nginx in host machine, but still have the port in the redirected url
  • skydock sounds great but no multi host support yet
  • pipework looks good also, but the disclaimer warn us that it should be provided by docker complex scenarios soon.

Any thoughts on that problem, and any recommendation on the best practice would be greatly appreciated, maybe I'm just missing something.

Best Answer

I am using nginx for this, it works very well (for me). From your comments sounds like its not working as your expecting however...

A simplistic configuration that should work is below.

/etc/nginx/sites-enabled/wordpress.domain.com

server {
  server_name wordpress.domain.com;
  location / {
      proxy_redirect off;
      proxy_set_header Host $host ;
      proxy_set_header X-Real-IP $remote_addr ;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
      proxy_pass http://localhost:49153;
  }
}