Basics
Creating and Running a Container from an Image
To run a container using an image:
docker run <image-name>
This command is a one-step process to create and start a container from an image.
Listing All Running Containers
To see all currently running containers:
docker ps
Creating a Container without Starting It
To create a container without starting it:
docker create <image-name>
Once created, you can start it with:
docker start <container-id>
Fetching Logs from a Container
To get logs from a running or stopped container:
docker logs <container-id>
This command is invaluable for debugging and monitoring container activity.
Stopping Containers
To stop a running container gracefully:
docker stop <container-id>
This will send a SIGTERM
signal, allowing the container to shut down gracefully.
Forcibly Terminating Containers
To kill a container, effectively sending a SIGKILL
:
docker kill <container-id>
Use this for unresponsive containers that need to be shut down immediately.
Copying Files to/from a Container
To copy files from your local filesystem to a container:
docker cp <path-to-local-file> <container-id>:<path-to-container-destination>
And to copy files from a container to your local filesystem:
docker cp <container-id>:<path-to-container-file> <path-to-local-destination>
This command is crucial for managing files within a container, especially for configurations and data persistence.
Advanced
Cleaning Up
To delete stopped containers:
docker system prune
This command will clean up unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
,