Install MySQL Inside Docker Container in Linux

Install MySQL Inside Docker Container in Linux

MySQL Server is a cross-platform relational database that allows to access data using Structured Query Language (SQL). MySQL Server have different editions. Community Edition (CE) is an open-source product that released under the GPLv2 license.

This tutorial explains how to install MySQL inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Install MySQL

  • Host network

Run the following command to create a container for MySQL that uses host network:

docker run -d --name=mysql --restart=always --network=host \
    -v /opt/mysql/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=pwd123 \
  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, MySQL service is listening on port 3306. It can be changed with -p option.

docker network create app-net
docker run -d --name=mysql --restart=always --network=app-net \
    -p 8080:3306 \
    -v /opt/mysql/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=pwd123 \

Note: don't forget to change root password.

Testing MySQL

Run the following command to open the MySQL client and enter password:

docker exec -it mysql mysql -u root -p -e "SHOW DATABASES"

The command outputs all databases on server:

| Database           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |

Uninstall MySQL

To completely remove MySQL, remove its container:

docker rm --force mysql

Remove MySQL image:

docker rmi mysql

You can also remove MySQL data:

sudo rm -rf /opt/mysql

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.