Master Docker: From Basics to Running MySQL in Containers
This guide introduces Docker fundamentals, explains how containers differ from virtual machines, walks through installing Docker on Linux, and demonstrates practical steps to pull and run a MySQL container, while also providing a handy reference of common Docker commands for managing containers and images.
1. Understanding Docker
Docker can be thought of as a large ship that carries applications in separate containers, similar to cargo boxes. Each container runs an independent service such as Redis or MySQL without needing a full operating system, making deployment fast and lightweight compared to virtual machines.
Containers share images, allowing developers to run services with a single command after downloading the image.
2. Installing and Using Docker
2.1 Install Docker
Step 1: Install dependencies
<code>yum -y install gcc
yum -y install gcc-c++</code>Step 2: Install Docker
<code>yum install -y docker</code>Step 3: Configure Docker daemon
<code>vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://9cpnqwer.mirror.aliyuncs.com"]
}</code>Configuring a registry mirror speeds up image pulls.
Step 4: Start, restart, stop Docker and enable on boot
<code># Start / restart / stop Docker
systemctl start docker
systemctl restart docker
systemctl stop docker
# Enable Docker to start on boot
systemctl enable docker</code>Step 5: Verify Docker is running (see image below)
Docker is now installed successfully.
2.2 Practical: Deploy MySQL with Docker
Pull the MySQL image (specify version if needed)
<code># Pull MySQL 5.7 image
docker pull mysql:5.7</code>Run the container with required options
<code>docker run -p 3306:3306 --name mysql --restart=always --privileged=true \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/conf:/etc/mysql \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=longxiabiancheng \
-d mysql:5.7</code>Explanation of flags:
-p : maps container port to host port.
--name : assigns a name to the container.
--restart : ensures the container starts with Docker.
-v : mounts host directories for logs, data, and configuration, enabling persistence.
-d : runs the container in detached (background) mode.
-e : sets environment variables, such as the root password.
--privileged : grants the container root privileges on the host.
Check the running container:
<code>docker ps</code>3. Common Docker Commands
<code>(1) docker ps # list running containers
(2) docker ps -a # list all containers
(3) docker stop <container_id_or_name>
(4) docker start <container_id_or_name>
(5) docker rm <container_id_or_name>
(6) docker rm -f <container_id_or_name>
(7) docker exec -it <container_id_or_name> /bin/bash
(8) docker-compose up -d
(9) docker-compose down
(10) docker rmi -f <image_id> # force delete image</code>Summary
Docker simplifies the installation of common services like MySQL and Redis, and when many services need to be orchestrated, Docker Compose or Kubernetes can be used for higher‑level management and scheduling.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.