TechSchool
Home / Cheat sheets / Docker

Docker cheat sheet

96 one-line examples for Docker. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.

Container Basics

CodeWhat it does
docker run hello-worldStart a test container to check the install.
docker run -it ubuntu bashOpen an interactive shell inside a new container.
docker run -d nginxStart a container in the background.
docker run --name web nginxGive the container a fixed name.
docker run -p 8080:80 nginxMap host port 8080 to container port 80.
docker run --rm alpine echo hiDelete the container after it stops.
docker psList the containers that run now.
docker ps -aList all containers, including stopped ones.
docker stop webStop the container named web.
docker start webStart the stopped container named web.
docker rm webRemove the stopped container named web.
docker logs webShow the output of the container named web.

Images

CodeWhat it does
docker pull nginxDownload the nginx image from the registry.
docker imagesList the images on your machine.
docker build -t myapp .Build an image from the Dockerfile in this folder.
docker build -t myapp:v1 .Build an image and add a version tag.
docker tag myapp myapp:latestAdd a second tag to an image.
docker push myuser/myappUpload your image to Docker Hub.
docker rmi nginxRemove the nginx image from the disk.
docker image pruneDelete images that no container uses.
docker history myappShow the layers of the myapp image.
docker inspect nginxShow full details of the nginx image.
docker save -o app.tar myappSave the image to a tar file.
docker load -i app.tarLoad an image from a tar file.

Dockerfile

CodeWhat it does
FROM node:20-alpineSet the base image for the build.
WORKDIR /appSet the working folder inside the image.
COPY package.json .Copy one file into the image.
COPY . .Copy all files from the host to the image.
RUN npm installRun a command during the build.
ENV NODE_ENV=productionSet an environment variable in the image.
EXPOSE 3000Document the port the app uses.
CMD ["node", "server.js"]Set the default command for the container.
ENTRYPOINT ["node"]Set a fixed command that CMD extends.
USER nodeRun the container as the node user.
ARG VERSION=1.0Define a build-time variable with a default.
docker build --no-cache -t myapp .Build the image again without the cache.

Ports and Volumes

CodeWhat it does
docker run -p 3000:3000 myappPublish container port 3000 on the host.
docker run -P myappPublish all exposed ports on random host ports.
docker port webShow the port mappings of container web.
docker volume create dataCreate a named volume called data.
docker volume lsList all volumes on the machine.
docker run -v data:/app myappMount the volume data at /app.
docker run -v $(pwd):/app myappMount the current folder into the container.
docker run --mount type=bind,src=.,dst=/app myappMount the current folder with the mount flag.
docker volume inspect dataShow details of the volume named data.
docker volume rm dataRemove the volume named data.
docker volume pruneDelete volumes that no container uses.
docker run -v $(pwd):/app:ro myappMount the folder as read-only.

Compose

CodeWhat it does
docker compose upStart all services in the compose file.
docker compose up -dStart all services in the background.
docker compose downStop and remove the services and network.
docker compose psList the containers of this project.
docker compose logs -fFollow the logs of all services.
docker compose buildBuild the images for all services.
docker compose restart webRestart the web service.
docker compose exec web shOpen a shell in the running web service.
docker compose run web npm testRun a one-time command in a new container.
docker compose pullDownload the latest images for the services.
docker compose configCheck and print the final compose file.
docker compose down -vStop services and delete their volumes.

Inspect and Debug

CodeWhat it does
docker exec -it web shOpen a shell in the running container.
docker exec web ls /appRun one command inside the container.
docker logs -f webFollow the live output of the container.
docker logs --tail 50 webShow the last 50 log lines.
docker inspect webShow full details of the container.
docker statsShow live CPU and memory use.
docker top webList the processes inside the container.
docker diff webShow files changed since the container started.
docker cp web:/app/log.txt .Copy a file from the container to the host.
docker cp ./conf web:/app/confCopy a file from the host to the container.
docker eventsStream the events of the Docker daemon.
docker inspect -f '{{.State.Pid}}' webPrint one field with a format string.

Cleanup

CodeWhat it does
docker system dfShow how much disk space Docker uses.
docker system pruneDelete stopped containers and unused data.
docker system prune -aAlso delete all unused images.
docker container pruneDelete all stopped containers.
docker image prune -aDelete all images that no container uses.
docker network pruneDelete networks that no container uses.
docker rm -f webForce remove a running container.
docker rm $(docker ps -aq)Remove all containers, running or stopped.
docker rmi $(docker images -q)Remove all images on the machine.
docker stop $(docker ps -q)Stop all running containers.
docker system prune --volumesAlso delete unused volumes.
docker builder pruneDelete the build cache.

Networks and Registry

CodeWhat it does
docker network lsList the networks on the machine.
docker network create appnetCreate a user-defined network called appnet.
docker run --network appnet myappConnect the container to the appnet network.
docker network inspect appnetShow the containers in the network.
docker network connect appnet webAdd the running container to the network.
docker network disconnect appnet webRemove the container from the network.
docker network rm appnetRemove the network called appnet.
docker loginLog in to Docker Hub.
docker logoutLog out of Docker Hub.
docker search nginxSearch Docker Hub for images.
docker pull myuser/myapp:v1Download a tagged image from Docker Hub.
docker versionShow the client and server versions.
More on TechSchool
See the Docker interview questions Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Linux Git Data Structures