Docker has become one of the most popular tools in the DevOps and cloud world. Whether you are a developer, system admin, or just starting your journey in IT, understanding Docker commands is essential. In this blog-style guide, we will go through the most commonly used Docker commands and explain them in a simple, human-friendly way. You can read this like a crash course or use it as a quick reference whenever you are working with Docker.
Docker Basics
Let’s begin with the very basic commands. These commands are your day-to-day essentials when working with Docker containers and images.
- docker init → Creates starter files for Docker in your project.
- docker build -t friendlyname . → Builds a Docker image from your Dockerfile and tags it with a name.
- docker run -p 4000:80 friendlyname → Runs the image and maps your local port 4000 to container’s port 80.
- docker run -d -p 4000:80 friendlyname → Runs the container in detached mode (in the background).
- docker exec -it [container-id] bash → Gives you shell access inside a running container.
- docker ps → Lists all currently running containers.
- docker stop <hash> → Gracefully stops a container using its ID or hash.
- docker ps -a → Shows all containers (including stopped ones).
- docker kill <hash> → Force stops a container immediately.
- docker rm <hash> → Removes a container by ID.
- docker rm -f <hash> → Force removes a container.
- docker rm $(docker ps -a -q) → Removes ALL containers on the machine.
- docker images -a → Lists all images available locally.
- docker rmi <imagename> → Removes an image from the system.
- docker rmi $(docker images -q) → Deletes all images at once.
- docker logs <container-id> -f → Shows live logs of a running container.
- docker login → Logs into Docker Hub or your registry.
- docker tag <image> username/repository:tag → Tags an image for pushing to a registry.
- docker push username/repository:tag → Uploads the image to Docker Hub/registry.
- docker run username/repository:tag → Runs an image directly from a registry.
- docker system prune → Cleans up unused containers, images, networks.
- docker system prune -a → Aggressively removes ALL unused containers, images, and networks.
- docker volume prune → Removes unused volumes.
- docker network prune → Removes unused networks.
Docker Compose
Docker Compose is used when you want to run multi-container applications. Instead of running individual commands for each service, you can define them in a single YAML file and manage everything with simple commands.
- docker-compose up → Creates and starts all containers defined in docker-compose.yml.
- docker-compose up -d → Starts containers in detached mode (background).
- docker-compose down → Stops and removes containers, networks, images, and volumes.
- docker-compose logs → Shows logs of all services.
- docker-compose restart → Restarts all services.
- docker-compose pull → Pulls the latest images for services.
- docker-compose build → Builds or rebuilds the images.
- docker-compose config → Validates and displays the configuration.
- docker-compose scale <service>=<count> → Scales a particular service up or down.
- docker-compose top → Shows running processes inside services.
- docker-compose run -rm -p 2022:22 web bash → Runs a one-time command inside a service.
Docker Services (Swarm Mode)
Docker Services are used in Swarm mode, where you want to manage a cluster of Docker nodes. These commands are useful when deploying containers at scale.
- docker service create <options> <image> <command> → Creates a new service.
- docker service inspect --pretty <service> → Displays detailed info about a service.
- docker service ls → Lists all services.
- docker service ps → Lists tasks of a service.
- docker service scale <service>=<count> → Scales a service to the given number of replicas.
- docker service update <options> <service> → Updates service options.
Docker Stack
Docker Stack is used for deploying and managing multi-service applications in a swarm cluster. It works with a Compose file but runs across multiple machines.
- docker stack ls → Lists running applications on a Docker host.
- docker stack deploy -c <composefile> <app> → Deploys an app using a Compose file.
- docker stack services <app> → Lists services in a stack.
- docker stack ps <app> → Shows running containers in a stack.
- docker stack rm <app> → Removes a deployed stack.
Docker Machine
Docker Machine is used to provision and manage Docker hosts (VMs). Although it is less common now (due to Kubernetes and cloud providers), it is still useful in some environments.
- docker-machine create --driver virtualbox myvm1 → Creates a VM using VirtualBox.
- docker-machine create -d hyperv --hyperv-virtual-switch 'myswitch' myvm1 → Creates a VM in Windows with Hyper-V.
- docker-machine env myvm1 → Shows environment info for the node.
- docker-machine ssh myvm1 'docker node ls' → Lists nodes in a swarm.
- docker-machine ssh myvm1 'docker node inspect <id>' → Inspects a node.
- docker-machine ssh myvm1 'docker swarm join-token -q worker' → Gets worker join token.
- docker-machine ssh myvm1 → Opens SSH session into VM.
- docker-machine ssh myvm2 'docker swarm leave' → Removes a worker node from swarm.
- docker-machine ssh myvm1 'docker swarm leave -f' → Forces master to leave swarm.
- docker-machine start myvm1 → Starts a VM.
- docker-machine stop $(docker-machine ls -q) → Stops all VMs.
- docker-machine rm $(docker-machine ls -q) → Removes all VMs.
- docker-machine scp docker-compose.yml myvm1:~ → Copies files to VM.
- docker-machine ssh myvm1 'docker stack deploy -c <file> <app>' → Deploys an app to VM.
Real-world Use Cases of Docker Commands
Now that we’ve gone through the commands, let’s make this more practical. Here are some real-world scenarios where these Docker commands become very useful. This way, you’ll not only remember the command but also recall when to use it.
1. Docker Basics in Action
Imagine you are a developer building a Node.js application. You want to test your app in a container before deploying it anywhere.
• You would start by creating a `Dockerfile` with your app dependencies.
• Then run `docker build -t mynodeapp .` to build the image.
• Next, run `docker run -p 3000:3000 mynodeapp` and open `http://localhost:3000` to test it.
• If something breaks inside the container, `docker exec -it <id> bash` lets you debug.
2. Docker Compose for Multi-Container Apps
Suppose you are running a WordPress site with a MySQL database. Running them separately with `docker run` would be messy. This is where Docker Compose shines.
• You write a `docker-compose.yml` file that defines both WordPress and MySQL services.
• Run `docker-compose up -d` to launch the full stack.
• If you change the MySQL password, you can run `docker-compose restart` to apply changes.
• Logs across both services can be checked with `docker-compose logs`.
3. Docker Services in Production
Imagine your company runs a Python Flask API that needs to scale during high traffic hours. You’re using Docker Swarm for orchestration.
• First, create a service with `docker service create --name flaskapi -p 5000:5000 myflaskapp`.
• If traffic increases, scale with `docker service scale flaskapi=5`.
• Later, you might update the image with a new version using `docker service update --image myflaskapp:v2 flaskapi`.
4. Docker Stack for Teams
Now imagine you’re deploying a full e-commerce app that includes frontend, backend, database, and cache servers. Your team uses a `docker-compose.yml` file, but in production you deploy it across multiple nodes using Docker Stack.
• Run `docker stack deploy -c docker-compose.yml shopapp` to launch the entire app.
• Use `docker stack services shopapp` to see all services.
• If one service misbehaves, check `docker stack ps shopapp` for details.
5. Docker Machine for Local Clusters
Let’s say you are learning Docker Swarm and don’t have cloud servers. You can use Docker Machine to create a cluster of VirtualBox VMs right on your laptop.
• Run `docker-machine create --driver virtualbox node1` and `node2` to create two VMs.
• SSH into node1 with `docker-machine ssh node1` and initialize swarm.
Hope you find it helpful!!