Norgul
Norgul

Reputation: 4783

Can't curl between two containers

I have trouble getting my two containers to communicate. I have 2 separate docker-compose.yml files in different projects.

First one is:

version: '2'

services:
  myapp:
    tty: true
    image: 'bitnami/laravel:latest'
    labels:
      kompose.service.type: nodeport
    ports:
      - 3000:3000
    expose:
      - "3000"
    volumes:
      - ./:/app

networks:
  default:
    external:
      name: "laravel"

And second one:

  app:
    build:
      context: .
      dockerfile: ./docker/app/Dockerfile
    image: bla/bla
    container_name: my-app
    ports:
      - "80:80"
      - "443:443"

networks:
  default:
    external:
      name: "laravel"

When going to browser or making a curl from terminal to localhost:3000 I get the response back. If I enter the myapp container I can also curl (which is really not strange).

But I can't make it so that I enter the app container and get a response. Instead I am getting:

curl: (7) Failed to connect to localhost port 3000: Connection refused

Upvotes: 1

Views: 4454

Answers (1)

Andrey
Andrey

Reputation: 2729

When you accessing localhost in container, you accessing container itself, not the host machine.

For example if you would like to access port on host machine, there are some discussion about it: https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/5

But you need to access another container on the same host. Generally it's a bad practice using localhost for it, because in future container could be moved to another host, you will forget about it and system will broke.

If you still want to do it, you need to create network bridges in Docker.

You can find documentation related to bridges: https://docs.docker.com/network/network-tutorial-standalone/

Upvotes: 1

Related Questions