Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker install MySQL

In this tutorial, you'll learn how to create a Docker container running MySQL, a popular relational database management system. We'll use the official MySQL image provided by the Docker community.

  1. Prerequisites:

    • Install Docker on your system
  2. Pull the official MySQL image:

    To pull the official MySQL image, run the following command:

    docker pull mysql
    

    This will download the latest version of the MySQL image to your system.

  3. Create a new container with MySQL:

    To create a new Docker container running MySQL, use the docker run command. We'll map the container's port 3306 (the default MySQL port) to the host's port 3306, so you can access the MySQL server from your host machine. Additionally, you'll need to set the MYSQL_ROOT_PASSWORD environment variable for the MySQL root user:

    docker run --name mysql-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
    

    Replace my-secret-pw with a secure password for the MySQL root user.

    This command creates a new container named mysql-container, maps the container's port 3306 to the host's port 3306, sets the root password, and runs the container in detached mode (-d flag). The container will run the MySQL server using the default configuration.

  4. Verify the MySQL container is running:

    To check if the MySQL container is running, use the docker ps command:

    docker ps
    

    You should see your mysql-container listed as running.

  5. Connect to the MySQL server:

    You can connect to the MySQL server running inside the container using a MySQL client or driver. If you have the MySQL command line client (mysql) installed on your host machine, you can connect to the server using:

    mysql -h localhost -P 3306 -u root -p
    

    Enter the root password you set earlier when prompted.

    This command connects to the MySQL server running at localhost:3306, which is the mapped port of the mysql-container.

  6. Customize the MySQL configuration (Optional):

    If you want to customize the MySQL configuration, you can create a local configuration file (e.g., my.cnf) and mount it as a volume inside the container.

    First, create a new directory for your configuration files:

    mkdir mysql-config
    

    Next, create a new my.cnf file in the mysql-config directory and customize it as needed. You can use the default my.cnf file from the MySQL image as a starting point.

    After customizing the configuration file, stop and remove the existing mysql-container:

    docker stop mysql-container
    docker rm mysql-container
    

    Finally, create a new container with the customized configuration, mounting the local mysql-config directory as a volume inside the container:

    docker run --name custom-mysql-container -p 3306:3306 -v $(pwd)/mysql-config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
    

    Replace my-secret-pw with the password you set for the MySQL root user.

    This command creates a new container named custom-mysql-container, maps the container's port 3306 to the host's port 3306, mounts the local mysql-config directory as a volume at /etc/mysql/conf.d inside the container, sets the root password, and runs the container in detached mode.

  1. Running MySQL in Docker Container:

    • Description: Run MySQL in a Docker container for a portable and isolated database environment.
    • Code Example:
      docker run -d -p 3306:3306 --name my-mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest
      
  2. How to Create a Docker Image with MySQL:

    • Description: Create a custom Docker image with MySQL installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM mysql:latest
      # Add custom configurations or scripts
      
  3. Docker-Compose MySQL Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including MySQL.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mysql:
          image: mysql:latest
          ports:
            - "3306:3306"
          environment:
            MYSQL_ROOT_PASSWORD: my-secret-pw
      
  4. MySQL Dockerfile Example:

    • Description: A Dockerfile example for creating a custom MySQL image with additional configurations or scripts.
    • Code Example (Dockerfile):
      FROM mysql:latest
      COPY my.cnf /etc/mysql/my.cnf
      
  5. Setting Up MySQL in a Docker Environment:

    • Description: Configure MySQL settings, such as authentication or database initialization, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mysql:
          image: mysql:latest
          ports:
            - "3306:3306"
          environment:
            MYSQL_ROOT_PASSWORD: my-secret-pw
            MYSQL_DATABASE: mydb
            MYSQL_USER: myuser
            MYSQL_PASSWORD: myuserpassword
      
  6. Configuring MySQL in Docker-Compose:

    • Description: Customize MySQL configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mysql:
          image: mysql:latest
          ports:
            - "3306:3306"
          volumes:
            - ./my.cnf:/etc/mysql/my.cnf
      
  7. Docker MySQL Image Customization:

    • Description: Customize the MySQL image by adding configurations, scripts, or creating a specific database environment.
    • Code Example (Dockerfile):
      FROM mysql:latest
      COPY my-scripts /scripts
      RUN chmod +x /scripts/setup.sh
      
  8. Docker MySQL Volume Mounts:

    • Description: Use Docker volumes to mount external directories or data files into MySQL containers for data persistence.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mysql:
          image: mysql:latest
          volumes:
            - ./mysql-data:/var/lib/mysql
      
  9. Custom MySQL Image Creation in Docker:

    • Description: Create a custom MySQL image with specific configurations and preloaded data for your application.
    • Code Example (Dockerfile):
      FROM mysql:latest
      COPY my-scripts /scripts
      RUN chmod +x /scripts/setup.sh
      
  10. Docker Swarm and MySQL Service:

    • Description: Deploy MySQL as a service in a Docker Swarm to scale and manage multiple MySQL containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        mysql:
          image: mysql:latest
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting MySQL in Docker:

    • Description: Troubleshoot MySQL-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <mysql_container_id_or_name>