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:

  • How to have my docker daemon log print to logfile not to STDOUT?
  • Is it possible to mount docker volume to a different filesystem?
  • How to serve couple containers with domain names with Docker NGINX image
  • Error starting datanode on hadoop
  • Use Docker rather than native/homebrew on Mac?
  • How does Vagrant restart docker containers on bootup?
  • "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?

  • Docker compose mysql reachable on port 3306
  • Adding repos to the dockerfile
  • can a dockerised app execute script on the host?
  • Build inside docker container in jenkins
  • Makefile fails executing sudo docker kill
  • Unable to retrieve restored archive from AWS S3 'Invalid size value: 1g'
  • 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.