Docker CLI Commands
Prerequisites
1. What is Docker CLI Commands
This section introduces commonly used Docker commands with clear explanations and examples.
2. CLI Commands
1
2
3
4
5
6
7
8
9
10
| docker run <image>
docker run -it --entrypoint bash <image>
docker start <container_id>
docker stop <container_id>
docker rm <container_id>
docker rm -f $(docker ps -aq)
docker rm -f $(docker ps -q)
docker cp <container_id>:<path> <host_path>
docker commit <container_id> <new_image_name>
docker rmi <image>
|
2-1. Run Container
Creates and starts a new container from an image.
Example:
2-2. Run with Interactive Shell
1
| docker run -it --entrypoint bash <image>
|
-it → interactive terminal--entrypoint bash → override default command
Example:
1
| docker run -it --entrypoint bash ubuntu
|
Useful for debugging inside container
2-3. Start Existing Container
1
| docker start <container_id>
|
Starts a stopped container.
2-4. Stop Running Container
1
| docker stop <container_id>
|
Gracefully stops a running container.
2-5. Remove Container
1
| docker rm <container_id>
|
Deletes a stopped container.
2-6. Force Remove Container
1
| docker rm -f <container_id>
|
- Stops + removes container immediately
2-7. Remove All Containers
1
| docker rm -f $(docker ps -aq)
|
docker ps -aq → all container IDs- Removes everything (running + stopped)
2-8. Remove Running Containers Only
1
| docker rm -f $(docker ps -q)
|
docker ps -q → running containers only
2-9. Copy Files Between Host and Container
1
| docker cp <container_id>:<path> <host_path>
|
Example:
1
| docker cp my_container:/imgs C:\backup
|
2-10. Create Image from Container (Commit)
1
| docker commit <container_id> <new_image_name>
|
Example:
1
| docker commit 123abc my-image
|
Saves container state as a new image
Not recommended for production → use Dockerfile instead
2-11. Remove image
Deletes a stopped image.