Salvatore D'angelo
Salvatore D'angelo

Reputation: 1149

Cannot ping docker container created with docker-compose

I want to create a PostgreSQL cluster composed by a master and two slaves within three containers. I want to do that with docker-compose. Everything works fine but I cannot ping containers from my Mac.

Here the code of my docker-compose.yml.

On Stackoverflow there is this thread How could I ping my docker container from my host that address docker standalone and not docker-compose.

version: '3.6'

volumes:
    pgmaster_volume:
    pgslave1_volume:
    pgslave2_volume:

services:
    pgmaster:
        container_name: pgmaster

        build:
            context: ../src
            dockerfile: Dockerfile

        image: docker-postgresql:latest

        environment:
            NODE_NAME: pgmaster # Node name

        ports:
            - 5422:5432

        volumes:
            - pgmaster_volume:/home/postgres/data

        networks:
            cluster:
                ipv4_address: 10.0.2.31
                aliases:
                    - pgmaster.domain.com

    pgslave1:
        container_name: pgslave1

        build:
            context: ../src
            dockerfile: Dockerfile

        image: docker-postgresql:latest

        environment:
            NODE_NAME: pgslave1 # Node name

        ports:
            - 5441:5432

        volumes:
            - pgslave1_volume:/home/postgres/data

        networks:
            cluster:
                ipv4_address: 10.0.2.32
                aliases:
                    - pgslave1.domain.com
    pgslave2:
        container_name: pgslave2

        build:
            context: ../src
            dockerfile: Dockerfile

        image: docker-postgresql:latest

        environment:
            NODE_NAME: pgslave2 # Node name

        ports:
            - 5442:5432

        volumes:
            - pgslave2_volume:/home/postgres/data

        networks:
            cluster:
                ipv4_address: 10.0.2.33
                aliases:
                    - pgslave2.domain.com

networks:
  cluster:
    driver: bridge
    ipam:
      config:
        - subnet: 10.0.2.1/24

On my Mac, I have a 192.168.0.0 local network. I expect that doing ping 10.0.2.31 I can ping my container but this is not possible. I think this is due to Linux VM created inside Mac where containers live and the IPs are not reachable outside this VM.

Can someone help me to understand how to make the above three IP reachable? IPs are reachable from one container to another.

Here my full code: https://github.com/sasadangelo/docker-postgres

Upvotes: 2

Views: 15737

Answers (1)

Efrat Levitan
Efrat Levitan

Reputation: 5612

you should be able to ping your containers from you host.

  • via public ip:

    just use their public ip. (you had been trying to ping your container local ip, inside the docker network)

    how to find the container public IP?

    • you can get it by running ifconfig inside the container.

    or

    • or by running on your host docker container inspect <container_id>.

      it should be there under NetworkSettings.<network_name>.IPAddress )

  • via container name/id

    docker is running some sort of dns on your machine so you can also use the container name or id - ping <container_name/id>

note

the way to access your containers outside the docker network is via their published ports. you have bound port 5432 on the docker network to port 5442 on your host, therefore the container should listen and accept traffic at 127.0.0.1:5442 (thats your localhost at the port you've bound)

Upvotes: 4

Related Questions