Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign domain names to containers in Docker?

I am reading a lot these days about how to setup and run a docker stack. But one of the things I am always missing out on is how to setup that particular containers respond to access through their domain name and not just their container name using docker dns.

What I mean is, that say I have a microservice which is accessible externally, for example: users.mycompany.com, it will go through to the microservice container which is handling the users api

Then when I try to access the customer-list.mycompany.com, it will go through to the microservice container which is handling the customer lists

Of course, using docker dns I can access them and link them into a docker network, but this only really works for wanting to access container to container, but not internet to container.

Does anybody know how I should do that? Or the best way to set that up.

like image 813
Christopher Thomas Avatar asked Nov 29 '16 13:11

Christopher Thomas


People also ask

How do I assign a name to a docker container?

How to Name a Docker Container. You can assign memorable names to your docker containers when you run them, using the --name flag as follows. The -d flag tells docker to run a container in detached mode, in the background and print the new container ID.

What is the command to set DNS server for all docker containers?

--dns=IP_ADDRESS Add the DNS server to the /etc/resolv. conf of the container and let the container use this server to resolve all hostnames that are not in /etc/hosts . --dns-search=DOMAIN sets the search domain of the container. When the search domain is set to .

Can we assign IP to docker container?

When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.


1 Answers

So, you need to use the concept of port publishing, so that a port from your container is accessible via a port from your host. Using this, you can can setup a simple proxy_pass from an Nginx that will redirect users.mycompany.com to myhost:1337 (assuming that you published your port to 1337)

So, if you want to do this, you'll need to setup your container to expose a certain port using:

docker run -d -p 5000:5000 training/webapp # publish image port 5000 to host port 5000 

You can then from your host curl your localhost:5000 to access the container.

curl -X GET localhost:5000 

If you want to setup a domain name in front, you'll need to have a webserver instance that allows you to proxy_pass your hostname to your container.

i.e. in Nginx:

server {   listen 80;   server_name users.mycompany.com;   location / {     proxy_pass http://localhost:5000;   } } 

I would advise you to follow this tutorial, and maybe check the docker run reference.

like image 67
MagicMicky Avatar answered Sep 22 '22 07:09

MagicMicky