Docker Cheat Sheet

Download Latest Docker Engine using only one command (linux)

curl > dockerinstall && chmod 777 dockerinstall && ./dockerinstall

Check Docker Version

docker -v

Search available Docker images on Docker hub

docker search ubuntu

Pull Docker image from online hub

docker pull $imagename
docker pull ubuntu:18.04

Note: In the OFFICIAL column, OK indicates an image built and supported by the company behind the project.

List all images in local docker engine

docker image ls

Run Apache Docker container with web files mounted and port exposed

docker run --name web --restart always -v /my/own/datadir:/var/www/html -p 80:80 -d apache:latest

To list all running containers

docker ps 

To list all running as well as stopped containers

docker ps -a 

Build Docker image in local environment from Dockerfile which is in current directory

docker build -t $imagename .
docker build -t apache-php .

Tag Docker image with specified version number

docker tag $current_imagename:tag $new_imagename:$new_tag
docker tag apache-php:latest apache-php:1

Push Docker image to registry

docker push $imagename:$tag
docker push apache-php:1

Run commands inside running container

docker exec -it $containername bash
docker exec -it web bash

If you have made changes in container, which you want to store

docker commit -m "What did you do to the image" -a "Author Name" $containerid $repository/$new_image_name
docker commit -m "Changed Apache configuration rules" -a "IdenticalCloud" 03be8c21654c identicalcloud/apache:1

Stop a running container

docker stop $containername
docker stop web

Delete stopped container

docker rm $containername
docker rm web

Delete running container

docker rm -f $containername
docker rm -f web

To remove docker image from local system

docker rmi $imagename
docker rmi apache:latest

To remove unwanted folders from stopped and deleted docker containers

This command will deleted all stopped containers, deleted container’s volume and logs, not used networks

docker system prune -a

Drafted On,
January 6, 2021

Leave a Comment