Docker expose link to host machine

I am using a docker-compose.yml file for an application service and a database service:

version: '2'

     image: app-image
     container_name: app-container
       - "5000:5000"
       - my-network
       - db-service

     image: db-image
     container_name: db-container
       - "5432:5432"
       - my-network

    driver: bridge

The application service connects to the database using a connection string with the db-service as host:

  • Mesos, Marathon, Docker, Wildfly
  • What is the difference between the size and the virtual size of the docker images?
  • Why Is Apache The Primary Webserver In Docker Containers (for PHP)
  • How to sync dir from a container to a dir from the host?
  • Reliable centralized logging in Kubernetes/Docker with Elastic Stack
  • Use Docker Compose to create multiple containers
  • "User ID=postgres;Password=password;Host=db-service;Port=5432;Database=my-table;Pooling=true;"

    That works fine on the running app container but I also need to access the database using the same connection string from my host machine. (for database migrations during development)

    • I thought about adding a route to my host machine:
      db-service -> localhost But this is not a good solution since it depends on the host operating system.

    • I tried to use localhost in the connection string since my host machine then connects to the database container. But obviously the application container will not connect any more.

    • I also played around with network_mode settings and network driver in the compose file but had no luck with that.

    How can I use the same hostname in a container like in my host machine?

  • Access rest api endpoint docker
  • docker windows exposed ports cannot be accessed from outside the host
  • Docker - Run Container from Inside Container
  • How can we use shared Dockerfile's which reside in different directories than the “current build context path”?
  • How can I keep container running on Kubernetes?
  • Docker containers with one public IP
  • One Solution collect form web for “Docker expose link to host machine”

    The only way you could use the same connection string for both your app-service and your host would be to update your host’s /etc/hosts file with: db-service

    (or use your if you’re running on Docker machine)

    But this solution may not be the cleanest one.
    The best one would, IMO, be not to use the same connection string.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.