NELKINDA SOFTWARE CRAFT

Docker Commands Cheat Sheet

When you are working with docker you will be using docker commands. The cheat sheet in this article provides some of the frequently used commands.

Author:
Shweta Sadawarte, Software Craftsperson at Nelkinda Software Craft Pvt Ltd
First Published:
by NNelkinda Software Craft Private Limited
Last Modified:
by Shweta Sadawarte
Approximate reading time:

1 Cheat Sheet Table

Docker CommandDescription
docker or
docker help
To view the list of available commands.
docker --versionView the currently installed version of the docker.
docker infoTo view the system-wide information like Dockers root Directory, OS version, Kernel Version, Docker Version, RAM, CPU and Docker Registry.
Docker Containers
docker start {container_id or container_name}Starts one or more stopped containers.
docker stop {container_id or container_name}Stops one or more running containers.
docker restart {container_id or container_name}Restarts a container.
docker kill {container_id or container_name}Kills the container by stopping its execution immediately.
docker runRuns a command in a new container.
Syntax: docker run -it -p port_number -d image_id
-it is to make sure the container is interactive (Allows us to provide input to the container)
-i means we want to attach our terminal to STDIN of process
-t to show text nicely on the screen. It represents --tty
-p is for port forwarding/mapping
-d to run the daemon in the background/detached mode
Example: docker run -p 8080:8080 -p 50000:50000 jenkins
The left-hand side of the port number mapping is the Docker host port to map to and the right-hand side is the Docker container port number. When you open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.
NOTE: For Port Mapping - Ports from source do not need to be matched/identical with container port.
docker execRuns a command in a run-time container.
Syntax: docker exec -it {container_id or container_name} bash
Example: docker exec -it demoapp bash
Create a new bash session in the container demoapp
docker attach {container_name or container_id}Attaches to a running container.
docker rm {container_name or container_id}Removes /Deletes container.
‘docker rm’ will work only when docker is stopped.
docker rm -f {container_name or container_id}
-f option to remove a running container forcefully.
docker pull {image_name or registry_path}Pulls an image or a repository from a registry.
docker push {repository_name}Pushes an image or a repository to a registry.
Docker Images
docker imagesDisplays all the images currently installed on the system.
docker psDisplays currently running instances.
docker ps -a
Displays all available instances i.e. all the running and exited containers.
docker pull {image_name or registry_path}Pulls an image or a repository from a registry.
docker push {repository_name}Pushes an image or a repository to a registry.
docker buildBuilds an image from a Docker file.
Syntax: docker build -t image_name:tag_name Dir
-t − To mention a tag to the image.
image_name − Name you want to give to your image.
tag_name − Tag you want to give to your image.
Dir − The directory where the Docker File is present.
Example: docker build -t demoapp:0.1 .
demoapp is the name we are giving to the image
0.1 is the tag number we are giving to our image
“.” is used to build the Dockerfile in the present folder
Example: docker build -f Dockerfile.dev .
-f specifies the file we are going to use to build the image. Here we are using Dockerfile.dev file for the build instructions instead of Dockerfile.
docker rmi {image_name or image_id}Removes/Deletes container images.
docker tagTo set tag or name of docker image into a repository.
Syntax: docker tag source_image{:tag} target_image{:tag}
Docker Prune
docker image pruneRemove all dangling images.
docker container pruneRemove all stopped containers.
docker volume pruneRemove all volumes not used by at least one container.
docker network pruneRemove all networks not used by at least one container.
docker system prunePrune everything. Delete all unused/stopped containers, unused networks, build cache and dangling images.