Mysql Docker Compose Configuration

Mysql Docker Compose Configuration

In this article we will talk about the docker and docker compose and will write a docker compose file together to run mysql for our development. In the future tutorials we will illustrate complex docker-compose. For now lets take one step at a time.

A Quick introduction to Docker

What is Docker?

Wikipedia defines Docker as

an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.

In simpler words:

  • Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. (or could be any)

  • The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. +Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources.

Let's make our hand dirty:

I assume you have docker installed in your machine. If not Docker Installation Guide From Docker will help you to do that. If you feel any difficulties don't hesitate to ask any question.

Let's go straight to the mysql.yml file

version: "3.7"
services:
  mysql:
    image: mysql:latest
    container_name: mysql
    hostname: mysql
    networks:
      - default
    volumes:
      - ~/docker/volumes/mysql/data:/var/lib/mysql
    environment:
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=admin
      - MYSQL_ROOT_PASSWORD=4545c8XAjQCHA?PdVzct
      - MYSQL_DATABASE=mysql-test
    ports:
      - "3306:3306"
    command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
    restart: always

Well,

Lets know the docker-compose file setp by step

The Compose file is a YAML file defining services, networks and volumes.

Tip: You can use either a .yml or .yaml extension for this file. They both work
  • version : is the version no for docker-compose

  • services: services refer to containers’ configuration.For example, let's take a dockerized web application consisting of a front end, a back end, and a database: We'd likely split those components into three images and define them as three different services in the configuration:

services:
  frontend:
    image: my-react-js-app
    ...
  backend:
    image: my-springboot-app
    ...
  db:
    image: mysql
    ...
  • Volumes & Networks: Volumes, on the other hand, are physical areas of disk space shared between the host and a container, or even between containers. In other words, a volume is a shared directory in the host, visible from some or all containers.Similarly, networks define the communication rules between containers, and between a container and the host. Common network zones will make containers’ services discoverable by each other, while private zones will segregate them in virtual sandboxes. If we need to find services in the same network then we can create a network and put each services into that network so all the containers can communicate to each other.

  • image: this is the name of the image that docker will fetch if not available in local

  • container_name: the mysql container will be created in such name

  • environment: in above docker-compose the environment is specific to mysql docker image. More can be found here .

MYSQL_ROOT_PASSWORD: This variable is mandatory sets the password 
of MySQL root superuser account. 
MYSQL_USER & MYSQL_PASSWORD: These variables are optional, used in conjunction 
to create a new user and  to set that user's password.  
This user will be granted superuser permissions for the database specified 
by the MYSQL_DATABASE variable. Both variables are required for a user to be created.
MYSQL_DATABASE: This variable is optional and allows you to specify the name 
of a database to be created on image startup

command :
To run this docker-compose file, type in the following command

docker-compose -f mysql.yml up -d
  • -f specifies the file name and we can be specific about it by mentioning the full file path name but if we run the command in the directory in which mysql.yml file exist then only the file name will suffice.
  • -d flag tells us to start the process in background.

to stop the service we would run the command

docker-compose -f mysql.yml down

Conclusion:

This concludes the setup of mysql database using docker. In your future post we will use this configuration as a reference.

Thanks for reading. See you in the next post.

Avatar
Moshiour Rahman
Software Architect

My interests include enterprise software development, robotics, mobile computing and programmable matter.

comments powered by Disqus
Next
Previous