How To

Docker Run Command

Reading Time: 5 minutes

The Docker Run Command is the command used to launch a docker container.  But in reality, it does more than that.  Not only does it run a container, but it will go out and get the image for the container if you don’t already have it.  Follow these instructions and in 5 minutes you’ll have a MySQL instance running in a Docker Container.

Let’s get started!

Assuming you have Docker Desktop installed if not download the Docker Desktop (here).  You can issue a command like this:

docker run --name mySqlGeekCafe -e MYSQL_ROOT_PASSWORD=p@ssW0rd -p 3306:3306 -d mysql 

Let’s break it down:

The run command:

We start with docker run command and pass it a few arguments

–name mySqlGeekCafe

--name mySqlGeekCafe: The name you want to reference the container by. This is optional but if omitted docker will create some crazy name for you.


-e MYSQL_ROOT_PASSWORD=p@ssW0rd-e Specifies an environment variable which in this case you are passing in MYSQL_ROOT_PASSWORD and setting the value to p@ssW0rd.

Environment variables are specific to the image. So you just need to know what environment variables it accepts. This is typically documented for the specific image you are looking for.  The default user is: root and the password for the database will be set to p@ssW0rd


-p: Allows you to specify port mapping.  Most of the time this is something you are going to need to do.  This allows your host (the host is your machine) to communicate with the container.

Remember that the container is like a virtual environment.  It will run in its own memory space isolated from you, this means it has its own set of ports.  MySql by default runs on port 3306.  So inside the container, that’s how it’s expecting communication.

By default, a container doesn’t allow outside communication.  It’s more secure to be closed off and be specific about who and what you’re letting in, than the reverse.

From outside the of the container, you will want to map your 3306 to the containers 3306.  The first port is the host (your machine) the second is the containers.

This also allows for some pretty cool options.  Let’s assume you’re already using port 3306 on your host; if you run the command as is, you’ll get an error that port 3306 is already in use.  A simple solution is to just map a different port from your host to the expected port on the container.

For example, use 3308 instead e.g. -p 3308:3306, this tells docker that any communication coming from the host (your machine) to the Docker container, should be accepted on 3308 but then channeled to port 3306 in the container.  It’s a bit like a traffic cop.  When you define your connection to MySQL, your port would be 3308 Docker pass it along push it to MySQL on port 3306.

-d Detached Model

-d: is a parameter on its own (no other data is need after it). -d simply says to run this in a detached mode.

If you omit this parameter, it just means that after running it, you will stay connected to the container.

-it Interactive Terminal mode

While not shown in this example, when you omit the -d parameter you typically want to use the -i -t or abbreviated by -it which allows you to interact with it


mysql: This is really the only thing you need to pass to the docker run. It’s the image name you want. Images often have a name:tag version combo to them, if the tag is omitted, it usually defaults to the latest (latest version).  You can use the tag to get a specific version of MySQL like mysql:5.7.20

I’d suggest using the 5.x versions at first.  The 8.x versions changed the security options, which I’ll discuss in a future post.

The Results / Output:

It should result in something like this:

Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
Digest: sha256:4589ba2850b93d103e60011fe528fc56230516c1efb4d3494c33ff499505356f
Status: Downloaded newer image for mysql:latest
  1. The first line Unable to find image 'mysql:latest' locally simply indicates that it couldn’t find the latest version of MySQL.  This could happen on the first time you use this command or like me, I cleared out / removed the image.
  2. The next line is going out the Docker repository to attempt to get the latest image.
  3. The Digest: is simply the sha256, checksum on the image
  4. Finally, you’ve downloaded the image

You now have a running container, with MySQL running inside of the container.

Issue the command docker ps and you should see your instance of MySQL running in the docker environment.

At this point, you can use MySQL as if you downloaded the setup files and installed it on your machine.  It’s like magic, the setup and removal of development tools just got a kick in the pants!

If I can channel a little Cosmo Kramer > “I’m lovin’ every minute of it!”

Stopping your container

The next thing you’ll want to do is stop your container.


To manually stop a container you can stop it by name or the use container id.  If you do it by Id, you actually only need the first few characters of the Id


>docker stop mySqlGeekCafe

> docker stop 5c5


If you reboot or shutdown your computer, Docker will stop your containers.  Even though Docker will restart when you start your computer, it doesn’t automatically restart your containers.  You’ll need to do this your self (or create a script to do it for you).

Starting an Existing Container

Once you have an existing container you get an error if you try and re-run it.  In this case use the Docker Start command

To start / restart the mySqlGeekCafe issue the following command

>docker start mySqlGeekCafe


A few warnings about how the data in your container is stored.

Before you start using Docker for all your data storage needs, you need to aware a few things.  By default, all your data is stored inside the container.  This means, if the container is destroyed, so is your data.  Simply stopping your container will not destroy your data.  Containers are removed when you issue the docker rm [container-name-or-id] command.


To keep your data (long term), there’s an additional parameter you can add, which effectively mounts your local drive to the containers system.  This way when data is written to the MySQL database it’s written to your hard disc instead of the one inside of the container.

The Volume Tag -v

However, if you need it you can add the -v option to your command which will mount the volumes (drives).  Depending on your OS, the actual syntax may be a bit different.  It’s essentially -v [local-folder-path]:/var/lib/mysql where [local-folder-path] is an actual path.

It can be a full path such as c:\data\mysql\project-x on windows or /Users/eric.wilson/data/mysql on a Mac.  The second parameter is a standard on for MySql.  The path /var/lib/mysql is where MySQL intends to store the data.  If you run the command again with the -v option, you should suddenly see the MySQL data folders and files generated in your local folder.

I don’t typically do this since after all this is my development environment.  My typical scenario is :

  1. Start the Docker Container.
  2. If it’s a new instance then I seed it with my seeder routines
  3. Use it throughout the day
  4. Stop it when I’m done.
  5. Repeat the cycle the next day.


In production, we use don’t typically use Docker for the Database or other types of services (Message Queues, Elastic Search, etc). Ideally, you want to use some sort of data services like Amazons RDS, or Azure Database or simply an actual server with MySql installed and use Docker for your code based environments.

Stay tuned for my next article, which shows how to use a docker-compose Yamal file, which makes it much easier to spin up several development services all at once.

Until next time, keep your Docker contained!

And P.S. here’s a nice Docker Cheat Sheet