This cheatsheet is a beginner-friendly guide to Docker commands, covering the most commonly used commands for running and managing containers, working with images, and networking. It also includes a section on Docker Compose, which is a tool for defining and running multi-container applications. This cheatsheet is designed to help new users get started with Docker and become familiar with the most essential commands in a concise and easy-to-follow format.
docker run <image>
: Run a container from an imagedocker start <container_id>
: Start a stopped containerdocker stop <container_id>
: Stop a running containerdocker kill <container_id>
: Forcefully stop a running containerdocker attach <container_id>
: Attach to a running containerdocker exec -it <container_id> <command>
: Run a command in an interactive shell in a running containerdocker rm <container_id>
: Remove a stopped containerdocker inspect <container_id>
: Inspect a containerdocker logs <container_id>
: View logs for a containerdocker top <container_id>
: View processes in a container
docker images
: List available imagesdocker rmi <image_id>
: Remove an imagedocker history <image_id>
: View the history of an imagedocker pull <image>
: Download an image from a registry (e.g., Docker Hub)docker build -t <name> .
: Build an image from aDockerfile
in the current directory and give it the specified namedocker push <image>
: Push an image to a registrydocker tag <image_id> <tag>
: Tag an image
docker run --cpus=<num> <image>
: Limit the number of CPU cores available to a containerdocker run --memory=<size> <image>
: Limit the amount of memory available to a container
docker run --name=<name> <image>
: Assign a name to a containerdocker run -e <key>=<value> <image>
: Set an environment variable in a container
docker run --user=<user> <image>
: Run a container as a specific userdocker run --user=<uid>:<gid> <image>
: Run a container as a specific user and group
docker run -v <host_path>:<container_path> <image>
: Bind mount a host path to a container pathdocker run -v <host_path>:<container_path>:ro <image>
: Bind mount a host path to a container path in read-only modedocker run -v <container_path> <image>
: Create a data volume in a containerdocker run --mount type=volume,src=<volume_name>,dst=<container_path> <image>
: Use a named volume in a container
docker run --network <network> <image>
: Connect a container to a networkdocker run --network <network> --ip <ip_address> <image>
: Connect a container to a network and specify its IP addressdocker network create <network>
: Create a new networkdocker network rm <network>
: Remove a network
docker run -p <host_port>:<container_port> <image>
: Publish a container port to the hostdocker port <container_id>
: View the port mappings for a container
docker run --security-opt=<option> <image>
: Set a security option for a container
docker run --health-cmd=<command> <image>
: Set a command to be used to check the health of a container
docker run --cap-add=<capability> <image>
: Add a capability to a containerdocker run --cap-drop=<capability> <image>
: Drop a capability from a container
docker run --isolation=<isolation> <image>
: Set the isolation level for a container
docker run --storage-opt=<option> <image>
: Set a storage option for a container
docker run --label <key>=<value> <image>
: Add a label to a container
docker run --uts=<uts> <image>
: Set the UTS namespace mode for a container
docker swarm init
: Initialize a Swarmdocker swarm join
: Join a Swarm as a workerdocker swarm join-token worker
: View the command to join a Swarm as a workerdocker swarm leave
: Leave a Swarm
docker service create <image>
: Create a new servicedocker service inspect <service_id>
: Inspect a servicedocker service logs <service_id>
: View logs for a servicedocker service ls
: List all servicesdocker service tasks <service_id>
: List tasks of a servicedocker service rm <service_id>
: Remove a servicedocker service rollback <service_id>
: Roll back a servicedocker service scale <service_id>=<replicas>
: Scale a servicedocker service update <service_id> <options>
: Update the configuration of a service
docker node demote <node_id>
: Demote a manager nodedocker node inspect <node_id>
: Inspect a nodedocker node ls
: List all nodesdocker node promote <node_id>
: Promote a worker nodedocker node update <node_id> <options>
: Update the configuration of a node
docker-compose up
: Start and recreate containersdocker-compose up -d
: Start containers in detached modedocker-compose up --no-recreate
: Start containers without recreating themdocker-compose up --force-recreate
: Recreate containers before starting themdocker-compose down
: Stop and remove containersdocker-compose stop
: Stop containersdocker-compose start
: Start stopped containersdocker-compose restart
: Restart running containersdocker-compose build
: Build images before starting containersdocker-compose pull
: Pull images before starting containers
docker-compose run <service> <command>
: Run a command in a new containerdocker-compose exec <service> <command>
: Run a command in an existing containerdocker-compose up --no-deps <service>
: Start a service without starting linked services
docker-compose config
: Validate and view the Compose filedocker-compose config > <file>
: Generate a Compose file in the specified format
docker-compose -f <file> <command>
: Use the specified Compose file for the commanddocker-compose --env-file <file> <command>
: Load environment variables from the specified file for the commanddocker-compose --project-name <name> <command>
: Set the project name for the command