box
Posted on September 30, 2024
Environment
-
Mac
- CPU: Apple M1
- OS: Sonoma 14.6.1
-
Docker Desktop
- Version: 4.34.2
Dir Structure
* Drawn by mermaid.
graph TB;
subgraph "top"
A["docker-compose.yml"]
B[".env"]
end
docker-compose.yml
services:
mysql:
image: mysql:8.0.39
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "${MYSQL_HOST_PORT}:3306"
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Structure
First, Define required attributes.
services:
mysql:
image: mysql:8.0.39
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
As image, we specify MySQL Docker Official Image.
If the version of MySQL is specified aslatest
, the image will be created based on the latest version.
The latest version as of this writing is 9.0.1.
The reason for specifying version 8.0.39 is that it is the most recent version compatible with MySQL Workbench.As environment, we set the root user password.
The root user is automatically created when MySQL is installed.
The list of environment variables that can be specified is written in docker hub.
Next, Mapping ports.
services:
mysql:
image: mysql:8.0.39
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
# new attribute.
ports:
- "${MYSQL_HOST_PORT}:3306"
3306
is the default port number for MySQL to listen on for TCP/IP.This port is used by MySQL Workbench to connect to the container.
Finally, Make DB data persistent.
services:
mysql:
image: mysql:8.0.39
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "${MYSQL_HOST_PORT}:3306"
# new attribute.
volumes:
- db-data:/var/lib/mysql
# new element.
volumes:
db-data:
Create persistent data stores by defining volumes element.
In this case, we create the volume nameddb-data
.The default data-dir for MySQL is
/var/lib/mysql
and we mount it to the volume.
.env
MYSQL_HOST_PORT={host-port}
MYSQL_ROOT_PASSWORD={root-user-password}
Docker
Create Container, Image, and Volume.
Open the terminal.
Move the current-dir to the location where
docker-compose.yml
is located.-
Execute docker compose create.
docker compose create
Check the Version of MySQL.
-
docker compose start
-
Check the name of the container.
docker ps
The
Names
column corresponds to it. -
Check the version in the container.
docker exec <container-name> mysql -V
For example, the following response is returned.
mysql Ver 8.0.39 for Linux on aarch64 (MySQL Community Server - GPL)
-
docker compose stop
MySQL Workbench
Download.
-
Download.
The version to be downloaded is
8.0.38
. The M1 chip uses the ARM architecture, so the appropriate one ismacOS (ARM, 64-bit), DMG Archive
.
Add Connection.
-
Start the container to connect to MySQL.
docker compose start
-
Enter any value for Connection Name, and update Port with
.env.MYSQL_HOST_PORT
. Click the Test Connection button.
After entering.env.MYSQL_ROOT_PASSWORD
, a connection test will be conducted.
If successful, click the OK button to close this dialog.-
Stop the container.
docker compose stop
Operate.
Posted on September 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.