How to connect a socket with Socket.io in a Docker container?

Although this is an old question I figured I would elaborate a little bit on it for anyone else that is wondering how you would go about connecting containers since the previous answer was a bit slim.

Using swarm in this case would be overkill especially for something like running the containers locally in a manner that would allow them to talk. Instead you simply want to establish the containers on the same docker network.

version: '3.5'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: app
    ports:
      - "4000:4000"
    volumes:
      - .:/app
    networks:
      - app-network

  pgsql:
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  postgres_data:
    driver: local

In the docker-compose.yml file example above you can see that I am defining a network via:

networks:
      app-network:
        driver: bridge

Then on both the app and pgsql containers I am assigning them to that network via:

networks:
      - app-network

This allows me to access the containers from one another via the container "name". So in my code I am now able to use pgsql:5432 and communicate with the postgres service. The app container is also reachable from the pgsql container via app:4000.

While this can get much more complex than the above example I figured I leave a working docker-compose.yml example above. You can find out more about docker networks at https://docs.docker.com/compose/networking/