Skip to content

Docker Compose MySQL configuration

Docker Compose MySQL configuration involves defining a MySQL service within a docker-compose.yml file to automate the deployment and management of a database container.^[600-developer__docker__docker-compose__docker-compouse-02.md]

Configuration Example

The following is a typical configuration snippet using MySQL version 5.7^[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 Properties

  • Image: Specifies the container image, for example, mysql:5.7^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Ports: Maps the host port to the container's internal port (e.g., "33060:3306"), allowing external access to the database^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Volumes: Mounts a local directory (like ./data) to /var/lib/mysql to persist database data even if the container is removed^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Environment Variables: Sets the database credentials and name during creation, such as MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, and MYSQL_ROOT_PASSWORD^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • Restart Policy: The restart: always directive ensures the container automatically restarts if it stops or fails^[600-developer__docker__docker-compose__docker-compouse-02.md].
  • [[Docker]]
  • DevOps
  • [[Data Persistence]]

Sources

^[600-developer__docker__docker-compose__docker-compouse-02.md]