Docker Commands You Need To Know

docker run

docker run -d -p 6379:6379 --name redis redis


The -d flag runs the container in the background

The -p flag maps a host port to a container port in the format of host:container

The --name flag names the container to easily

The last field in this command tells docker which container to run

To see the result in the command line, run the command docker ps and you should see similar output:

$ docker ps


CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES


f1c442d82548        redis               "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes>6379/tcp   redis

Additional useful flags:

-it Keeps stdin open even if attached and allocates a tty for the container process. -i and -t together allow you to specify a shell process when running a container

docker run -d -it --name redis redis /bin/bash

-e Allows setting any environment variables

example: docker run -d -it -e "CONFIG=/path/to/config" --name redis redis /bin/bash

-v Mounts a volume form the host into the container with options.

The pattern is host-dir:container-dir:options

example:  docker run -d -it -v host/cache:/cache:rw --name redis redis /bin/bash

docker build

Example - build with a path:

docker build -t test-mysql .


-t adds an identifiable tag to the container

. specifies where the Dockerfile to build off of is located

You can also pass a Dockerfile in from stdin

docker build -f ./Dockerfile

Or from a URL

docker build https://github/some/repo

Specify a specific Dockerfile in a path

docker build -f Dockerfile.nginx .

docker stop

docker stop redis


Stops container either by name set with --name flag or by container id

Can't stop by image name

docker stop f1c442d82548

docker start

docker start redis

Starts container either by name set with --name flag or by container id

docker start f1c442d82548

docker logs

docker logs -f redis


-f flag follows the logs of the container in real time

Last parameter can be container name or id

Other useful tags:

-tail Number of lines to show from the end of the logs

--since Show logs since timestamp (e.g. 2013-01-02T13:23:37) or minutes (e.g. 42m for 42 minutes)

--details Gives a more in depth log including environment variables