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

Umur Alpay
CodeResult
Published in
3 min readOct 2, 2022

This story is originally published at https://www.coderesult.com/the-browser-console-and-what-it-can-do/

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 https://hub.docker.com/_/mysql 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.

https://docs.docker.com/get-docker/

2- Create docker-compose.yaml

List of Docker Compose Properties Explained

image

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

container_name

Name of the container

ports

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.

MYSQL_ROOT_PASSWORD

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

MYSQL_USER

An additional user other than root

MYSQL_PASSWORD

The additional user’s password

MYSQL_ROOT_HOST

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.

Bonus

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

Conclusion

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

--

--