To connect a database running in a Docker container to a database viewer (like DBeaver, HeidiSQL, etc.), follow these general steps.
Start Your Database Container
First, ensure that your database container is running. For example, if you are using PostgreSQL, you might start the container with.
docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
In this command:
-e POSTGRES_PASSWORD=mysecretpassword
sets the password for thepostgres
user.-p 5432:5432
maps port 5432 of the container to port 5432 on your host.
Find the Host IP Address
If you're running Docker on Linux, you can usually connect to localhost
. If you’re using Docker on Windows or macOS (with Docker Desktop), you can also use localhost
.
To verify, you can run.
docker inspect my_postgres | grep IPAddress
Open Your Database Viewer
- Launch your database viewer (e.g., DBeaver).
- Create a new connection:
- Choose the appropriate database type (e.g., PostgreSQL, MySQL, etc.).
- Input the connection details:
- Host:
localhost
(or the IP address from the previous step) - Port:
5432
(or the port you mapped) - Database: Name of the database (e.g.,
postgres
) - Username:
postgres
(or the relevant username) - Password: The password you set (e.g.,
mysecretpassword
)
- Host:
Test the Connection
Most database viewers have a "Test Connection" button. Click it to ensure that everything is set up correctly. If there are issues, check:
- Container status (
docker ps
) - Correct port mapping
- Firewall settings
Connect
Once the connection is successfully tested, you can save the configuration and connect to your database.