List of useful docker commands

List of useful docker commands
List of useful docker commands

This is a selective list of useful Docker commands that’ll help in your day to day job. Docker is built around two concepts; Image and container. A Docker image is a compiled version of a docker file that is built up from a series of read-only layers and A Docker container is a running instance of an image that includes application + dependencies. You can have as many as running containers of the same image.

Build (Docker-file) → Image (output) → Run → (Container).

Docker-file – set of docker Instructions
Image – Compiled version of Docker-file
Container – Executable binary

By default docker images is a stack of layers (basically ordered file system) and it starts with a base layer, image layers are created based on each Dockerfile commands. When the container is created it is formed on top of the image layer called the container layer. All writes and changes to the container add new or modified data which are stored in the writable layer. Say if the container is deleted the underlying image layer remains unchanged.

You might like distroless images

Example:  The below Dockerfile contains four commands, each of which creates a layer.

FROM node:10.19.0 AS build-envADD . /app
WORKDIR /app
FROM gcr.io/distroless/nodejs
COPY --from=build-env /app /app
WORKDIR /app
CMD ["hello.js"]

In this article, we will discuss some of the docker handy commands to remember and help you in debugging.

Docker Commands, Help & Tips

$ Docker 

Show commands & management commands

$ docker version

Show the Docker version information

$ docker info

Provides information about the number of containers and images in the current system

WORKING WITH CONTAINERS

$ docker container ls --all

View all spawned containers:

$ docker build -t friendlyname . 

Command to build the Docker image. In other words, create an image from the dockerfile of the current directory. The t command to find the docker easier with tag.

$ docker container run -it -p 80:80 friendlyname

Run image as containers in the foreground:  -p [port-on-the-host]:[port-in-the-container] denotes the ports mapping on the host and the container respectively. The command uses two flags on the run command: –interactive (or -i) and –-tty (or –t). First, the –interactive option tells Docker to keep the standard input stream (stdin) open for the container even if no terminal is attached. Second, the –tty option tells Docker to allocate a virtual terminal for the container, which will allow you to pass signals to the container. This is usually what you want from an interactive command-line program.

$ docker container run -d -p 80:80 friendlyname

Run a container in the background

$ docker pull gcr.io/distroless/nodejs

Download the indicated version of the image version to the current machine.

$ docker ps

Lists running containers that are running on the machine with the below information.

  • Container ID
  • Image used
  • Command executed in the container
  • Time since the container was created
  • The duration that the container has been running network ports exposed by the container name of the container 
$ docker ps -la

List running container including stopped ones.

$ docker logs CONTAINER

CONTAINER is the name of the container you want to use.

$ docker inspect CONTAINER ID

Shows detailed information of a container in JSON format.

$ docker start container id

Starts a stopped container using the last state

$ docker stop container id

stops one container

$ docker stop `docker ps -q`

Stop all running containers

$  docker rm $(docker ps -aq)

Remove all stopped containers

$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|
$ docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files between host and container and vice versa

$ docker stats

Shows the execution statistics of the container.

$ docker container kill <hash>

Force shutdown of the specified container

$ docker ps -a | grep "pattern" | awk '{print $3}' | xargs docker rmi 

Remove containers based on a pattern

IMAGE COMMANDS

$ docker images

List information of the images that are available on the machine  (Name, id, space it occupies, Time elapsed since it is created)

$ docker rmi $(docker images -f "dangling=true" -q)

Removing dangling images (requires elevated privileges)

$ docker rmi $(docker images -qa)

Remove all images

$ docker images -q --filter dangling=true | xargs docker rmi

Remove untagged images

$ docker save 74ee95e45216 > busy.tar.gz

save an image as a tar file with the following id

$ docker system df	

List unwanted containers and dangling images:

$ docker system prune

Remove  – all stopped containers, all networks not used by at least one container, all dangling images, all dangling build cache

$ docker history [OPTIONS] IMAGE	

show the history of an image

NETWORKING

$ Docker network ls 

List the existing network on the machine.

docker network inspect [NETWORK_NAME]

Returns information about one or more networks. Network command by default provides all results in a JSON object. (“bridge” is default)

Volume

$ Docker volume ls

List the exists volume of the machine 

$ docker volume prune

Cleanup unused volumes

$ docker pull nginx
$ docker image inspect nginx

Inspect and see volume Details

Image tagging and Repository

$ docker tag <image> username/repository:tag

Tag <image> for upload to registry

$ docker push username/repository:tag 

Uploads the version (tagged image) of the indicated image to a docker registry.

$ docker run username/repository:tag

Run image from a registry

$ docker login

Log in this CLI session using your Docker credentials

SSH into Container

$ docker exec -it <container name> /bin/bash

Debug the container: docker exec command that can be used to connect to a container that is already running.

  • Use docker ps to get the name of the existing container
  • Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container

Conclusion

I hope, this docker cheat sheet will help you to gain confidence in using the Docker commands in your day to day job. Apart from this running ‘docker help’ will display information about the basic syntax for using the docker command-line program as well as a complete list of commands for your version of the program. Refer to official documentation for more information.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top