Cloud Native 6 min read

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.

Lobster Programming
Lobster Programming
Lobster Programming
Master Docker: From Basics to Running MySQL in Containers

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.

Docker architecture illustration
Docker architecture illustration

Containers share images, allowing developers to run services with a single command after downloading the image.

Container vs VM comparison
Container vs VM comparison

2. Installing and Using Docker

2.1 Install Docker

Step 1: Install dependencies

yum -y install gcc
yum -y install gcc-c++

Step 2: Install Docker yum install -y docker Step 3: Configure Docker daemon

vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://9cpnqwer.mirror.aliyuncs.com"]
}

Configuring a registry mirror speeds up image pulls.

Step 4: Start, restart, stop Docker and enable on boot

# Start / restart / stop Docker
systemctl start docker
systemctl restart docker
systemctl stop docker
# Enable Docker to start on boot
systemctl enable docker

Step 5: Verify Docker is running (see image below)

Docker test output
Docker test output

Docker is now installed successfully.

2.2 Practical: Deploy MySQL with Docker

Pull the MySQL image (specify version if needed)

# Pull MySQL 5.7 image
docker pull mysql:5.7

Run the container with required options

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

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:

docker ps

3. Common Docker Commands

(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

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsLinuxmysqlContainers
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.