2

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.

1

The best pratice is AVOID do any command line tasks in a Docker container, the best way to get your nginx containers up and running exactly like you expect is by creating a custom image and ADD your configuration file in image's Dockerfile.

You can find a detailed description on how to create your custom image with custom configuration here.

1

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;
  }
}
  • Thanks for the proposal, it is barely the configuration I used, and unfortunately it is the one, which doesn't remove the port, the wordpress.domain.com is accessible, but then I'm redirected to a page with the port added. – Jean-Christophe Meillaud Mar 9 '14 at 23:55
0

My answer is a bit late : for future reference, you should take a look at https://github.com/jwilder/nginx-proxy. This tool automatically generates appropriate Nginx configuration to forward incoming requests to Docker containers (based on subdomains).

First, launch the nginx-proxy:

docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

For instance, if you want to redirect sub.domain.com to a specific container, simply set the VIRTUAL_HOST container environment variable to "sub.domain.com":

docker run -e VIRTUAL_HOST=sub.domain.com ...

Here is a detailed tutorial I wrote about it: http://blog.florianlopes.io/host-multiple-websites-on-single-host-docker.

It matches perfectly your expected behavior.

  • Welcome to ServerFault! Ideally your answer would contain the details/code/instructions necessary for addressing the OP's issue; links can change/go dead/stale over time, thus using them to provide the full answer is frowned upon. It's better to provide the full details here, and a link for reference. – Castaglia Apr 6 '16 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.