Cannot get connection between containers

docker communication between containers
docker-compose
docker-compose network
dial tcp
docker-compose connect to container
docker connection refused between containers
docker-compose frontend - backend network
docker-compose host network
update resolution

The issue was a typo in the docker_compose file. I had the postgres container labeled as postgre. That fixed the network lookup issue. Please advice if I should delete this post.

I have included the code here.. https://github.com/vinceyoumans/achal

PROBLEM: Two containers in a docker compose file. one is standard Postgress container. The second is a goLang Scratch container. The GoLang main.go file panics when the postgres connection fails. you can see the code on github.

The error is:
dial tcp: lookup postgres on 127.0.0.11:53: no such host

REQUEST: look over the main.go and dockercompose file to see what is wrong with this network.. what I am missing.

main.go
db, err := gorm.Open("postgres", "host='postgres' port=5432 user=docker dbname='docker' password='password'")

if err != nil {
    fmt.Println("============   exiting ==========")
    fmt.Println(err)
    panic("failed to connect database e")
    //  the error i get... dial tcp: lookup postgres on 127.0.0.11:53: no such host
    // panic("failed to connect database: " + err)
}

The docker-compose.yml

version: '3.6'
services:
  postgre:
    image: postgres:11.1-alpine
    ports:
      - '5432:5432'
    #network_mode: bridge
    #container_name: postgres

    environment:
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
      POSTGRESS_DB: 'db_amex01'
    volumes:
      - ./init:/docker-entrypoint-initdb.d/

  todo:
    build: ./go_amex/
    # ports:
    #   - "8000:8080"

# sudo docker-compose -f docker-compose.yml up

#  HELP:  how would I add the goLang service from this point?

You have a typo in your compose file. A DNS alias is automatically configured for the service name, and a default docker network is also created for your project. So all that should be needed is to connect to the service name, which in your case was "postgre" instead of "postgres". To fix that, try this compose file:

version: '3.6'
services:
  postgres:
    image: postgres:11.1-alpine
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
      POSTGRESS_DB: 'db_amex01'
    volumes:
      - ./init:/docker-entrypoint-initdb.d/

  todo:
    build: ./go_amex/
    # ports:
    #   - "8000:8080"

For more details on compose file networking, see: https://docs.docker.com/compose/networking/

Note that the next error you will likely see is a connection refused. Compose will start both containers simultaneously, and your application will likely be running before the database finishes starting. To solve that, you will want a retry loop with a short delay between retries, and a timeout or retry limit, in your application code.

Can't connect to other containers inside docker network, Can't connect to other containers inside docker network E.g. I should be able to ping foo from bar, but I cannot. FROM debian:stretch-slim RUN set -x && apt-​get update && apt-get install -y apt-utils RUN set -x && apt-get  If no connections fail but requests submitted via the ingress network continue to have problems, move to the next section on troubleshooting the ingress network. If no connections fail, and the issue has only been seen container-container, then check another set of services or hosts until one task fails to reach another.


You're missing the networking configuration. This is required if you want to communicate between containers.

version: '3.6'
services:
  postgres: # you were missing the 's'
    image: postgres:11.1-alpine
    ports:
      - '5432:5432'
    networks:
      - mynet

    environment:
      POSTGRES_USER: 'user'
      POSTGRES_PASSWORD: 'password'
      POSTGRESS_DB: 'db_amex01'
    volumes:
      - ./init:/docker-entrypoint-initdb.d/

  todo:
    build: ./go_amex/
    ports:
      - "8000:8000" # I looked at your main.go file, it's port 8000!
    networks:
      - mynet

networks:
  mynet:
    driver: bridge

Now you can see (via dns resolution) each service from within the containers by using the service name as the hostname.

4 Reasons Why Your Docker Containers Can't Talk to Each Other , Containers that don't share a network cannot communicate with one another¹. If you don't find the container you're trying to connect to in any of the networks,  Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.. It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to


The issue was a typo in the docker_compose file. I had the postgres container labeled as postgre. That fixed the network lookup issue. Please advice if I should delete this post.

Connection refused? Docker networking and how it impacts your , Can't connect to the server running in your container? your browser to http://​127.0.0.1:8000 you'll get connection refused or connection reset. What docker run -p 5000:5000 does, and why our example above doesn't work. Containers are not as complex as they sound, turns out they are pretty simple concept and so is their architecture, a docker container is simply a service running on a setup Your containers run on the docker architecture using the configuration in the DockerFile, the docker-compose.yml file or the image specified in the docker run command to set up your containers.


Unable to connect to the Docker Container from the host browser on , And so, I have to find a way to connect to it's API through a specific IP address. I will checkout the link you provided and see if there are any  Yes. You can mount a socket into a container using a volume mount. And multiple containers can mount the same volume, whether that's a named volume or a host mount, to share the socket between the containers. You see this frequently with containers that mount the docker socket today, e.g.


Docker Compose Networking, Configure networking between containers when using Docker Compose. Containers on a single network can reach and discover every other container on the Keep in mind, proxy cannot connect to db , as they do not share a network;​  You can use the --link option with docker run to make network connection information about a server container available to a client container. The client container uses a private networking interface to access the exposed port in the server container. Docker sets environment variables about the server container in the client container that describe the interface and the ports that are available.


How to Connect Docker Containers, This guide will show you how to link Docker containers using a Node.js For Ubuntu 19.04, if you get an E: Package 'docker-ce' has no  Containers each have their own network namespace by default. Compose will place all containers on a shared network and set an alias in DNS for the service name. So to connect between containers, all you need to do is point to your service name instead of the 127.0.0.1 (assuming mysql is your service name):