2

A common workflow for me is I run docker-compose up during development in a web-project, I run docker inspect repo_app_1 | grep IPAddress and then go to the ipaddress in the browser.

Instead of fetching container's IP, I want to add the name of this container with its IP to the hosts file.

What would be the best way to do that? It's certainly possible, I can think of one way -- hijack the docker and docker-compose commands so that after each execution we run a script which runs docker container's output through awk and appends it to hosts file and also manages to delete the older entries.

Peeyush Kushwaha
  • 3,453
  • 8
  • 35
  • 69
  • you can expose application port to docker host and later use docker host's hostname with that port. Have your tired this? – Akshay barahate May 04 '19 at 16:31
  • Have you considered running the containers in your docker-compose setup behind a containerized load balancer? By exposing the port of this load balancer, you wouldn't have to worry about modifying your host file. – ctt May 04 '19 at 16:32
  • Possible duplicate of [Access docker container from host using containers name](https://stackoverflow.com/questions/37242217/access-docker-container-from-host-using-containers-name) – Akshay barahate May 04 '19 at 16:44
  • Usually you use a `ports:` directive to make container ports visible on the host, and (from the host console specifically) access `localhost:12345` in some form. The container-private IP address don't work in several contexts (from other hosts; on a Mac) and looking them up at all is a little inadvisable. – David Maze May 04 '19 at 19:55
  • @DavidMaze `ports` will expose it to `container-ip:port`, you further need `expose` key in config to specify which ports should be bound to your host machine so you can access it on `localhost:port` from the host machine. I agree about the container-private IP for deployments, but this question was for development purposes for which it should be fine. – Peeyush Kushwaha May 05 '19 at 08:20
  • `ports:` makes it accessible via `host-ip:port`; if you have multiple host IP addresses you can choose which one (defaults to "all"). `expose:` is basically a no-op. – David Maze May 05 '19 at 09:45

1 Answers1

1

One possibility is to use Traefik, a Docker-aware reverse proxy that includes its own monitoring dashboard.

See for instance "Traefik on Docker for Web Developers - With bonus Let's Encrypt SSL!", from Juan Treminio, in order to register automatically your containers and access them through a pre-defined URL.

Juan describes how to solve the "port dance":

If port 80 is mapped to web-server-A you must choose another port to bind for web-server-B and web-server-C.
This can quickly get old because you must remember that http://localhost goes to A, http://localhost:81 goes toB and http://localhost:82 goes to C.

He points out:

On virtual machines this problem does not really occur because you can assign a static IP address to your servers, and bind it to your system’s hosts file (/etc/hosts).
Containers are ephemeral by nature and do not normally get created on your host’s network but rather private networks with their own random IP addresses within special ranges. However, you must edit /etc/hosts for every VM you spin up and the list grows with the number of projects you handle.

Træfik solves both of these problems, first by removing the need to use ports in URLs and second by not needing you to edit /etc/hosts at all.

A new container will register itself to the Traefik docker network (docker network create --driver bridge traefik_webgateway) with:

docker run -d --name some-mailhog \
    --network traefik_webgateway \
    --label traefik.docker.network=traefik_webgateway \
    --label traefik.frontend.rule=Host:mailhog.localhost \
    --label traefik.port=8025 \
    mailhog/mailhog

The URL becomes simple http://mailhog.localhost.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I think this would work to map containers to specific urls. However it'd require separate configuration for each container. Since grepping for ip address would be easier for me than doing that, I don't see myself using this. I proposed using the name of the container as the url precisely to avoid configuration per-container. – Peeyush Kushwaha May 04 '19 at 16:37
  • @PeeyushKushwaha the all point, with Træfik , is for you to *not* grep IP addresses, since it (Træfik) is Docker-aware. – VonC May 04 '19 at 16:39
  • that's after I configure it for that container, correct? – Peeyush Kushwaha May 04 '19 at 16:41
  • @PeeyushKushwaha I just added the example from the article: no IP addresses involved. – VonC May 04 '19 at 16:41
  • @PeeyushKushwaha Test it first, but yes: Traefik is made for this kind of use case. – VonC May 04 '19 at 16:44