Docker Compose file for MySQL and phpMyAdmin

Tags: docker, mysql, linux, webdev
Published: 2020-04-25 10:00:01


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.

![](https://cdn-images-1.medium.com/max/2000/0*YzBoxMiLm8W3rkJ4.jpg)

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](https://www.docker.com/). 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 <code>Dockerfile</code> or a <code>docker-compose.yml</code> or <code>both.</code>

    mkdir ~/DockerContain

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

{% gist https://gist.github.com/bidhanahdib/39a778e9d55b096559ed407c8d2c5b66 %}

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

Here, <code>appsdb</code> is the MySQL container name and <code>restart</code> policy is mentioned <code>always.</code> 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.

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

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

<code>depends_on</code> is expressing a dependency on services. <code>phpmyadmin/phpmyadmin</code> docker image is used for the PHPMyAdmin container. Port is mapped <code>'8080:80'</code> and the environment variable <code>PMA_HOST</code> 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

This will pull the Docker Images from the [Docker hub](https://hub.docker.com/) 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. <code>-d option</code> at the end means detached which means container run in the background of your terminal.

![](https://cdn-images-1.medium.com/max/2000/0*UF-5p7gq-N3xX83n.png)

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

    docker ps

![](https://cdn-images-1.medium.com/max/2048/0*WnQ8_A_iRsKDbCq0.png)

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

![](https://cdn-images-1.medium.com/max/2048/0*MW4kgru-amQ8gtJQ.png)

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

![](https://cdn-images-1.medium.com/max/2048/0*-6dfWr74sm6YWvi-.png)

Now, both containers are running and serving.

Don’t execute the command <code>docker-composer -f mysql-phpmyadmin.yml down</code> 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