Day 20 - Docker CheatSheet
90 DaysOfDevOps

A passionate Linux user and FOSS promoter from India. I like playing around systems and networking. Despite having tried a bunch of programming languages, python and golang remain my favourties.
Also checkout a FOSS community by my university students - https://foss.coep.org.in/cofsug/
Docker is an open source software platform used to create, deploy and manage virtualized application containers on a common operating system (OS) with the aid of certain tools.
Installing Docker Engine
| Linux | https://docs.docker.com/desktop/install/linux-install/ |
| macOS | https://docs.docker.com/desktop/install/mac-install/ |
| Windows | https://docs.docker.com/desktop/install/windows-install/ |
Essential Glossary related to Docker
| Terminology | Meaning |
| Image | A standalone, virtual and executable "package" that can run particular software configured inside it. |
| Container | It provides isolated environment in order to achieve the separation between the host system environment and the environment contained within the image. |
| Engine | It is an open source containerization technology for building and containerizing your applications. Acts as a client-server application with the server, api and the docker CLI(client). |
| Hub | It iss a web-based storage platform for public/private access to docker images. |
Commands
Running a container
Start a container from image
docker run <image>Start a container with custom name
docker run --name <your_name> <image>Start a container in detached mode
docker run -d <image>Start a container with Port mappings
docker run -P [host_port]:[container_port] <image>Start a container with a hostname
docker run --hostname <your_name> <image>Start container in interactive mode
docker run -it <image>Start container with a specific entrypoint
docker run --entrypoint <executable> <image>Start a container by mapping a local directory into the container
docker run -v <host_dir>:<target_dir_in_container> image
Operations on images
Build an image from Dockerfile
docker build <directory>/<url>Download an image from DockerHub
docker pull <image>:[TAG] # can also specify version/TAG #default is the 'latest' versionList all the images
docker imagesRemove all containers
docker rmi $(docker images -q)List dangling images
docker images -f dangling=trueRemove all dangling images
docker rmi $(docker images -f dangling=true -q)Give tag to an image
docker build tag <name>/<image_ID> <new_name>:[tag]Save an image to a tar file
docker save <image> > <name.tar>Load an image from a tar file
docker load -i <tar_file>
Operations related to a container
Stop or remove all the containers at once
docker rm -f $(docker ps -aq) docker stop -f $(docker ps)List all the running containers
docker ps docker ps -a # list all active and stopped containersStop a container
docker stop <container1> <container2> ...Delete a container
docker rm <container1> <container2> ... docker rm -f <container1> <container2> ... # force deleteRestart a running container
docker restart <container1> <container2> ...Start a stopped container
docker start <container1> <container2> ...Execute a command within a running container
docker exec <container> <command> # for e.g. docker exec -d mycontainer touch /tmp/execWorksCopy files from localhost to container and reverse
docker cp <source_path> <container>:<dest_path> # localhost to container docker cp <container>:<source_path> <dest_path>Rename a container
docker rename <old_name> <new_name>Create an image from a container
docker container commit -a "author" -m "message" <container> <image_name>Delete all the stopped containers
docker prune $(docker ps -a)
Monitoring
Show overall stats of all containers
docker stats <container>Show logs of a container
docker logs <container>Show processes running in a container
docker top <continer>Show mapped ports of a container
docker port <container>Detailed info about any docker entity
docker inspect <image>|<container>|<entity>List files/directories added, edited or deleted from a container compared to the base image
docker diff <container>



