Docker Cheatsheet
Getting Started
The getting started guide on Docker has detailed instructions for setting up Docker.
After setup is complete, run the following commands to verify the success of installation:
Docker Cheatsheet for Beginners
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.
Running and managing containers
Run a container from an image
docker 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
Managing images
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
Managing container resources
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
Managing container metadata
docker run --name=<name> <image>
: Assign a name to a containerdocker run -e <key>=<value> <image>
: Set an environment variable in a container
Managing container users
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
Managing container volumes
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
Managing container networks
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
Managing container ports
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
Managing container security
docker run --security-opt=<option> <image>
: Set a security option for a container
Managing container health
docker run --health-cmd=<command> <image>
: Set a command to be used to check the health of a container
Managing container capabilities
docker run --cap-add=<capability> <image>
: Add a capability to a containerdocker run --cap-drop=<capability> <image>
: Drop a capability from a container
Managing container isolation
docker run --isolation=<isolation> <image>
: Set the isolation level for a container
Managing container storage
docker run --storage-opt=<option> <image>
: Set a storage option for a container
Managing container labels
docker run --label <key>=<value> <image>
: Add a label to a container
Managing container UTS namespace
docker run --uts=<uts> <image>
: Set the UTS namespace mode for a container
Managing a Swarm
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
Managing Swarm services
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
Managing Swarm nodes
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
Managing Compose projects
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
Managing Compose services
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
Managing Compose configuration
docker-compose config
: Validate and view the Compose filedocker-compose config > <file>
: Generate a Compose file in the specified format
Managing Compose environments
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
For More please Visit Docker Docker free training Docker repository Alpine Linux
Follow me on : Medium Linkedin Researchgate