🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
Syntax:
docker exec -it [CONTAINER_ID_OR_NAME] /bin/bashHere'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:latestStep 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:latestIn 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 bashStep 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 -pWhen 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":
CREATE DATABASE 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:
exitAnd to exit the container's shell, just type exit again.
Tip
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.
Conclusion
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
- Docker Create Container
- Docker Stop All Containers
- Docker Remove All Stopped Containers
- Docker Start Container
- Docker Restart All Containers
- Docker Go Inside Container - The docker exec Command
- Docker List Containers
- Docker Fetching Logs from Containers
- Docker Rename Container
- Docker Remove Unused Containers
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment