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.7image 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)
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt remove -y $pkg
doneIf 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
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 updateTroubleshooting note: If
curlreportscurl: (16) Error in the HTTP2 framing layerwhile downloading the GPG key, adding the--http1.1parameter resolves the issue.
3. Install Docker Engine and the Compose plugin
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin4. Add the current user to the docker group (to run without sudo)
sudo usermod -aG docker $USERYou must log out and log back in (or reboot), otherwise the docker command will still require sudo.
5. Verify the installation
docker --version # 输出如 Docker version 29.5.2
docker compose version # 输出 Docker Compose version v2.x2. 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
docker run hello-worldOutput:
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 timeout2.2 Restart the Docker service and try again
sudo systemctl restart docker
docker run hello-worldIt still timed out, but this time the IP changed to 31.13.69.245:
dial tcp 31.13.69.245:443: i/o timeout2.3 Check if the host can access Docker Hub
curl -I https://registry-1.docker.io/v2/Output:
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:
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-worldThe result still timed out (the IP changed to 103.97.3.19):
dial tcp 103.97.3.19:443: i/o timeout2.5 Restart Docker again, problem persists
sudo systemctl restart docker
docker run hello-worldThis time the error changed:
dial tcp: lookup docker.mirrors.ustc.edu.cn on 127.0.0.53:53: no such hostThis 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:
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-worldThis time, the pull and run finally succeeded:
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
mkdir -p ~/docker/services
cd ~/docker/services3.2 Write docker-compose.yml
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
EOFPitfall reminder: MySQL 8.0 requires adding
--default-authentication-plugin=mysql_native_password, otherwise some older clients will encounter ancaching_sha2_passworderror when connecting.
3.3 First start (encountering network fluctuation)
docker compose up -dThe output shows:
WARN[0000] the attribute `version` is obsolete...
[+] up 19/24
✘ Image mysql:8.0 Error failed to resolve reference ... EOFThe 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
docker compose up -dThis time, all images were pulled successfully:
[+] 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 StartedLesson 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
docker psOutput:
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 mysql574.2 Connect to MySQL 5.7
docker exec -it mysql57 mysql -uroot -pmystrongpwd57Enter the MySQL command line, and execute exit to exit.
4.3 Connect to MySQL 8.0
docker exec -it mysql80 mysql -uroot -pmystrongpwd80Successfully entered as well.
4.4 Connect to Redis
docker exec -it redis72 redis-cli -a myredispassThe 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.

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:
networks:
default:
external:
name: services_dev-network
services:
go-app:
image: golang:1.26-alpine
networks:
- default
environment:
DB_HOST: mysql57 # 或 mysql80
REDIS_HOST: redis72In your code, simply connect to mysql57:3306 and redis72:6379.
6. Version Selection Notes and Precautions
| Service | Version | Popularity | Lifecycle Reminder |
|---|---|---|---|
| MySQL 5.7 | 5.7.44 | Accounts for about 18.8%, a common version for legacy systems | Reached EOL in October 2023, no longer receives official security updates, not recommended for new projects unless necessary |
| MySQL 8.0 | 8.0.46 | Currently the most mainstream (58% share) | Official support ends on April 30, 2026, requires upgrading to 8.4 LTS afterwards |
| Redis 7.2 | 7.2.x | Stable and mature | Base 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
| Action | Command |
|---|---|
| Start all services | cd ~/docker/services && docker compose up -d |
| Stop all services | cd ~/docker/services && docker compose down |
| Restart only MySQL 8.0 | docker restart mysql80 |
| View MySQL 5.7 logs | docker logs -f mysql57 |
| Enter Redis command line | docker exec -it redis72 redis-cli -a 密码 |
| Clean up unused images/containers | docker system prune -a |
| Back up data volumes | tar -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.

