Docker Go Inside Container - The docker exec Command

In this guide, we will explore how to go inside a Docker container using the docker exec command. To demonstrate this, we'll launch a MySQL container using Docker and then give into that container to create a database.

Why Enter a Container? 

The reasons for diving into a container can vary: 

Debugging: If an app isn't behaving as expected, getting inside its container can help diagnose the problem. 

Configuration: Occasionally, one might need to tweak settings or configurations directly inside a container. 

Exploration: For those new to Docker, entering a container can provide a hands-on learning experience, showcasing the environment a Dockerized app runs in. 

The docker exec Command 

The docker exec command allows users to run specific commands inside a running container, and with the right flags, it can give you an interactive shell inside the container.


docker exec -it [CONTAINER_ID_OR_NAME] /bin/bash

Here's the breakdown: 

docker exec: This tells Docker we want to execute a command in a specific container. 

-it: These flags enable an interactive terminal session. The -i stands for "interactive" and -t allocates a pseudo-terminal. 

[CONTAINER_ID_OR_NAME]: Replace this with the actual ID or name of the container you want to enter. 

/bin/bash: This runs the bash shell inside the container. 

Step-By-Step Example

Step 1: Pulling the MySQL Docker Image 

If you haven't already, start by pulling the latest MySQL image from Docker Hub:

docker pull mysql:latest

Step 2: Running a MySQL Container 

Next, you'll want to create and run a MySQL container. Note that you'll need to provide a root password which is essential for accessing the MySQL server:

docker run --name my-mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

In the command above: 

--name my-mysql-container: Name of the container. 

-e MYSQL_ROOT_PASSWORD=my-secret-pw: Sets the root password to "my-secret-pw".Change this to something more secure in a production environment. 

-d mysql:latest: Runs the container in detached mode using the latest MySQL image.

Step 3: Accessing the Container's Bash Shell 

Often, it's useful to enter a Docker container's shell, especially for debugging or maintenance. Use the following command to access the bash shell of the MySQL database container:

docker exec -it my-mysql-container bash

Step 4: Accessing MySQL Inside the Container 

Once you're inside the container's shell, you can log into MySQL using the root password:

mysql -u root -p

When prompted, enter the password (my-secret-pw in our example).

Step 5: Creating a Database 

With access to MySQL, creating a database is straightforward. Let's say you want to create a database named "sampleDB":


After executing the command, you'll receive a confirmation message that the database has been created. 

Step 6: Exiting MySQL and the Container 

To exit MySQL, type:


And to exit the container's shell, just type exit again.


While it can be informative (and fun) to dive into containers, remember that changes made inside a running container are ephemeral. If the container is deleted and recreated from its image, your changes will be lost. Always make persistent changes in the Dockerfile or in mounted volumes. 


In this guide, we have explored how to launch a MySQL container using Docker and then dive into that container to create a database using the docker exec command.

Related Container Management Guides