Docker cheatsheet

Build:

build command is used to build images from a Docker file.

Commands:

To build image from a Docker file

docker build -t <image-name>:<version>

To list all locally stored images.

docker images

To delete an image

docker rmi <image-name>:<version>

Run

To run a container from the image.

Commands:

Docker run --name <container-name> <docker-image>

Flag used:

-d : To detach a container on the start

-rm : To remove a container once it stops.

-p : To publish the host IP

-v : To define and share the volume across containers.

-read-only: To set to the read-only permission.

Ship:

To ship the container anywhere.

Commands:

To pull images from the registry.

docker pull <image-name>:<version>

To log in to the registry.

docker login <registry>:8000

To push an image to the registry.

docker push <repo-name>/ <image-name>:<version>

Clean:

Clean the wasted resources.

Commands:

To clean image

docker image prune

To remove the unused image

docker image prune -a

To prune the entire system

docker system prune

To kill all containers

docker kill $(docker ps -q)

To delete all stopped container

docker rm $(docker ps -a -q)

Delete all images.

docker rmi $(docker images -q)

Interaction With Container

To make interaction with the container.

Command:

To run a command in a container.

docker exe -ti <container-name> command.sh

To follow container logs.

docker logs -ft <container-name>

To save running a container as an image.

docker commit -m "message" -a "author" <contianer-name> <username>/<image-name>: tag

Important Terms:

Layer:

Read-only files to provision the system

Image:

Read only the layer that is the base of an image.

Container:

A runnable instance.

Registry/ Hub:

A place where docker images reside.

Docker machine:

A VM to run the docker container.