Docker basic commands¶
Docker is a platform for developing, shipping, and running applications in containers.^[600-developer-docker-docker.md] This page outlines the essential command-line operations for managing images, containers, and interacting with the Docker daemon.
Image Management¶
Images are read-only templates used to create containers.^[600-developer-docker-docker.md]
To list all images currently stored on the local machine:
docker image ls
docker images
To build an image from a Dockerfile in the current directory:
docker build -t friendlyhello .
To remove a specific image, use the image ID:
docker image rm <image id>
docker rmi <image id>
To prepare an image for uploading to a registry (like Docker Hub), tag it:
docker tag <image> username/repository:tag
To upload the tagged image to a registry:
docker push username/repository:tag
Container Lifecycle¶
Containers are runnable instances of images.^[600-developer-docker-docker.md]
Running Containers¶
To run a container, mapping the host port to the container port:
# Run in foreground (port 4000 on host maps to 80 in container)
docker run -p 4000:80 friendlyhello
To run a container in the background (detached mode):
docker run -d -p 4000:80 friendlyhello
To run a specific command in a detached container:
docker run -d centos /bin/bash -c "while true;do echo 123; sleep 2; done"
Listing and Inspecting¶
To list running containers:
docker container ls
docker ps
To list all containers, including those that are stopped:
docker container ls -a
docker ps -a
To view the logs of a container (follow mode with tail):
docker logs -t -f --tail 3 <container ID>
To view the running processes of a container:
docker top <container ID>
To inspect the low-level details of a container:
docker inspect <container ID>
Stopping and Removing¶
To gracefully stop a running container:
docker container stop <hash>
To forcefully shutdown a container immediately:
docker container kill <hash>
To remove a specific container:
docker container rm <hash>
docker rm <container ID>
To remove all containers (e.g., stopped ones), you can combine commands:
docker container rm $(docker container ls -a -q)
Interaction and Execution¶
To attach your terminal's standard input, output, and error to a running container:
docker attach <container ID>
To execute a command inside a running container (e.g., run ls -l in /tmp):
docker exec -t <container ID> "ls -l /tmp"
To open an interactive shell (bash) inside a container:
docker exec -t <container ID> /bin/bash
System Information and Registry¶
To check the version of the Docker client and daemon:
docker version
To display system-wide information:
docker info
To log in to a Docker Registry (such as Docker Hub):
docker login
To display help information:
docker --help
Troubleshooting Removal Conflicts¶
You cannot remove an image (docker rmi) if it is currently being used by a stopped container^[600-developer-docker-docker.md]. You must first remove the container using its ID before removing the image^[600-developer-docker-docker.md].
Sources¶
^[600-developer-docker-docker.md]