I have an asp.net (version 8) webapi project that I have set up to work with docker.
I have the container that runs my API and a volume where I put my database.
It works and I’m able to run API endpoints to get my data from the postgresql database.
I want to be able to view the database running in the volume though.
How can I do this?
I’m using a Visual Studio Code plugin called PostgreSQL (Postgresql management tool by Chris Kolkman) and I can connect to the postgresql database I installed on my operating system but not the one that is running on my docker volume.
Here is my docker-compose.yml file
services:
#asp.net web application
webapp:
container_name: shop-back-api
build:
context: .
dockerfile: Dockerfile
ports:
- "5999:5999"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- Database__ConnectionStrings__DefaultConnection=Host=db;Port=5432;Database=shopback;Username=postgres;Password=XXXXX
#postreSQL database
db:
container_name: shop-back-db
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_DB: shopback
POSTGRES_USER: postgres
POSTGRES_PASSWORD: XXXXX
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
I also have the "Docker" extension installed and I can view my images, containers, and volumes from Visual Studio Code but I’m not sure if this can help me as it doesn’t give me access to the database running on my volume.
2
Answers
All I had to do was uninstall the postgresql server installed on my OS and I was then able to connect to the postgresql server running on the Docker volume right away.
I have tried to access a Postgresql database in a Docker container from Visual Studio Code and it works correctly.
The only thing to keep in mind is the Postgres configuration. Consider enabling the IP where the database listens. Inside the container, make sure that PostgreSQL is configured to accept connections from any address (listen_addresses = ‘*’ in postgresql.conf and setting the access rules in pg_hba.conf).
And if you already have Postgresql running on your computer, consider changing the access port so that there are no conflicts, since you will have to access with the address 127.0.0.1:5432