Home > Software design >  Issue/understanding problems with docker-compose networks
Issue/understanding problems with docker-compose networks

Time:10-16

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:

CodePudding user response:

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.

  • Related