0

Im trying to wrap my head around docker-compose networks.

I have created an docker-compose file that consists of a frontend (ReactJS) and backend (PHP Laravel)

Through my frontend, I am able to make http request to the backend with localhost:8000 But, in my backend, I can't make any request back to the frontend with localhost:3000

Why is this not possible?

I found this problem, because I created a new container, that enables me to screenshot the content of a website. But when I need to screenshot localhost:3000 through the chrome container, it does not have access to localhost:3000? But if I change it to frontend:3000 it does? How can I enable the chrome container to be able to connect to the backend through localhost:8000

So basically I can connect through http://frontend:3000/ but that does not have access to the localhost:8000 which is what I need.

Hope this makes sense.

My docker-compose file:

version: "3.5"

services:
  lumen:
    ports:
      - "8000:8000"
    volumes:
      - ./server:/var/www/html
      - ./server/vendor:/var/www/html/vendor/
    build: 
      context: server
      dockerfile: Dockerfile
    command: php -S lumen:8000 -t public
    restart: always
    privileged: true
    depends_on:
      - database
    networks:
      - database
      - test

  frontend:
    build:
      context: client
      dockerfile: Dockerfile
    volumes:
      - ./client/src:/app/src
    ports:
      - 3000:3000
    stdin_open: true
    #restart: always
    networks:
      - database

  chrome:
    image: selenium/standalone-chrome:latest
    networks:
      - database
      - test
    privileged: true
    shm_size: 2g
    ports:
      - 4444:4444

  # Database Service (Mysql)
  database:
    image: mysql:latest
    container_name: blogmoda_mysql
    environment:
      MYSQL_DATABASE: blogmoda-app
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    command: ['--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci','--default-authentication-plugin=mysql_native_password']
    ports:
      - "127.0.0.1:3306:3306"
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - database

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev_phpmyadmin
    links:
      - database
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    depends_on: 
      - database
    ports:
      - 9001:80
    networks:
      - database

volumes:
  db-data:

# Networks to be created to facilitate communication between containers
networks:
  database:
  test:
helth
  • 3
  • 2

1 Answers1

0

You can reach other containers on the same network using the container name as the host. You're already doing this with the frontend host. It should work for lumen:8000 as well from within the frontend container.

Nick K9
  • 3,885
  • 1
  • 29
  • 62
  • That works, but in my case, because i'm instruction another container to perform an request to another container, I have to edit the host location from localhost:8000 to lumen:8000, which doesn't feel right. But thank you for taking your time on a useful answer. – helth Oct 15 '22 at 14:42
  • Can you provide an environment variable to the `frontend` container which will set the target URL to `lumen:8000`? I'd expect this sort of setup in the compose file, it doesn't feel strange at all. Given that each container is on its own IP in the Docker network, it's more accurate to think of them as separate hosts than to rely on the bridging to the host machine using `localhost`. – Nick K9 Oct 15 '22 at 14:48
  • That would make sense, but when i'm make a request to the backend from the frontend via the browser, It can't find the lumen:8000 host? Only via localhost:8000 so right now its set to localhost:8000, and that's because lumen:800 does not work directly from the browser – helth Oct 15 '22 at 15:00
  • I presume the browser is running on the host, so yes: DNS on the host isn't going to understand the DNS hostnames generated by compose for the containers. Does adding [`driver: bridge`](https://docs.docker.com/compose/compose-file/compose-file-v3/#driver-1) to the user-defined `database` network let you use `localhost:8000`? – Nick K9 Oct 15 '22 at 15:12