Important Docker Commands You Should Know

Rohan Aggarwal
The Startup
Published in
6 min readSep 27, 2020

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host.

What are a docker image and docker container?

As per the official website, a Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

And docker container is an instance of the docker image, docker container is a docker image brought to life.

Breakdown of docker commands syntax:

Every docker command can be broken down into 3 parts :

1. Keyword 'docker'
2. Main task (start, stop, build, etc).
3. Options (port, tty, interactive volumes, etc).
4. Reference to image or container with all the property flags.

Every docker command starts with the ‘docker’ keyword.

2nd part of the command represents the main task which we want to execute on a docker image or on a docker container (like run for running a container, build for building an image, etc).

3rd part includes the extra options like the port number mapping (-p ), interactive communication (-i), tty (-t), volumes (-v), etc. A flag with a single dash (-) is the shortcut for the full name flag like (-p for — port) and a double dash is used for the full name.

4th part is the reference to an image or a container (like the container-id, image-name, image-id, etc).

Important Container commands :

Creating a container :

docker create {image-id/image-name}

Creates a container from a docker image. It returns a container id, which can be used for starting that container.

Starting a container

docker start {container-id}

Starts a container, but this command will not return any logs to the docker-CLI. To get the logs we need to add -a flag like

docker start -a {container-id}

Running a container

docker run {image-id/image-name}

Pull and starts the container, it is a combination of docker create and docker start command and there is no need of adding any external flag to get the logs on docker CLI.

Listing all running containers

docker container ls

Lists all the running containers. There is an alternative command to list all the running container

docker ps

Listing all container

docker ps --all / docker ps -a

Lists all the container that includes running containers, stopped containers, and newly created containers.

Inspecting container

docker inspect {container-id}

Returns all the information about the container like source, image used to create this container, restart count, platform, current state, etc.

Checking logs

docker logs {container-id}

Prints the logs on the terminal. It can be used in the scenarios when you used docker start {container-id} command and forgot to add -a tag.

Stopping container

docker stop {container-id}

Stops the running docker container gracefully. It takes its time to do the cleanup work and then stops the container, which is less than 10 seconds because if the cleanup work is taking more than 10 seconds then it runs the kill command.

Killing container

docker kill {container-id}

Stops the container abruptly, instant stopping of the container.

Removing container

docker rm {container-id}

Deletes a stopped container, We cannot remove a running container. First, we have to stop/kill the container then only we can delete or remove that container.

Removing containers

docker container prune

You will get the prompt ‘Are you sure…’, If you want to override this prompt then you can use force flag (--force or -f)

Removing containers using filters

  • Removes containers which are older than 12 hours
docker container prune --filter "until=12h"
  • Removing all container except the given container
docker container prune --filter "label!=mycontainer"

Similarly, we can use other filters as well.

Important image commands :

Building an image

The most basic command to build an image from a docker file. This represents the build context, we can use this dot only when we are executing the command in the same directory where the Dockerfile is present. This command will return the image id.

docker build .

But the build number is not that easy to remember, So we can add a tag to give a name to out build.

docker build -t {username}/my_build:{tag} .

Here username is the username of the docker hub account, my_build is the name of the image which we are building and the tag is the version of the build and dot is again present for the build context.

We can add more things like port, volume, etc in the build command.

Pushing image to the registry

docker push {image-id/ image-name:{tag}}

Adds our image to the remote repository. So that we can pull it every time we need it, It's like committing our image to a remote repository.

Listing all images

docker image ls

Lists all the images present in your system that includes both pulled images from docker hub repository and locally created images from docker files.

Checking image history

docker image history {image-name/image-id}

Given all the details like when it was created, what is the size of the image, how many times the same image was created, etc

Inspecting image

docker image inspect {image-id/image-name}

Returns all the information about the image, information like volume, created date, docker version, working directory, docker file details which are used to create this image, etc.

Deleting or removing an image

docker image rm {image-id/ image-name}

Deletes/remove an image from the image list. So, if we are done using any image we can remove the unnecessary images using this command. Sometimes we get messages like:

Error response from daemon: conflict: unable to delete bf756fb1ae65 (must be forced) — image is being used by stopped container 85da219662dc

So in this case we can remove the dependent image first and then remove the current image (preferred option) or we can force delete this image using

docker image rm {image-id/ image-name} --force

Removing all dangling images

docker image prune

Removes all the dangling images from the system.

To remove all the unused images with the dandling ones, use the same command with -a option (-a is for --attach).

docker image prune -a

Removing images using filters

It is the same as removing containers using filters,
we can use a filter with --filter option.

docker image prune -a --filter "until=12h"

Removes/deletes all the dangling and unused images older than 12 hours.

Miscellaneous commands

Checking version

docker version

Gives the complete description of the docker version.

Deleting all the unused containers and images

docker system prune

It first prompts that ‘Are you really want to continue’ and if you press Y then it will remove all the unused containers, images, and networks

By default, this command does not remove volumes. So to remove the volumes we have to pass extra options.

docker system prune --volumes

Removing all unused volumes

docker volume prune

Advance commands with options

Running the container in the background

docker run -d {image-id/ image-name}

Here -d is used for detach, it will run this container in the background. It allows you to use the terminal for other commands.

Adding port

docker run -p 4000:8080 {image-id/image-name}

Here -p is the short form for port and 4000:8080 means any request coming on port 4000 on the specified host will be redirected to 8080 port of the container.

Connecting to STDIN and STDOUT

docker run -i -t -p 4000:8080 {image-id/image-name}

-i is short for interactive and is used for opening a connection to docker client (STDIN) -t is short for --tty, it allocates a pseudo-terminal that connects your terminal with docker client for interaction. (STDIN and STDOUT)

we can combine these two commands

docker run -it -p 4000:8080 {image-id/image-name}

Auto deleting container

docker run -it -p 4000:8080 -rm {image-id/image-name}

Here the -rm is short for removal and it is used for automatically deleting the container when it stops.

Executing another command on a running container

docker exec -it {container-id} {command}

This is used when we have to execute multiple commands like in redis, we have to start the redis server and redis-CLI to interact with the redis server, So, we can use exec command like

docker run redisdocker exec -it {container-id} redis-cli

Here the container id is the id of the Redis container which gets created from 1st command.

Opening the terminal in any container

If we want to see which all programs or data are present in any container then we can open a shell terminal inside the container using the exec command.

docker exec -it {container-id} sh

For more details refer to the official website: https://www.docker.com/play-with-docker

--

--