View Database within a Docker Container
This guide provides an overview of how to view a Postgres or MySQL database running within a container via the CLI without exposing the port.
Postgres
You will need four pieces of information before we start:
- The name of the container. In this example, the name is
postgres-db. - The name of the database user. In this it is
username. - The password of the database user. In this example, it's
password. - The name of the database. In this example, the name is
db-name.
The easiest way to find the name of the container is by running
docker psonce the containers are running.The username and password can generally be found in the
docker-compose.yml,Dockerfile, or.envfile.
From the terminal, run:
docker exec -it postgres-db sh
psql -U username -d db-name
Enter the password if you are prompted.
To verify it worked, run \dt to view the tables.
MySQL
You will need four pieces of information before we start:
- The name of the container. In this example, the name is
mysql-db. - The name of the database user. In this example, it's
username. - The password of the database user. In this example, it's
password. - The name of the database. In this example, the name is
db-name.
The easiest way to find the name of the container is by running
docker psonce the containers are running.The username and password can generally be found in the
docker-compose.yml,Dockerfile, or.envfile.
From the terminal, run:
docker exec -it mysql-db sh
mysql -u username -p
Enter the password if you are prompted.
Next, enter the following to set the database.
USE db-name;
To verify it worked, run SHOW TABLES; to view the tables.