没有不值得去解决的问题,也没有不值得去学习的技术!

Configure a Development Environment: Manage Multiple MySQL and Redis Versions With Docker

At this point, all three database containers are running normally.

This article is the development environment chapter of the “From Windows to Ubuntu: Complete Migration and Configuration Guide” series.
It is intended for developers who have already installed Ubuntu and want to build an efficient, isolated, multi-version database environment.


Why use Docker to manage databases?

On Windows, I was used to running independent database containers using WSL2 + Docker, and I naturally continued this approach after migrating to Ubuntu. Compared to installing directly on the host machine:

  • Multi-version coexistence: Run MySQL 5.7 and 8.0 simultaneously on the same machine, along with any version of Redis, without mutual interference.
  • Environment isolation: Each project’s database is independent or shared by version, preventing configuration conflicts.
  • Lightweight and easy to clean up: Stopping a container immediately releases resources, data volumes are retained, and deletion leaves no residue.
  • Version locking: The mysql:5.7 image always corresponds to the latest 5.7 release and will not change automatically due to system upgrades.

This article uses MySQL 5.7, MySQL 8.0, and Redis 7.2 as examples to set up three independent containers for subsequent Go/PHP projects to share.


1. Install Docker Engine (Command Line Only, No Docker Desktop)

Ubuntu’s “Software” or “App Center” cannot find Docker Desktop. This is a characteristic of software distribution in Linux distributions—professional tools usually require manually adding official software repositories. Installing from the official repository is lighter and more stable.

1. Uninstall conflicting old packages (skip for a fresh system)

Bash
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
  sudo apt remove -y $pkg
done

If you are on a fresh installation of Ubuntu 26.04, these packages do not exist yet, so you can skip this step.

2. Add Docker’s official GPG key and repository

Bash
sudo apt update
sudo apt install -y ca-certificates curl

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl --http1.1 -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt update

Troubleshooting note: If curl reports curl: (16) Error in the HTTP2 framing layer while downloading the GPG key, adding the --http1.1 parameter resolves the issue.

3. Install Docker Engine and the Compose plugin

Bash
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

4. Add the current user to the docker group (to run without sudo)

Bash
sudo usermod -aG docker $USER

You must log out and log back in (or reboot), otherwise the docker command will still require sudo.

5. Verify the installation

Bash
docker --version          # 输出如 Docker version 29.5.2
docker compose version    # 输出 Docker Compose version v2.x

2. Network and Image Pull Troubleshooting (Real-World Record)

In a VPN environment, I encountered a network timeout when pulling the first test image. Below is the complete troubleshooting and resolution process.

2.1 First attempt: Run hello-world directly

Bash
docker run hello-world

Output:

Plaintext
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: failed to resolve reference "docker.io/library/hello-world:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/hello-world/manifests/latest": dial tcp 128.242.240.85:443: i/o timeout

2.2 Restart the Docker service and try again

Bash
sudo systemctl restart docker
docker run hello-world

It still timed out, but this time the IP changed to 31.13.69.245:

Plaintext
dial tcp 31.13.69.245:443: i/o timeout

2.3 Check if the host can access Docker Hub

Bash
curl -I https://registry-1.docker.io/v2/

Output:

Plaintext
HTTP/1.1 200 Connection established

HTTP/2 401 
date: Sun, 31 May 2026 10:04:04 GMT
content-type: application/json
content-length: 87
...

This indicates that the host network is normal (the 401 response is due to a lack of authentication, but the connection was established). The problem lies with the Docker daemon itself.

2.4 Configure a domestic image accelerator (first attempt)

Create /etc/docker/daemon.json, using the USTC, NetEase, and Baidu mirrors:

Bash
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}
EOF
sudo systemctl daemon-reload
docker run hello-world

The result still timed out (the IP changed to 103.97.3.19):

Plaintext
dial tcp 103.97.3.19:443: i/o timeout

2.5 Restart Docker again, problem persists

Bash
sudo systemctl restart docker
docker run hello-world

This time the error changed:

Plaintext
dial tcp: lookup docker.mirrors.ustc.edu.cn on 127.0.0.53:53: no such host

This means the image accelerator’s domain could not be resolved — the VPN was affecting DNS.

2.6 Final solution: Use DaoCloud mirror + specify public DNS

Modify /etc/docker/daemon.json to configure both the mirror and DNS:

