Docker has revolutionized the way we build, ship, and run applications.
In this article, we’ll explore the most useful Docker commands every developer should know.
1. Installation and Setup
Before diving into Docker commands, ensure Docker is installed on your machine. You can download Docker Desktop from the official Docker website.
2. Basic Docker Commands
docker run
Run a Docker container from an image. If the image is not present locally, Docker will pull it from the Docker Hub.
docker run hello-world
docker pull
Download an image from a Docker registry.
docker pull nginx
docker ps
List running Docker containers.
docker ps
To include stopped containers, use:
docker ps -a
docker stop
Stop a running container.
docker stop <container_id>
docker rm
Remove a stopped container.
docker rm <container_id>
docker rmi
Remove an image.
docker rmi <image_id>
3. Image Management
docker build
Build an image from a Dockerfile.
docker build -t myapp .
docker images
List all Docker images on your machine.
docker images
docker tag
Tag an image for a specific repository.
docker tag myapp myrepo/myapp:latest
docker push
Push an image to a Docker registry.
docker push myrepo/myapp:latest
4. Container Management
docker exec
Run a command in a running container. This is useful for debugging and inspection.
docker exec -it <container_id> /bin/bash
docker logs
View the logs of a container.
docker logs <container_id>
docker inspect
Inspect the details of a container or image.
docker inspect <container_id>
docker-compose
Docker Compose is a tool for defining and running multi-container Docker applications. Define your services in a docker-compose.yml
file, and then use the following commands:
docker-compose up
Start and run your entire application defined in docker-compose.yml
.
docker-compose up
docker-compose down
Stop and remove all containers defined in docker-compose.yml
.
docker-compose down
5. Networking and Volumes
docker network ls
List all Docker networks.
docker network ls
docker network create
Create a new Docker network.
docker network create mynetwork
docker volume ls
List all Docker volumes.
docker volume ls
docker volume create
Create a new Docker volume.
docker volume create myvolume
docker run
with Networking and Volumes
Run a container with a specific network and volume.
docker run -d --name mycontainer --network mynetwork -v myvolume:/data myimage
6. Cleanup Commands
docker system prune
Clean up unused Docker objects (containers, images, networks, and volumes).
docker system prune
docker volume prune
Remove all unused volumes.
docker volume prune
docker network prune
Remove all unused networks.
docker network prune
Top comments (0)