Running a simple MySQL container in Docker via docker-compose.yaml

02 October 2022

Sometimes you need a simple MySQL server but installing and running locally may take some effort or you don't want to pollute your current database. For me, I don't like to install a database server on my laptop. Even anything that can be containerized I would like to have it in a container.

With docker, you can easily start up a MySQL server in 2 ways.

The first one is to pull the latest image from and follow the instructions and run it from the cli but I don't find it very convenient.

The second way is to use docker-compose.yaml file to set up application properties and run it. It's more configurable in my opinion. Let's go step by step and run a standalone MySQL container.

1- Install Docker

You can install docker from the link below based on your OS.

Get Docker
Home page for Get Docker

2- Create docker-compose.yaml

    image: mysql:8.0
    container_name: mysql-test-db
      MYSQL_DATABASE: 'demo'
      MYSQL_USER: 'user'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_ROOT_HOST: '%'
      - '3306:3306'

List of Docker Compose Properties Explained


Image of your MySQL version, in this example we use version 8.0.


Name of the container


Port of your MySQL instance will connect to, port on the left is the exposed port and the port on the right is the port in the container. If you change the port on the left then you need to adjust your db connection on your computer or application to the new port.


This is your password of root account which has full permission on your SQL server


An additional user other than root


The additional user's password


To allow root connections from other hosts, set this environment variable.

3- Running docker-compose file

On the terminal navigate to the directory your docker-compose file in and run

docker-compose up

When you run the command your terminal screen will fill up some logs like this and you won't be able to use that terminal tab again.

In order to prevent that you can use the command, this way your command line will be released.

docker-compose up -d

You can check the instance from your Docker Desktop application and connect to your database from your preference. I use the database tab from PhpStorm, I find it very useful.


Let's say you have a couple of {name}-docker-compose.yaml files in the same directory, you can run the command in order to run that specific yaml file.

docker-compose -f mysql-docker-compose.yml up -d


With Docker we can have separate application instances which won't collide or conflict with other applications. That way we can easily experiment with new things.

Follow me on Instagram, Facebook or Twitter if you like to keep posted about tutorials, tips and experiences from my side.

You can support me from Patreon, Github Sponsors, Ko-fi or Buy me a coffee