Setting up a MySQL container in Docker

Lars Wächter
Mar 29 · 3 min read
Image for post
Image for post
https://unsplash.com/photos/fN603qcEA7g

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 enter 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.

The Startup

Medium's largest active publication, followed by +718K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store