Skip to content

Instantly share code, notes, and snippets.

@NovemberDev
Created September 14, 2021 20:52
Show Gist options
  • Save NovemberDev/600c28377808247dfed366d975b49fd6 to your computer and use it in GitHub Desktop.
Save NovemberDev/600c28377808247dfed366d975b49fd6 to your computer and use it in GitHub Desktop.
Run a postgres db really quick in a docker container (cmd)
docker network create -d bridge internal-network
docker pull postgres
docker run --network=internal-network --name postgres-db --publish 5432:5432 -e POSTGRES_PASSWORD=root -d postgres
@NovemberDev
Copy link
Author

NovemberDev commented Sep 14, 2021

If you compose your other containers with this option in the docker-compose.override.yml or docker-compose.yml file:

networks:
    default:
        external: true
        name: internal-network

Your connection string from within another docker container (which accesses the db from this new container) will be:

"Host=host.docker.internal;Port=5432;Database=database;Username=postgres;Pwd=root;"
Your connection string from "outside" aka the host machine (with pgAdmin) will be:

"Host=127.0.0.1;Port=5432;Database=postgres;Username=postgres;Password=root;"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment