Install MySQL in Docker Container on Debian 12 with Easy Steps

Posted on

Install MySQL in Docker Container on Debian 12 with Easy Steps

Install MySQL in Docker Container on Debian 12 with Easy Steps

In this guide, we want to teach you to Install MySQL in Docker Container on Debian 12 Bookworm. By running MySQL in a Docker container you will get a lot of benefits such as improving your development speed and easy maintenance. Here you can follow the steps below provided by the Orcacore team to install Docker on your Debian 12 create a Docker Container for MySQL and run it.

Before you start to Install MySQL in Docker Container on Debian 12, you need some requirements. Let’s see what we need.

Requirements for MySQL Setup with Docker

First, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow this guide on Initial Server Setup with Debian 12 Bookworm.

Then, you must have Docker running on your server. For this purpose, you can visit this guide on Install Docker CE on Debian 12 Bookworm.

Now follow the steps below to Install MySQL in Docker Container on Debian 12.

Step 1 – Pull MySQL Docker Image on Debian 12

To Install MySQL in Docker Container on Debian 12, you can easily pull the MySQL image from Docker Hub on your server. The official image that is available for MySQL in Docker Hub is mysql. To download the MySQL image on Debian 12, run the command below:

docker pull mysql

When your download is completed, you will get the following output:

Pull MySQL Docker Image on Debian 12
Install MySQL in Docker Container on Debian 12 – Pull Image

You can verify your MySQL image download is completed with the command below:

docker images
**Output**
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        latest    8da80fe49fcf   34 hours ago   577MB

Step 2 – Start and Run MySQL COntainer on Debian 12

At this point, you must create a container for your MySQL image and start your container on Debian 12.

You can use the docker run command to create and run your MySQL container:

docker run --name mysql -p 3306:3306 -v mysql_volume:/var/lib/mysql/ -d -e "MYSQL_ROOT_PASSWORD=password" mysql

The options used in the command mean:

  • --name mysql: Assigns the name "mysql" to the container.
  • -p 3306:3306: Maps port 3306 on the host to port 3306 on the container.
  • -v mysql_volume:/var/lib/mysql/: Creates a Docker volume named "mysql_volume" and mounts it to the /var/lib/mysql/ directory in the container. This ensures that your MySQL data is persistent even if the container is stopped or removed.
  • -d: Runs the container in detached mode (in the background).
  • -e "MYSQL_ROOT_PASSWORD=password": Sets the MySQL root password to "password". Important: In a production environment, you should use a strong and unique password and consider using Docker secrets for managing sensitive information.

When you run the command, you will get the following output:

**Output**
5c245750272df99bd294239a57dab4ff3583104b8f2610bfaf2db12f50e74127

Then, verify your container is up and running with the following command:

docker ps
**Output**
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
5c245750272d   mysql     "docker-entrypoint.s…"   44 seconds ago   Up 43 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

Step 3 – Connect To MySQL Docker Container

At this point, you can easily connect to your MySQL shell by using the following commands:

docker exec -it mysql bash

This will change your shell prompt, then, run the command below to access your MySQL shell:

mysql -u root -p

Enter the password you have generated for MySQL in the previous step and press enter. You should see:

**bash-4.4# mysql -u root -p
Enter password:
**Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.1.0 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

That’s it, you are done. From there you can easily work with your MySQL docker container.

Conclusion

At this point, you have learned to Install MySQL in Docker Container on Debian 12 by pulling the latest MySQL image and running the container on your server by using the Docker Run command. Also, you can easily connect to your MySQL shell.

Hope you enjoy it. You may also interested in these articles:

Install Ruby on Rails on Debian 12 Bookworm

How To Install Portainer on Centos 7

Alternative Solutions for Running MySQL in Docker on Debian 12

While the docker run command is effective for quick setups, alternative solutions exist that offer improved manageability and configuration options. Here are two different approaches: using Docker Compose and utilizing a Dockerfile for customized image creation.

1. Using Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application’s services, networks, and volumes. This makes it easier to manage complex applications with multiple interacting containers.

Here’s how you can use Docker Compose to Install MySQL in Docker Container on Debian 12:

  1. Create a docker-compose.yml file:
version: "3.9"
services:
  db:
    image: mysql:latest
    container_name: mysql_db
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - mysql_data:/var/lib/mysql

volumes:
  mysql_data:

Explanation:

  • version: "3.9": Specifies the Docker Compose file version.
  • services:: Defines the services that make up the application. In this case, we have a single service named db.
  • image: mysql:latest: Specifies the Docker image to use for the service (MySQL).
  • container_name: mysql_db: Assigns a name to the container.
  • restart: always: Configures the container to automatically restart if it fails.
  • ports: - "3306:3306": Maps port 3306 on the host to port 3306 on the container.
  • environment:: Sets environment variables for the container. Here, we set the MySQL root password.
  • volumes: - mysql_data:/var/lib/mysql: Mounts a named volume mysql_data to the /var/lib/mysql directory in the container, ensuring data persistence.
  • volumes: mysql_data:: Defines the named volume.
  1. Start the application:

Navigate to the directory containing the docker-compose.yml file and run the following command:

docker-compose up -d

The -d flag runs the application in detached mode.

  1. Verify the container is running:
docker ps

This will show the running MySQL container.

Benefits of using Docker Compose:

  • Simplified Management: Docker Compose makes it easier to manage the MySQL container and its dependencies.
  • Configuration as Code: The docker-compose.yml file allows you to define the application’s configuration in a declarative way.
  • Scalability: Docker Compose can be used to scale the application by running multiple instances of the MySQL container.

2. Using a Dockerfile for Customized Image Creation

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using a Dockerfile allows you to create a customized MySQL image with specific configurations, such as pre-populated databases, custom configuration files, or specific MySQL versions.

Here’s how you can use a Dockerfile to Install MySQL in Docker Container on Debian 12 with custom configurations:

  1. Create a Dockerfile:

Create a file named Dockerfile in an empty directory.

FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD=password

# Copy custom MySQL configuration file (optional)
COPY my.cnf /etc/mysql/conf.d/my.cnf

# Copy SQL script to initialize the database (optional)
COPY init.sql /docker-entrypoint-initdb.d/

EXPOSE 3306

Explanation:

  • FROM mysql:latest: Specifies the base image to use (the official MySQL image).
  • ENV MYSQL_ROOT_PASSWORD=password: Sets the MySQL root password.
  • COPY my.cnf /etc/mysql/conf.d/my.cnf: Copies a custom MySQL configuration file to the container. This is optional, but allows for specific tuning. Create my.cnf file with your desired configurations.
  • COPY init.sql /docker-entrypoint-initdb.d/: Copies an SQL script to the /docker-entrypoint-initdb.d/ directory. MySQL automatically executes any .sql files in this directory on first startup, allowing you to initialize the database schema and data. Create an init.sql file with your desired database creation statements and initial data.
  • EXPOSE 3306: Exposes port 3306 for external access.
  1. Create my.cnf and init.sql (optional):

If you are using the COPY commands in the Dockerfile, create the my.cnf and init.sql files with your desired configurations and initialization scripts.

Example init.sql:

CREATE DATABASE IF NOT EXISTS mydatabase;
USE mydatabase;

CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    email VARCHAR(255)
);

INSERT INTO users (username, email) VALUES ('john_doe', 'john.doe@example.com');
  1. Build the Docker image:

Navigate to the directory containing the Dockerfile and run the following command:

docker build -t my_custom_mysql .

The -t my_custom_mysql flag assigns the name "my_custom_mysql" to the image. The . specifies the current directory as the build context.

  1. Run the container:
docker run --name my_mysql -p 3306:3306 -v mysql_data:/var/lib/mysql -d my_custom_mysql

Benefits of using a Dockerfile:

  • Customization: Dockerfiles allow you to create highly customized MySQL images with specific configurations and pre-populated data.
  • Reproducibility: The Dockerfile ensures that the image is built consistently every time.
  • Version Control: The Dockerfile can be version controlled, allowing you to track changes to the image configuration.

By using Docker Compose or a Dockerfile, you can gain more control over your MySQL Docker deployment and simplify the management of your application. These alternatives provide flexibility and scalability beyond the basic docker run command.

Leave a Reply

Your email address will not be published. Required fields are marked *