docker run {containerName}:{Tag}

Run an image to create a container using the containers name, Id, or first few numbers of the Id.

> docker run nifty_euclid

> docker run dfa45e069b37

> docker run dfa4

You can specify which version of the container by adding the tag on to it. If you want to run a specific version of nginx add ':tag' to it. Find the nginx tags on docker hub

> docker run nginx:1.14-alpine-perl

If no tag is specified it defaults to the tag called 'latest' eg nginx:latest

Run with Detach

-d / --detach

Detach runs the container in the background so that running container is not attached to the current terminal.

> docker run -d dfa4

NOTE: If you run a container with detach then you will not see the output of the application.

Run with Name

--name {containerName}

Add a name to the container which can use to refer to this container.

> docker run --name containerName nginx

Run with Port mapping

-p / --publish {hostPort}:{containerPort}

the first port is the port you will expose to the machine and the second port is the port to listen to in the container.

> docker run -p 8080:80 nginx

The default port for nginx is 80 therefore the container must listen to that port and expose it via the chosen port (which is 8080 in this case). Outside the container navigate to http://localhost:8080/

This allows you to run to instances on the same machine very easily. for example you can run with with -p 8080:80 and another with 8081:80

Run with Environment Variables

-e / --env {ENV_VAR}={value}

If an application gets it's configuration from environment variables, you can set the environment variables of the container you are about to start up.

> docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

Run with Command

docker run {containerName}:{Tag} {command}

In some cases you may wish to start the image with a different command. NOTE: This does not affect the entrypoint

> docker run busybox sleep 1000

eg The default command of busybox is "sh" (see busy box docker file). We can override it with the sleep command so that the container stays alive.

If your image defines an entrypoint then you can override the default command which gets appended to the entrypoint. eg the command "python /" runs that file but if the entrypoint is "python" and the command is "/" we can override the command while keeping the entrypoint:

> docker run mypythonscript /

NOTE: Understanding how this works in the Dockerfile greatly improves understanding this

Run with Entry point

The entrypoint command can be overriden with --entrypoint eg --entrypoint sleep

Continue to supply a command for the entry point at the end. eg a command for sleep could be 1000

> docker run --entrypoint sleep mypythonscript 1000

NOTE: Understanding how this works in the Dockerfile greatly improves understanding this

Run with volume for Data persistence

Bind mounting

You can share a folder on the machine with the container so that when files are added to the container, they are also persisted to the host machine. When another container starts up it can have access to those files as well. Before you run the below command, create a folder in your D drive named myData and put a random text file in it.

-v {localFolderPath}:{containerfolderPath}

> docker run --name myApp -v D:\\myData:/myData -d busybox sleep 3600

We start up a light weight linux container called busybox and make it sleep for 3600 seconds so that it does not exit immediately.

-v (volume) allows you to map a location on your drive to a location on the container.

If you then exec into the box and explore that folder you will see that it has all the files from your machine in the container.

> docker exec -ti myApp sh

$ cd myData

$ ls

Add another file to the container, stop the container and check your local file system

$ touch newFile.txt

$ exit

> docker stop myApp

> D:

> cd myData

> dir

If you follow those commands you should find that D:\\myData\\newFile.txt exists. The file you added to the container has been persisted to your machine.

You can use volumes to share the source code so that the container updates it's files as you develop.

Volume mounting

Volumes are the same concept as Bind mounting except you manage the data through docker. This makes finding the data from the host a bit difficult.

-v {volumeName}:{containerfolderPath}

> docker run -v myVolume:/data/db -d mongo

You can view your new volume once it has been created.

> docker volume ls

> docker volume inspect myData

Now add data to the db, delete the container, run a new container with the same volume paramter, query your data. You should find your data has persisted.

Remove all Volumes

Volumes are never automatically removed because they store data therefore you have to explicitly remove them. Remove volumes with rm.

> docker volume rm myData

The following will remove all volumes not used by any containers.

> docker volume prune

Run with Restart policy

When you run a container you can specify its restart behaviour with --restart={value}.

no - Do not restart the container when it exits.

on-failure - Restart the container only if it exits with a non zero exit status.

always - Always restart the container regardless of the exit status.

> docker run --restart=always myApp

for '--restart=on-failure' Docker will try restart the container forever, you can add a retry count to that.

> docker run --restart=on-failure:10 myApp

Providing a maximum restart limit is only valid for the on-failure policy.

Run with auto remove container

Sometimes you want a temporary container. You can add --rm on run so that the container removes it's self from Docker on exit.

> docker run --rm myApp


Check out these links for more info:

My Docker Samples