0

Hello everyone i am just beginning docker and really appreciate the whole container concepts but one thing i cant seem to figure out is that when i use docker compose to define my services i have to give a network also and all containers connect to that network and hence are reachable to one another but what if my one service needs to connect to a database service hosted on localhost.How will my service be able to reach the database i have searched about networking mode host and net:host options in compose file but they don't seem to work my docker version info is as follows.

Client: Docker Engine - Community
Version:           18.09.2
API version:       1.39
Built:             Sun Feb 10 04:12:31 2019
OS/Arch:           windows/amd64
Experimental:      false

Server: Docker Engine - Community
Engine:
 Version:          18.09.2
 API version:      1.39 (minimum version 1.12)
 Built:            Sun Feb 10 04:13:06 2019
 OS/Arch:          linux/amd64
 Experimental:     false

my docker-compose.yml file

version: "3"
services:
 web:
   # replace username/repo:tag with your name and image details
   image: dbaccessserviceimage
   network_mode: "host"
   deploy:
     replicas: 5
     resources:
       limits:
         cpus: "0.1"
         memory: 50M
     restart_policy:
       condition: on-failure
   ports:
     - "4000:80"

Really need help as i am designing the architecture to move all our production into containers A docker compose file with appropriate version and network option would be highly appreciated.

Thanh Nguyen Van
  • 10,292
  • 6
  • 35
  • 53
JayD
  • 748
  • 1
  • 13
  • 38
  • can you share `Dockerfile` file to see how to you denfine network ? – Thanh Nguyen Van Jul 17 '19 at 10:07
  • @ThanhNguyenVan check my edit – JayD Jul 17 '19 at 10:10
  • can you show the output `docker inspect container_id`? – Thanh Nguyen Van Jul 17 '19 at 10:16
  • A couple of probable issues come to mind. Is the localhost server only available over 127.0.0.1 and/or a socket? Can the docker container ping the "real ip" of the localhost machine (not 127.0.0.1)? Normally, the way this is done, is to create a database service in the docker environment and use the service name as the hostname. You can map the actual database files to a local volume for persistence. – Dov Rine Jul 17 '19 at 10:18
  • Just found this that might help: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Dov Rine Jul 17 '19 at 10:20
  • @ThanhNguyenVan when i try to run docker stack i get ignoring unsupported options: network_mode – JayD Jul 17 '19 at 10:21
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Jul 17 '19 at 13:20
  • Docker provides a magic hostname `host.docker.internal` on non-Linux, or else you can look up the host’s `docker0` IP address on Linux (perhaps it is 172.17.0.1). Configure your container to use that. `localhost`, from the container’s point of view, is the container. – David Maze Jul 17 '19 at 13:22

0 Answers0