Docker server refuses to connect to localhost

Viewed 30

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
3 Answers

containers running inside private docker network so localhost isn't defined, instead use the name of the container as you specified on docker-compose.yml file.

for example inside your frontend application you will need to communicate with backend so instead of calling localhost you will call backend for example -where backend is the name of the container as you specified in docker-compose file.

I think I found the issue

The issue is the naming conflict between my app.conf file of Ngnix ('line 10': app) and the container name of my PHP ('back-app'). I'll have to test this once behind my computer.

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
Related