BookStore Docker 1 : Docker Basic With Process of creating MySQL Docker Image and Container
Simply Put,
Through all my docker documentation, I will leave the processes, and my goal is simple "create Docker Container for MySQL, and then link mysql-continaer
to Spring Boot Project
."
Reference Link (MySQL Monitoring)
https://joo.hashnode.dev/book-store-docker-2-mysql-monitoring
Reference Link (Dockerize Spring Boot) :
Docker Containers
Containers are
instances of Docker images
.Containers include
the runtime state of an application
andany data changes
that occur while the container is running.Removing a container means deleting the container instance itself, but it does not affect the underlying Docker image.
Docker Images
Images are the
blueprints
from which containers are created.Images include
the application code
,libraries
, andenvironment settings
needed to run the application.Removing an image will delete the image itself
Removing an image is independent of container removal.
Installation Docker
Docker is an open platform for developing, shipping, and running applications.
Install Docker Desktop: https://docs.docker.com/get-docker/#installation
To run Windows containers, We need more than the Windows 10 version.
Docker Installation Link : https://docs.docker.com/desktop/install/windows-install/
Install Docker Destop On Windows
: By default, Docker Desktop is installed at C:\Program Files\Docker\Docker
(I denied,, so I couldn't proceed.
I selected recommended settings
After Login
Create Container For MySQL
1.Pull the MySQL Image:
Open a terminal or command prompt.
Run the following command to pull the latest MySQL image from Docker Hub:
docker pull mysql:latest
--> Pulled image successfully.
2.(Optional) To view a summary of image vulenabilities and recommendations
: checking vulnerabilities
docker scout quickview mysql:latest
3.Create the MySQL Docker container with port mapping:
To run the container, Use the following command
To start your MySQL Docker container and map the container's MySQL port (3306) to your host's port (13306):
MySQL Port : 3306 --> host's port 13306
docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=yourPassWord -p 13306:3306 -d mysql:latest
Explanation of the command
: --name mysql-container
: Names the container mysql-container
.
-e MYSQL_ROOT_PASSWORD=yourPassword
use password you set-p 13306:3306
: Maps port 13306 on your host to port 3306 in the container.-d mysql:latest
: Runs the container in detached mode using the latest MySQL image.
(Allow when promted)
Live Check MySQL State docker ps (if mysql-container runs well)
Verify that your MySQL container is running by using the the following command:
docker ps
You should see your
mysql-container
listed.
4.Connect to the MySQL instance & Access it using cmd
After you check running docker, you can connect MySQL Instance using the 2 following command.
Similar purposes but operate differently:
\=> I guess you can use anything you want.
(I just added for reference and practice using Docker.)
Command 1: Connect to MySQL within a Running Container
docker exec -it mysql-container mysql -u root -p
docker exec -it mysql-container
: This part runs a command inside an already running Docker container namedmysql-container
.mysql -u root -p
: This runs the MySQL client within that container, connecting to the MySQL server inside the same container.Use Case: Useful when you want to interact with the MySQL server directly inside the container.
Successfully logged in With right password.
: You can monitor MySQL
When Failed with wrong password
-> Access denied.
**Command 2:**Connect to Your Local Machine's MySQL with Specific IP and Port
mysql -h 127.0.0.1 -P 13306 -u root -p
This command runs the MySQL client on your local machine (my case is windows)
-h 127.0.0.1 -P 13306
: It connects to a MySQL server running at IP address127.0.0.1
(localhost) and port13306
.Use Case: Useful when you want to connect to a MySQL server that is running either directly on your local machine or inside a Docker container with port forwarding configured.
Hostname: 127.0.0.1 (
BookStore
db connection)Port: 13306
Username: root
Password: yourPassWord (I used for creating a docker container)
Enter the passwordyourPassWord
when prompted.
\==> This password is the password that i set while creating Docker container.............Frankly, I thought this is MySQL's root password......................I've wasted of time.
How to Stop Docker Container?
: Whenever I run the docker, I can use different access password.
Exit the MySQL Monitor first
: After accessing mysql-container, I can monitor mysql
First, exit the MySQL monitor if you're still inside it:
EXIT
Stop a running container:
docker stop <container_name_or_id>
==> Based on above
docker stop mysql-container
check docker state
Check Docker Log again (optional)
docker stop <container_name_or_id>
==> Based on above
docker logs mysql-container
Remove a stopped container:
docker stop <container_name_or_id>
==> Based on above
docker rm mysql-container
\==> Removing a stopped container does not remove the Docker image.
(Docker Image
is like blueprint
, Container
is the state of runtime application
.)
Create a New Container:
docker run --name mysql-newContainer -e MYSQL_ROOT_PASSWORD=121212 -p 13306:3306 -d mysql:latest
How to remove Docker's Image?
List Docker Images
First, identify the image you want to remove. You can see lists of Docker's Image using the following command
docker images
This will display a list of images, showing their repository names, tags, and image IDs.
Remove a Docker Image
Once you have the image ID or name, you can remove the image using:
docker rmi <image_id_or_name>
Replace <image_id_or_name>
with the actual ID or name of the image. For example, to remove the mysql:latest
image, you can use:
docker rmi mysql:latest
How to Restart the Stopped Container?
: which means, it's stopped but still existed.
Restart the Stopped Container
check docker state (not running)
After restarting, you can then access the MySQL server:
To restart the stopped container and then access it.
docker start <container name>
==> based on this
docker start mysql-container
After running stopped container again.
Access MySQL Inside the Container:
docker exec -it mysql-container mysql -u root -p
when prompted password -> Enter the password you set for running container
: Again, you can monitor MySQL
MySQL Logs cmd
Sometimes the MySQL server logs inside the container can provide hints if there are connection issues. To view logs:
docker logs mysql-container
Check Container Status : docker ps +@
(docker ps --a --filter ~~)
check the status of the mysql-container
to see if it’s running or stopped:
docker ps -a --filter "name=mysql-container"
docker ps
Check Network and Port Configuration
Ensure that Docker is correctly mapping the ports. You’ve confirmed that port 13306
is listening on the host, which is good.
netstat -an | find "13306"
Error
Typo
ERROR 2003 (HY000)
Connect to the MySQL instances
If you use below command, you'd encounter ERROR 2003
docker run -it --rm mysql:latest mysql -h127.0.0.1 -P13306 -u root -p
With the cmd above, we encounter ERROR 2003
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:13306' (111)
Instead, Use Command below
Connecting to a MySQL instance running on my local machine at IP 127.0.0.1
mysql -h 127.0.0.1 -P 13306 -u root -p
Subscribe to my newsletter
Read articles from Byung Joo Jeong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by