Search code examples
dockerdocker-composedocker-network

Docker server refuses to connect to localhost


Ever since I specified a defined bridge network to my docker-compose file my server refuses to curl with localhost. I've tried a number of options but just cannot seem to make it work. Astonishingly enough the phpadmin - localhost:8081 works - so how do they do it.

All I want to create is an Php-Api in one docker-compose file and the frontend in a second using the same network {scorpionet} What Am I doing wrong?

So I'm not sure if this is allowed buthere goes. You can find the full code on my github repo

version: "3"

services:
  back-app:
    container_name: back-app
    build:
      context: ./dockerfiles
      dockerfile: php.dockerfile
    image: site
    restart: unless-stopped
    volumes:
      - ./site:/var/www
    networks:
      - scorpionet

  back-server:
    container_name: back-server
    build:
      context: ./dockerfiles
      dockerfile: nginx.dockerfile
    image: nginx:alpine
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./site:/var/www
      - ./config/nginx/:/etc/nginx/conf.d/
    depends_on:
      - back-app
      - back-db
    networks:
      - scorpionet

  back-db:
    container_name: back-db
    platform: linux/x86_64
    image: mysql:8
    env_file:
      - ./env/db.env
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - scorpionet

  back-phpmyadmin:
    container_name: back-phpmydmin
    image: phpmyadmin/phpmyadmin
    env_file:
      - ./env/phpmyadmin.env
    ports:
      - "8081:80"
    networks:
      - scorpionet

volumes:
  dbdata:
    driver: local

networks:
  scorpionet:
    name: scorpionet
    driver: bridge

Solution

  • WORKING - cool

    So after a few bug fixes discovered that Nginx could find its conf file.
    So modified the docker-compose file.
    Also, discovered declaring your networks first before using them seems to work better or is it just me?

    
    version: "3"
    
    networks:
      scorpionet:
        
    services:
      backapp:
        container_name: backapp
        build:
          context: ./dockerfiles
          dockerfile: php.dockerfile
        image: site
        restart: unless-stopped
        volumes:
          - ./site:/var/www
        ports:
          - "127.0.0.1:9000:9000"
        networks:
          - scorpionet
    
      backserver:
        container_name: backserver
        build:
          context: ./dockerfiles
          dockerfile: nginx.dockerfile
        image: nginx:stable-alpine
        restart: unless-stopped
        ports:
          - "127.0.0.1:9080:80"
        volumes:
          - ./site:/var/www
          - ./dockerfiles/nginx/default.conf:/etc/nginx/conf.d/default.conf
    
        networks:
          - scorpionet
    
      backdb:
        container_name: backdb
        platform: linux/x86_64
        image: mysql:8
        env_file:
          - ./env/db.env
        ports:
          - "127.0.0.1:9306:3306"
        volumes:
          - dbdata:/var/lib/mysql
        networks:
          - scorpionet
    
      backphpmyadmin:
        container_name: backphpmydmin
        image: phpmyadmin/phpmyadmin
        env_file:
          - ./env/phpmyadmin.env
        ports:
          - "127.0.0.1:9081:80"
        networks:
          - scorpionet
    
    volumes:
      dbdata:
        driver: local