Bash
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://docker.m.daocloud.io"],
  "dns": ["8.8.8.8", "1.1.1.1"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
docker run hello-world

This time, the pull and run finally succeeded:

Plaintext
latest: Pulling from library/hello-world
4f55086f7dd0: Pull complete 
d5e71e642bf5: Download complete 
Digest: sha256:0e760fdfbc48ba8041e7c6db999bb40bfca508b4be580ac75d32c4e29d202ce1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
...

Conclusion: In a VPN environment, the Docker daemon might not correctly use the system DNS. You need to explicitly specify a public DNS (like 8.8.8.8) and pair it with a working image accelerator. The DaoCloud mirror docker.m.daocloud.io works in this environment.


3. Deploy MySQL 5.7 / 8.0 and Redis 7.2 Containers

3.1 Create the project directory

Bash
mkdir -p ~/docker/services
cd ~/docker/services

3.2 Write docker-compose.yml

Bash
cat > docker-compose.yml << 'EOF'
version: "3.8"      # 新版 Docker Compose 可省略该行,会有警告但无影响

networks:
  dev-network:
    driver: bridge

services:
  mysql57:
    image: mysql:5.7
    container_name: mysql57
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: mystrongpwd57
    ports:
      - "3306:3306"
    volumes:
      - ./data/mysql57:/var/lib/mysql
    networks:
      - dev-network

  mysql80:
    image: mysql:8.0
    container_name: mysql80
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: mystrongpwd80
    ports:
      - "3307:3306"
    volumes:
      - ./data/mysql80:/var/lib/mysql
    networks:
      - dev-network
    command: --default-authentication-plugin=mysql_native_password

  redis:
    image: redis:7.2
    container_name: redis72
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis72:/data
    command: redis-server --requirepass myredispass --appendonly yes
    networks:
      - dev-network
EOF

Pitfall reminder: MySQL 8.0 requires adding --default-authentication-plugin=mysql_native_password, otherwise some older clients will encounter an caching_sha2_password error when connecting.

3.3 First start (encountering network fluctuation)

Bash
docker compose up -d

The output shows:

Plaintext
WARN[0000] the attribute `version` is obsolete...
[+] up 19/24
 ✘ Image mysql:8.0  Error failed to resolve reference ... EOF

The error was caused by an authentication failure (EOF) when pulling MySQL 8.0, likely due to a temporary issue with the image accelerator.

3.4 Retry directly, success

Bash
docker compose up -d

This time, all images were pulled successfully:

Plaintext
[+] up 25/34
 ✔ Image mysql:5.7   Pulled
 ✔ Image redis:7.2   Pulled
 ✔ Image mysql:8.0   Pulled
 ✔ Network services_dev-network Created
 ✔ Container mysql57 Started
 ✔ Container redis72 Started
 ✔ Container mysql80 Started

Lesson learned: Transient network issues with Docker Hub or image accelerators can often be resolved by simply retrying once.


4. Verify Container Status and Test Connections

4.1 Check container status

Bash
docker ps

Output:

Plaintext
CONTAINER ID   IMAGE       COMMAND                   STATUS          PORTS                                                    NAMES
9ec38ca384b6   redis:7.2   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   0.0.0.0:6379->6379/tcp, [::]:6379->6379/tcp              redis72
4e93fd2e2c3f   mysql:8.0   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp, [::]:3307->3306/tcp   mysql80
29598dd51087   mysql:5.7   "docker-entrypoint.s…"   45 seconds ago   Up 42 seconds   0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp   mysql57

4.2 Connect to MySQL 5.7

Bash
docker exec -it mysql57 mysql -uroot -pmystrongpwd57

Enter the MySQL command line, and execute exit to exit.

4.3 Connect to MySQL 8.0

Bash
docker exec -it mysql80 mysql -uroot -pmystrongpwd80

Successfully entered as well.

4.4 Connect to Redis

Bash
docker exec -it redis72 redis-cli -a myredispass

The output shows the Warning: Using a password with '-a' ... may not be safe warning (just a security warning that does not affect usage), and then enters the 127.0.0.1:6379> prompt.

At this point, all three database containers are running normally.

At this point, all three database containers are running normally.

5. Connect from Project Containers (Network Interoperability)

The database containers are located on the custom network services_dev-network (automatically created by docker-compose). Project containers only need to join the same external network to access them via container name.

Example docker-compose.yml snippet:

YAML
networks:
  default:
    external:
      name: services_dev-network

services:
  go-app:
    image: golang:1.26-alpine
    networks:
      - default
    environment:
      DB_HOST: mysql57         # 或 mysql80
      REDIS_HOST: redis72

In your code, simply connect to mysql57:3306 and redis72:6379.


6. Version Selection Notes and Precautions

ServiceVersionPopularityLifecycle Reminder
MySQL 5.75.7.44Accounts for about 18.8%, a common version for legacy systemsReached EOL in October 2023, no longer receives official security updates, not recommended for new projects unless necessary
MySQL 8.08.0.46Currently the most mainstream (58% share)Official support ends on April 30, 2026, requires upgrading to 8.4 LTS afterwards
Redis 7.27.2.xStable and matureBase version for the open-source Valkey fork, safe to use

For new projects, it is recommended to use MySQL 8.4 LTS (supported until 2031) and Redis 7.2 directly.


7. Daily Management Command Cheat Sheet

ActionCommand
Start all servicescd ~/docker/services && docker compose up -d
Stop all servicescd ~/docker/services && docker compose down
Restart only MySQL 8.0docker restart mysql80
View MySQL 5.7 logsdocker logs -f mysql57
Enter Redis command linedocker exec -it redis72 redis-cli -a 密码
Clean up unused images/containersdocker system prune -a
Back up data volumestar -czf mysql57-backup.tar.gz ~/docker/services/data/mysql57

8. Summary

At this point, we have completed a clean, reusable database container environment:

  • MySQL 5.7 → Port 3306
  • MySQL 8.0 → Port 3307
  • Redis 7.2 → Port 6379

All data is persisted in ~/docker/services/data/, and the containers start automatically with the system. Any subsequent Go, PHP, or Node.js project can quickly gain multi-version database support simply by connecting to these container names via the Docker network.


All commands in this article have been verified on Ubuntu 26.04, with Docker version 29.5.2.
If you have any questions or want to discuss, feel free to leave a comment below.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系