Home > Software engineering >  how to connect local symfony project to remote docker service
how to connect local symfony project to remote docker service

Time:08-25

I have a dockerized development environment in a remote server :

iig_docker_postgres_adminer             entrypoint.sh docker-php-e ...   Up      0.0.0.0:8081->8080/tcp
iig_docker_postgres_nginx               /docker-entrypoint.sh ngin ...   Up      443/tcp, 0.0.0.0:8080->80/tcp
iig_docker_postgres_php                 docker-php-entrypoint php-fpm    Up      9000/tcp
iig_docker_postgres_postgres_database   docker-entrypoint.sh postgres    Up      0.0.0.0:5432->5432/tcp

This development stack is available using this url :

http://syy0ana001.hpmetier.sf.intra.laposte.fr:8080

My postgres Dockerfile :

FROM postgres:12

My docker compose file :

  database:
    container_name: ${COMPOSE_PROJECT_NAME}_postgres_database
    build:
      context: ./postgres
    hostname: database
    restart: unless-stopped
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: dbtest
    ports:
      - "${POSTGRES_PORT}:5432"
    volumes:
      - ./postgres/local_pgdata:/var/lib/postgresql/data
    networks:
      - symfony

POSTGRES_PORT=5432

Now, i'm trying to connect to postgres database from local symfony project .

I tried :

DATABASE_URL="postgresql://root:124524..@http://syy0ana001.hpmetier.sf.intra.laposte.fr:5432/dbtest?serverVersion=12&charset=utf8

It doesn't work,

Then i inspected the postgres volume :

docker inspect iig_docker_postgres_postgres_database

I got :

"Networks": {
                "symfony": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "database",
                        "ecc337072ae2"
                    ],
                    "NetworkID": "305da1272e2238216593c6c44352aa7958c1f958240487aece8c4223e133ea45",
                    "EndpointID": "e0ea773bc69025d1796ca4fff54adb3a6859e0de7afdb42619734893fab98883",
                    "Gateway": "192.168.96.1",
                    "IPAddress": "192.168.96.5",
                    "IPPrefixLen": 20,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:60:05",
                    "DriverOpts": null
                }

Then i tried :

DATABASE_URL="postgresql://user:[email protected]:5432/dbtest?serverVersion=12&charset=utf8"

volumes:
  symfony_dockerized_app_sync:
  db_app:


networks:
  symfony:
    driver: bridge
    external: true

It doesn't work,

how can i correctly connect to remote docker service from my local machine ?

Thank you !

CodePudding user response:

If I read this line correctly:

iig_docker_postgres_postgres_database   docker-entrypoint.sh postgres    Up      5432/tcp, 0.0.0.0:5432->8084/tcp

I would say the problem is in your docker-compose.yml. You are mapping host port 5432 to container internal port 8084. Does your postgres instance in the container run on port 8084?

Your container also exposes port 5432 but you are not mapping that one.

After you check this, make sure that the port 5432 is open in the firewall of your server and everything should work.

  • Related