A guide to understand how to install MySQL using Docker, creating a MySQL container using Docker Compose, connecting database from command line and Adminer. The following setup done in Ubuntu.
1. Install MySQL Server using Docker
We need to pull down the correct MySQL image and docker pull mysql/mysql-server:tag
is the command to be used. The tag
is the label for the image version you want to pull (for example, 5.5
, 5.6
, 5.7
, 8.0
, or latest
). If :
is omitted, the tag
latest
label is used, and the image for the latest GA version of MySQL Community Server is downloaded.
$ sudo docker pull mysql/mysql-server:8.0
Verify MySql image :
$ docker images
– List out all images.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql/mysql-server 8.0 716286be47c6 3 weeks ago 381MB
2. Create MySQL Docker Container
Following is the command to create MySQL docker container.
--name=mysql-docker
– To Specify Container name.-p 3306:3306
– Port Binding-p <host_port>:<container_port>
, if would like to access form different port (ex:3309
) then command would be-p 3309:3306
-e MYSQL_ROOT_HOST='%'
– to specify host to access MySQL DB forroot
user.'%'
is to allow to access from any other containers. You can change this to specific host.-e MYSQL_ROOT_PASSWORD=password
– To setroot
user password.-e MYSQL_DATABASE=docker_db
– Name of the Database to be created.-d mysql/mysql-server:8.0
– To Specify image name (which is created in previous step), -d means containers starts in detached mode.
docker run --name=mysql-docker -p 3306:3306 -e MYSQL_ROOT_HOST='%' -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=docker_db -d mysql/mysql-server:8.0
Verify created Container :
$ docker ps -a
– List out all containers.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60881813f5d3 mysql/mysql-server:8.0 "/entrypoint.sh mysq…" 41 seconds ago Up 39 seconds (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-docker
Stop MySQL Container :
$ docker stop mysql-docker
Start MySQL Container :
$ docker start mysql-docker
3. Connecting MySQL server from within container
You can run MySQL client using command $ docker exec -it mysql-docker mysql -uroot -p
. It asks user password, enter password that used to create container.
Show the Database names to verify Setup :
Issue mysql command show databases;
to list out all the database names.
mysql> show databases; +--------------------+ | Database | +--------------------+ | docker_db | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.12 sec)
Your setup is done. If you are using Docker-Compose, now same setup Let’s configure using Docker-Compose.
4. Setup MySQL server and create a container using Docker-Compose
Make sure you already install Docker-Compose. Before starting Docker Compose remove previous setup,
$ docker stop mysql-docker
– Stop container
$ docker container rm mysql-docker
– Remove container
$ docker image rm mysql/mysql-server:8.0
– Remove Image
4.1. Create a Directory
$ mkdir mysql-db
4.2. Create a docker compose .yml
file
$ cd mysql-db
– Navigate to directory which is created in previous step
$ touch docker-compose.yml
and $ sudo nano docker-compose.yml
– Add the following lines and save the file.
version: '3' services: mysql-docker: image: mysql/mysql-server:8.0 environment: MYSQL_ROOT_HOST: "%" MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: docker_db ports: - "3306:3306"
4.3. Once you created docker-compose.yml
run the following command in the same directory where .yml
is created.
$ docker-compose up
4.4. Verify docker container
You can verify created container and images using $ docker-compose ps
and $ docker-compose images
$ docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------------------- mysql-db_mysql-docker_1 /entrypoint.sh mysqld Up (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp
4.4. You can follow Step 3 to connect your database from mysql container (mysql-db_mysql-docker_1). If you want to connect your database from Adminer follow the next steps.
5. Configuring Adminer for MySQL Container
Adminer is a simple and lightweight php web application to work with your databases like mysql, it’s pretty easier to work with SQL and DB operations in DEV environment using Adminer.
5.1. Before update Adminer setup, first we need to down the previous setup, to do that run following command.
$ docker-compose down
5.2. Add the following configuration in docker-compose.yml
file
version: '3' services: mysql-docker: image: mysql/mysql-server:8.0 environment: MYSQL_ROOT_HOST: "%" MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: docker_db ports: - "3306:3306" adminer: image: adminer:latest environment: ADMINER_DEFAULT_SERVER: mysql-docker ports: - "8080:8080"
5.2. Run $ docker-compose up
command
After running $ docker-compose up
command Adminer will be pulled and mysql and adminer containers will be started. You can verify them using command $ docker-compose ps
$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------- mysql-db_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:8080->8080/tcp mysql-db_mysql-docker_1 /entrypoint.sh mysqld Up (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp
5.3. Go to your web browser and enter URL http://localhost:8080
You will see following Login screen. Database field is optional. Enter your root user and password details and Login.
Once you login you will see following screen, here you can connect to specific DB and even you can create new Databases.
5.4. Docker Compose useful commands
Following are some useful docker compose commands to work with containers.
$ docker-compose start
– To start all the containers based on.yml
configuration.$ docker-compose stop
– To stop all the containers based on.yml
configuration.$ docker-compose down
– To stop all running containers and remove them based on.yml
configuration.$ docker-compose ps
– To show List of all active containers,$ docker-compose ps -a
to show all containers.$ docker-compose images
– To show list of all images.
6. Conclusion
In this guide we have covered 1. How to install MySql Server and container using Docker, 2. How to setup MySql Server and container using Docker-Compose, 3. Setting up Adminer to connect your mysql database.
You also might be interested in Spring Boot + Docker Example.