Setting up a MySQL container in Docker

Setting up a MySQL container in Docker

Docker is a term you might already have heard or read about in the tech scene since it’s a quite upcoming trend in the last couple of years. Docker is platform as a service (PaaS) mostly used by software developers to develop, deploy, and run applications. A big benefit of it is that you can ship and run your application (almost) anywhere without having any external software installed on your operating system that is needed for your app besides Docker .

So, what exactly is Docker? Source

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have.

This means the application runs inside a container which is some kind of a special type of process that is isolated from other processes.

The following describes how to setup a Docker container that runs a MySQL database.

Installing Docker

The first step is to install Docker. On their website you can find an instruction how to install Docker on different operating systems.

Creating MySQL Docker container

First of all we have to pull the official MySQL Docker image from the registry. The image is executable package of software that includes everything needed to run the application (MySQL).

docker pull mysql

Next, we create a new container from the image we pulled before.

docker create --name mysql-test -e MYSQL_ROOT_PASSWORD=test mysql

This command creates a new Docker container called mysql-test and sets the MySQL root user password to test.

Run the following command to list all installed containers on your system.

docker ps -a

You should see the container we just created.

Running the container

Time to start the container. mysql-test is the container’s name we set when we created it. You can use the container’s id here as well.

docker start mysql-test

Let’s check if the container is running. This should print all running containers to the console.

docker ps

Now we can access the container via bash.

docker exec -it mysql-test bash

Last but not least we can start the mysql-client and run SQL commands. The root password is test which we set on the container’s creation.

mysql -u root -p

Instead of pulling, creating and starting the container in multiple steps, you can accomplish this with one command as well.

docker run --name mysql-test -e MYSQL_ROOT_PASSWORD=test -d mysql

That’s it! I hope you got a brief overview of Docker and containerization. Now it’s time to create and run your own containers.

The next step might be to run your developed application in it’s own container. You can find a bunch of tutorials on the Internet for that.