Docker cheat sheet
Run, build and clean up containers. Replace names in angle brackets.
Containers
docker run -d -p 8080:80 --name web nginx- Run in the background, host port 8080 to container port 80
docker run --rm -it ubuntu bash- Interactive container removed on exit
docker ps- Running containers (-a for all)
docker stop <name>- Stop a container
docker start <name>- Start a stopped container
docker rm <name>- Remove a stopped container
docker exec -it <name> sh- Open a shell in a running container
docker logs -f <name>- Follow container logs
Images
docker pull <image>:<tag>- Download an image
docker images- List local images
docker build -t <name>:<tag> .- Build from the Dockerfile here
docker tag <image> <repo>/<name>:<tag>- Add a name for pushing
docker push <repo>/<name>:<tag>- Upload an image to a registry
docker rmi <image>- Remove an image
Volumes and cleanup
docker volume ls- List volumes
docker run -v <volume>:/data <image>- Mount a volume
docker system df- Disk used by Docker
docker system prune- Remove stopped containers, unused networks, dangling images
Dockerfile
FROM <image>:<tag>- Base image
WORKDIR /app- Working folder in the image
COPY . .- Copy files into the image
RUN <command>- Run a command at build time
EXPOSE 8000- Document the port used
ENV KEY=value- Set an environment variable
CMD ["node", "index.js"]- Default command at start
Compose
docker compose up -d- Start all services in the background
docker compose ps- Service status
docker compose logs -f <service>- Follow a service log
docker compose down- Stop and remove (add -v to delete volumes)
docker compose build- Rebuild images
Learn it properly