DEV Community

Bidhan Khatri
Bidhan Khatri

Posted on • Updated on • Originally published at bidhankhatri.com.np

Docker Compose file for MySQL and phpMyAdmin

Here we are going to deploy MySQL and phpMyAdmin container through Docker compose file and also access the MySQL container from the Docker host MySQLWorkbench.

What is Docker?

Docker is an open-source containerization platform and uses OS-level virtualization to deliver software in packages called containers. It is written in Golang and developed by dotCloud Inc and now it is known as Docker Inc. It is basically a container engine that uses the Linux Kernel features like namespaces and control groups to create containers on top of an operating system. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines.

If you need to deploy containers than you could do through the command lines via long string commands options every time. It’s time-consuming steps if you have lots of configuration to be done or else you could use the tools through which you can deploy the containers from your configuration file. You can use Dockerfile or a docker-compose.yml or both.

mkdir ~/DockerContain
Enter fullscreen mode Exit fullscreen mode

This is my docker-compose.yml file which I've renamed to mysql-phpmyadmin.yml for the MySQL and phpMyAdmin.

vim mysql-phpmyadmin.yml

The Compose file is a YAML file defining services, networks, and volumes for a Docker application. version: '3.2' it's a compose file version. mysql:8.0 We have used MySQL Docker Image version 8.0 here. If the version tag 8.0 were not mentioned then it will go for the latest one.

Here, appsdb is the MySQL container name and restart policy is mentioned always. This means always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts, or the container itself is manually restarted.

'6603:3306' means MySQL container is running on port 3306 and this container can be accessible from the docker localhost machine's port 6603. It's a port mapping.

We also defined the environment variables for our container. Here, we have specified the root password for our MySQL.

depends_on is expressing a dependency on services. phpmyadmin/phpmyadmin docker image is used for the PHPMyAdmin container. Port is mapped '8080:80' and the environment variable PMA_HOST is specified db to connect the MySQL container.

Now execute below command to run the container from the docker-compose file.

docker-compose -f mysql-phpmyadmin.yml up -d
Enter fullscreen mode Exit fullscreen mode

This will pull the Docker Images from the Docker hub if the Images are not locally present and start the container. If the images are available locally then it will start container from the local images. -d option at the end means detached which means container run in the background of your terminal.

The below command will show the various details about the running containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

Both Docker Container is running. Now you can access the PHPMyAdmin through your browser. Goto: http://localhost:8080
Username:root and password:helloworld

You can also access the Mysql from your MySQLWorkbench.
Hostname:localhost Port:6603 Username:root Password:helloworld

Now, both containers are running and serving.

Don’t execute the command docker-composer -f mysql-phpmyadmin.yml down as it will stop and remove the containers, networks, images, and volumes.

Instead, you can execute below commands which are also safe.

docker-composer -f mysql-phpmyadmin.yml stop
docker-composer -f mysql-phpmyadmin.yml start
Enter fullscreen mode Exit fullscreen mode

Top comments (0)