Docker Cheatsheet
Installation & General Commands
● Use docker --help to get help with Docker or subcommands.
● Display system-wide information with docker info.
● Start the Docker daemon using docker -d.
Images
● Build an image from a Dockerfile using docker build -t <image_name> ..
● Build an image without the cache using docker build -t <image_name> .
--no-cache.
● List all local Docker images with docker images.
● Delete a specific image using docker rmi <image_name>.
● Remove all unused images with docker image prune.
Docker Hub
● Log in to Docker Hub with docker login -u <username>.
● Publish an image to Docker Hub using docker push <username>/<image_name>.
● Search for an image on Docker Hub with docker search <image_name>.
● Pull an image from Docker Hub using docker pull <image_name>.
Containers
● Create and run a container from an image with a custom name: docker run --name
<container_name> <image_name>.
● Run a container and publish its ports to the host: docker run -p
<host_port>:<container_port> <image_name>.
● Run a container in the background using docker run -d <image_name>.
● Start an existing container with docker start <container_name>.
● Stop a running container with docker stop <container_name>.
● Remove a stopped container using docker rm <container_name>.
● Open a shell inside a running container using docker exec -it
<container_name> sh.
● Fetch and follow the logs of a container with docker logs -f <container_name>.
● Inspect a running container using docker inspect <container_name>.
● List currently running containers with docker ps.
● List all containers (running and stopped) using docker ps --all.
● View resource usage stats with docker container stats.
Typical Docker File
FROM ubuntu:20.04
FROM node:16
WORKDIR /app
COPY /home/rahul/app/ .
RUN npm install #during build time
ENV PORT=5000
EXPOSE 5000
CMD [“npm”,”start”]
docker build -t rahul/test_app:1.0 . #building image running that image from docker file in current directory
docker run -p 3000:5000 rahul/test_app:1.0 #running the image as a process aka container
Docker Volumes
1. Create a docker volume: docker volume create my_data1
2. List volumes: docker volume ls
3. Inspect a volume: docker volume inspect my_data1
4. Remove a volume: docker volume rm my_data1
5. Mount volume to a container: docker run -v my_data1:/app/data test_app:1.0
6. Mount host directory: docker run -v /host/path:/container/path image-name
Docker Networking
1. Creating bridge network: docker network create mongodb #mongodb is network name
Docker compose
1.start services: docker compose up
2.start in detached mode: docker compose up -d
3.stop services : docker compose down
4.LIST containers: docker compose ps
YAML FILE EXAMPLE