[2/4] Docker: Setting up MySQL

Previously, in this series we talked about getting started with docker and performing some elementary operations. In this episode, we’d dive deeper into docker, and then we’d swing by some of the commands you could use to follow along, and perform some common tasks in a terminal window.

Docker command line: Getting started

If you were to run a following command, you can see all containers, including the ones which are stopped, using the following command:

$ docker container ls -a

With following command, you can see all docker images:

$ docker image ls -a

As you can probably tell, this command is almost identical to the command to list (ls) all (-a).
Following are the equivalent commands to the two commands we saw above, respectively:

$ docker ps -a $ docker images -a

Let’s say you want to, download (pull) a docker image, say MySQL, in advance. It is just like cloning a repository from GitHub:

$ docker pull mysql

If no tag is specified, then :latest tag is assumed by default. Now to run this newly downloaded image, you can possibly run the following command:

Running the container:

$ docker run \
  -e MYSQL_DATABASE=epidemics \
  -e MYSQL_USER=epidemics \
  -e MYSQL_PASSWORD=epidemics \
  -p 3306:3306 \
  --name mysql \
  --mount type=bind,source=/abs_path/,target=/docker-entrypoint-initdb.d \

-d represents detached mode, -e is for environment variable and -p is for port mapping. The way port mapping works is, it lets you map a port your computer (say X) to a port of a docker container (say Y). The syntax is X:Y.

The MySQL container will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.
MySQL Docker docs

You can learn more about any command, using --help option, e.g.

$ docker container --help $ docker image --help $ docker run --help

You can not see the logs, when your container is running in a detached mode. You have 2 options to fix this.

  • You can remove the -d option, add -i and add -t to get an interactive session on your TTY (terminal). That way you can see the logs or the output on your screen and can also send key combinations like Ctrl+C
  • The other option is to use following command. The -f means, we want to “follow” the logs
    $ docker logs -f mysql

Clean up:


  • To delete a stopped container by name/ID
    $ docker rm [Container ID | Container Name]
  • To force delete running/stopped container by name/ID
    $ docker rm -f [Container ID | Container Name]
  • To delete all stopped containers
    $ docker rm $(docker ps --no-trunc -aq)
  • To delete all containers
    $ docker rm -f $(docker ps --no-trunc -aq)


  • To delete an image by name/ID
    $ docker rmi [Container ID | Container Name]
  • To delete all images
    $ docker rmi $(docker images -q)
  • To delete all images without tags, either of the following could be used depeneding on what are you trying to do:
  • $ docker rmi $(docker images -f "dangling=true" -q)
  • $ docker rmi $(docker images -a | grep "^" | awk '{print $3}')
  • $ docker images -q --filter "dangling=true" | xargs -r docker rmi


  • In docker 1.13 you can use docker image prune, but I believe the former is more powerful.
  • I am on Docker version 18.03.0-ce, build 0520e24 and docker-compose version 1.18.0, build 8dd22a9.


  1. Hoskins, J. (n.d.). Remove Untagged Images From Docker. Retrieved July 27, 2013, from http://jimhoskins.com/2013/07/27/remove-untagged-docker-images.html
  2. Leszko, R. (2017). Continuous delivery with Docker and Jenkins : delivering software at scale. Birmingham, UK: Packt Publishing.
    author = {Leszko, Rafał},
    title = {Continuous delivery with Docker and Jenkins : delivering software at scale},
    publisher = {Packt Publishing},
    year = {2017},
    address = {Birmingham, UK},
    isbn = {9781787125230}

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s