0

I have some docker containers such as php, nginx, etc. A also have postgres locally because I have learned that dabatase inside docker container is a bad practise. But I can't connect to local postgres from docker container.

At this moment I have done next

In postgresql.conf I changed listen_addresses

listen_addresses = '*'

In pg_hba.conf I have added next line

host    all     all     0.0.0.0/0       md5

The I executed next command for iptables

iptables -I INPUT -p tcp -m tcp -s 0.0.0.0 --dport 5432 -j ACCEPT

Then I restarted postgres.

My database configuration

DB_CONNECTION=pgsql
DB_HOST=my_server_ip_address
DB_PORT=5432
DB_DATABASE=mydbname
DB_USERNAME=mydbuser
DB_PASSWORD=mydbpasswd

But i still can't connect to posgresql. At the same moment I can connect to postgres via psql or phpstorm

My docker-compose.yml

version: '3.7'

networks:
  backend-network:
    driver: bridge
  frontend-network:
    driver: bridge

services:
  &app-service app: &app-service-template
    container_name: k4fntr_app
    build:
      context: ./docker/php-fpm
      args:
        UID: ${UID?Use your user ID}
        GID: ${GID?Use your group ID}
        USER: ${USER?Use your user name}
    user: "${UID}:${GID}"
    hostname: *app-service
    volumes:
      - /etc/passwd/:/etc/passwd:ro
      - /etc/group/:/etc/group:ro
      - ./:/var/www/k4fntr
    environment:
      APP_ENV: "${APP_ENV}"
      CONTAINER_ROLE: app
      FPM_PORT: &php-fpm-port 9000
      FPM_USER: "${UID:-1000}"
      FPM_GROUP: "${GID:-1000}"
    networks:
      - backend-network

  &queue-service queue:
    <<: *app-service-template
    container_name: k4fntr_queue
    restart: always
    hostname: *queue-service
    depends_on:
      - app
    environment:
      CONTAINER_ROLE: queue

  &schedule-service schedule:
    <<: *app-service-template
    container_name: k4fntr_schedule
    restart: always
    hostname: *schedule-service
    depends_on:
      - app
    environment:
      CONTAINER_ROLE: scheduler

  &sportlevel-listener sportlevel_listener:
    <<: *app-service-template
    container_name: k4fntr_sl_listener
    restart: always
    hostname: *sportlevel-listener
    ports:
      - "${SPORTLEVEL_LISTEN_PORT}:${SPORTLEVEL_LISTEN_PORT}"
    depends_on:
      - app
    environment:
      CONTAINER_ROLE: sl_listener

  &php-fpm-service php-fpm:
    <<: *app-service-template
    container_name: k4fntr_php-fpm
    user: 'root:root'
    restart: always
    hostname: *php-fpm-service
    ports: [*php-fpm-port]
    entrypoint: /fpm-entrypoint.sh
    command: php-fpm --nodaemonize
    networks:
      - backend-network
      - frontend-network

  echo-server:
    container_name: k4fntr_echo
    image: oanhnn/laravel-echo-server
    volumes:
     - ./:/app
    environment:
      GENERATE_CONFIG: "false"
    depends_on:
      - app
    ports:
      - "6001:6001"
    networks:
      - backend-network
      - frontend-network

  nginx:
    container_name: k4fntr_nginx
    image: nginx
    volumes:
    - ./docker/nginx/config:/etc/nginx/conf.d
    - ./:/var/www/k4fntr
    depends_on:
      - *php-fpm-service
    ports:
      - "${NGINX_LISTEN_PORT}:80"
    networks:
      - frontend-network

  redis:
    container_name: k4fntr_redis
    image: redis
    restart: always
    command: redis-server
    volumes:
      - ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
      - ./docker/redis/redis-data:/data:rw
    ports:
      - "16379:6379"
    networks:
      - backend-network
Viktor
  • 1,532
  • 6
  • 22
  • 61
  • Why is running a Postgresql instance in a container "bad practice"? Anyway - https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – madflow Mar 17 '20 at 10:25
  • I have read this topic, but no one from these advice doesn't help me. I can't understand why – Viktor Mar 17 '20 at 10:34
  • So you tried --network="host" ? – madflow Mar 17 '20 at 10:43
  • If you look at my docker-compose.yml you can see that nginx container has already used network which is called fronted-network. When I try to create new network with the driver "host" I got an error. I also got an error if I try to add "host: to networks near the frontend-network – Viktor Mar 17 '20 at 10:54

0 Answers0