DEV Community

Cover image for Mysql Dockerfile --> Docker container. 🐧🐳
VISHAK
VISHAK

Posted on • Edited on

Mysql Dockerfile --> Docker container. 🐧🐳

Spinup Mysql Docker Container✨

Spinning a MySQL DB on docker is easier than installing and configuring it on a raw server and can spin up the MYSQL container within a few mins.

We can avoid MySQL secure installations, cnf configurations, and other setups that needed to be composed before starting the DB.

We can classify this into 3 stages,

1) Dockerfile creation

2) Docker image creation

3) Docker container creation

1) Create the docker file

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
First, create a docker file.

# vim Dockerfile

Refer to the Dockerfile below to spinup MySQL 8.0

FROM mysql:8.0.23    
MAINTAINER vk@gmail.com
COPY my.cnf /etc/mysql/my.cnf  
ENV MYSQL_ROOT_PASSWORD mypassword 
ENV MYSQL_USER bob  
ENV MYSQL_DATABASE DATA123 
ENV MYSQL_PASSWORD new_password 
RUN mkdir /var/lib/mysql-files
RUN touch /var/log/mysqld.log
RUN chown mysql:mysql /var/log/mysqld.log
EXPOSE 3306
Enter fullscreen mode Exit fullscreen mode

Explanation:

FROM mysql:8.0.23 --> Choosing base image as mysql.
MAINTAINER vk@gmail.com --> Author field.
COPY my.cnf /etc/mysql/my.cnf --> Need to setup the cnf file as for your configuration and copy the .cnf file to the directory where Dockerfile is located.
ENV MYSQL_ROOT_PASSWORD mypassword --> 'mypassword' will be the password for root user
ENV MYSQL_USER bob --> Create a additional mysql user named 'bob' if needed
ENV MYSQL_DATABASE DATA123 --> Creating database 'DATA123'
ENV MYSQL_PASSWORD new_password --> Password for mysql users.
RUN mkdir /var/lib/mysql-files --> Create mysql lib file directory.
RUN touch /var/log/mysqld.log --> Creating mysql log file to write the mysql logs.
RUN chown mysql:mysql /var/log/mysqld.log --> Ownership change for that file.
EXPOSE 3306 --> Assigning container port.

2) Docker image creation

Move to the directory where the Dockerfile is located, then run the below command to create the docker image.

# docker build -t image_name .

3) Finally create docker container.

# docker images // to view the image id

# docker run -d -p host_port:container_port --name {container-name} {image id}

eg : # docker run -d -p 3306:3306 --name mydatabase 037da8935fa

check the status of the container,
# docker ps -a

Cheers!🥂✨

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more