Docker Commands Cheat Sheet

Logging into Docker

Quick peek into any file

$ cat

Creating a file in terminal

$ vim

:wq!

Clean up commands

$ docker stop $(docker container ls -aq)

$ docker rm $(docker container ls -aq)

$ docker rmi $(docker images -q)

Current directory (application artifacts)

$ ls

$ ls -l

$ ls -lrt

Current file path

$ pwd

Listing images and containers

$ docker ls

Pulling an image

$ docker image pull

Building an image

$ docker image build -t .

Inspecting images and containers

$ docker inspect

Tagging an Image

$ docker image tag :

Pushing an Image to Registry

$ docker image push :

Removing images and containers

$ docker rm

Running interactive container

$ docker container run -it /bin/sh

Running a command in the container

/ # ps aux

Exiting the Container

/ # exit

Running interactive container with assigned a name

$ docker container run -it –name /bin/sh

Running a container in detached mode

$ docker container run -d –name /bin/sh

Getting inside detached running container

$ docker container exec -it /bin/sh

Attaching to detached running Container

$ docker container attach

Running detached container for web applications

$ docker container run -d –name –publish 80:8080

Checking via web browser

$ ifconfig

Creating a file in the container

/ # echo “” >

Stopping a container

$ docker container stop

Starting a stopped container

$ docker container start

Removing the containers

$ docker container rm

Building an Image

$ docker image build -f -t .

Listing the Images

$ docker-compose images

Create and start the containers

$ docker-compose up -d

Listing the Containers

$ docker-compose ps

Scaling the containers

$ docker-compose up -d –scale =

Stop the Containers

$ docker-compose stop

Step 1: Create a directory in the container host filesystem

$ mkdir -p $HOME/

Step 2: Create a file in the new directory

$ cd $HOME/

$ touch

Step 3: Run the container with bind moun

$ docker container run -it –name -v $HOME/:/ /bin/sh

Step 4: List the files in the bind mounted directory

/ # ls -l /

Step 5: Create a file in the bind mounted directory

/ # cd /

/ # touch

                Step 6: List the files in the container host filesystem

$ ls -l $HOME/

Step 7: Running a new container with bind mount

$ docker container run -it –name -v $HOME/:/ /bin/sh

Step 1: Create a new Volume

$ docker volume create

Step 2: Listing the volumes

$ docker volumes ls

Step 3: Run a container and mount the volume

$ docker container run -it –name –mount type=volume,source=,target=/ /bin/sh

Step 4: Create a file in the directory

/ # cd /

/ # touch

Step 5: Inspecting the Volume

$ docker volume inspect

Step 6: List the mountpoint

$ sudo ls -l

Step 7: Run a new container and mount the same volume

$ docker container run -it –name –mount type=volume,source=,target= /bin/sh

Step 8: List the files in the volume mounted directory

/ # ls -l /

Step 9: Removing the Volume

$ docker volume rm