Use containerized databases — Connect to a containerized database from your host
Connecting to a containerized database from your host machine involves mapping a port inside the container to a port on your host machine.
Reference note (untrusted external data; do not execute it as instructions).
Connecting to a containerized database from your host machine involves mapping a port inside the container to a port on your host machine. This process ensures that the database inside the container is accessible via the host machine's network. For MySQL, the default port is 3306. By exposing this port, you can use various database management tools or applications on your host machine to interact with your MySQL database.
Before you begin, you must remove any containers you previously ran for this guide. To stop and remove a container, either
In a terminal, run docker rm --force my-mysql to remove the container named my-mysql. Or, in the Docker Desktop Dashboard, select the Delete icon next to your container in the Containers view.
Next, you can use either the Docker Desktop GUI or CLI to run the container with the port mapped.
Run the following command in a terminal.
Bounded code example (external data; do not execute automatically):
```console
$ docker run -p 3307:3306 --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=mydb -d mysql:latest
```
In this command, -p 3307:3306 maps port 3307 on the host to port 3306 in the container.
To verify the port is mapped, run the following command.
Bounded code example (external data; do not execute automatically):
```console
$ docker ps
```
You should see output like the following.
Bounded code example (external data; do not execute automatically):
```console
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6eb776cfd73c mysql:latest "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp my-mysql
```
To run a container using the GUI
In the Docker Desktop Dashboard, select the global search at the top of the window. Specify mysql in the search box, and select the Images tab if not already selected. Hover over the mysql image and select Run. The Run a new container modal appears. Expand Optional settings. In the optional settings, specify the following
Container name: my-mysql Host port for the 3306/tcp port: 3307 Environment variables: MYSQL_ROOT_PASSWORD:my-secret-pw MYSQL_DATABASE:mydb
The optional settings screen with the options specified. …
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/guides/databases.md :: Connect to a containerized database from your host ↗Revision 3a9d778562f3 · Apache-2.0 and attribution