docker-compose-mysql-service-configuration¶
The docker-compose-mysql-service-configuration refers to the specific settings defined within a docker-compose.yml file to deploy and manage a MySQL database container.^[600-developer__docker__docker-compose__docker-compouse-02.md]
Configuration Example¶
The following docker-compose.yml snippet demonstrates a standard configuration for a MySQL 5.7 service named db^[600-developer__docker__docker-compose__docker-compouse-02.md]:
version: "3"
services:
db:
image: mysql:5.7
ports:
- "33060:3306"
volumes:
- ./data:/var/lib/mysql
restart: always
environment:
- MYSQL_USER=tommy
- MYSQL_PASSWORD=tommy
- MYSQL_DATABASE=dockerDb
- MYSQL_ROOT_PASSWORD=root
Key Settings¶
- Image: The configuration specifies
mysql:5.7as the base Docker image to use for the container^[600-developer__docker__docker-compose__docker-compouse-02.md]. - Port Mapping: The ports mapping
"33060:3306"exposes the MySQL service. This maps port 33060 on the host machine to port 3306 inside the container, allowing external access to the database^[600-developer__docker__docker-compose__docker-compouse-02.md]. - Data Persistence: A volume mapping
./data:/var/lib/mysqlis used to persist database data. This maps a local directory nameddatato the default MySQL data directory inside the container, ensuring that data is not lost if the container is removed^[600-developer__docker__docker-compose__docker-compouse-02.md]. - Restart Policy: The
restart: alwaysdirective ensures that the container automatically restarts if it stops or if the system reboots^[600-developer__docker__docker-compose__docker-compouse-02.md]. - Environment Variables: The configuration injects several environment variables to set up the database instance on startup^[600-developer__docker__docker-compose__docker-compouse-02.md]:
MYSQL_USERandMYSQL_PASSWORD: Credentials for a specific user^[600-developer__docker__docker-compose__docker-compouse-02.md].MYSQL_DATABASE: The name of the database to be created^[600-developer__docker__docker-compose__docker-compouse-02.md].MYSQL_ROOT_PASSWORD: Sets the password for therootuser^[600-developer__docker__docker-compose__docker-compouse-02.md].
Related Concepts¶
- Docker Compose
- [[MySQL]]
- [[Data Persistence]]
Sources¶
600-developer__docker__docker-compose__docker-compouse-02.md