Basic Docker Commands with Example Use Cases
Below is a list of basic Docker commands along with examples of their usage.
1. docker run
Starts a new container from an image.
docker run -d --name my_container my_image
This command starts a container named my_container
in detached mode (-d
) from the my_image
image.
2. docker build
Builds a Docker image from a Dockerfile.
docker build -t my_image:latest .
This command builds an image from the current directory (.
) and names it my_image
with the tag latest
.
3. docker ps
Displays a list of running containers.
docker ps
This command shows all running containers, their IDs, names, and statuses.
4. docker stop
Stops a running container.
docker stop my_container
This command stops the container named my_container
.
5. docker rm
Removes a stopped container.
docker rm my_container
This command removes the container named my_container
.
6. docker rmi
Removes a Docker image.
docker rmi my_image:latest
This command removes the image named my_image
with the tag latest
.
7. docker pull
Downloads an image from a Docker registry (e.g., Docker Hub).
docker pull ubuntu:latest
This command pulls the ubuntu
image with the latest
tag from Docker Hub.
8. docker push
Uploads an image to a Docker registry.
docker push myrepo/my_image:latest
This command pushes the image my_image
with the latest
tag to the myrepo
registry.
9. docker exec
Executes a command in a running container.
docker exec -it my_container bash
This command opens an interactive bash shell in the running container my_container
.
10. docker logs
Displays logs from a running container.
docker logs my_container
This command shows the logs from the container named my_container
.
11. docker network create
Creates a new Docker network.
docker network create my_bridge_network
This command creates a bridge network named my_bridge_network
.
12. docker network ls
Lists all Docker networks.
docker network ls
This command lists all Docker networks available on the host.
13. docker-compose up
Starts containers defined in a docker-compose.yml
file.
docker-compose up -d
This command starts all containers defined in the docker-compose.yml
file in detached mode (-d
).
14. docker-compose down
Stops and removes containers, networks, and volumes defined in a docker-compose.yml
file.
docker-compose down
This command stops and removes all resources defined in the docker-compose.yml
file.
These basic Docker commands help with the daily management of containers, images, and networks. With them, you can easily start, stop, remove, and monitor containerized applications.