This provides a comprehensive overview of various Docker commands and their use cases. Here's a summary:
-
docker run
:- Used to run a container from an image.
- If the image isn't present on the host, Docker pulls it from Docker Hub (only the first time).
- Example:
docker run nginx
runs an instance of the nginx application.
-
docker ps
:- Lists all running containers and basic information (ID, image, status, name).
- Use
docker ps -a
to see all containers, including stopped or exited ones.
-
Stopping and Removing Containers:
- Use
docker stop [container ID or name]
to stop a container. - Use
docker rm [container ID or name]
to remove a stopped or exited container.
- Use
-
Managing Images:
docker images
lists available images on the host.- To remove an image, use
docker rmi [image name]
, ensuring no dependent containers are running.
-
docker pull
:- Use it to download an image without running a container.
- Example:
docker pull ubuntu
pulls the Ubuntu image.
-
Behavior of Containers:
- Containers are designed to run a specific task; they exit once the task is complete.
- Running a container from an OS image like Ubuntu will exit immediately if no task is specified.
-
Executing Commands in Containers:
docker exec [container ID or name] [command]
executes a command in a running container.- Example:
docker exec
to view the contents of a file in a container.
-
Running Containers in Foreground and Background:
- Default: Containers run in the foreground (attached mode).
- Use
-d
option to run in detached (background) mode. - Use
docker attach [container ID or name]
to reattach to a running container.
-
Example of a Web Application Container:
- Running a web application (e.g.,
xyz/simple-webapp
) that listens on a specific port.
- Running a web application (e.g.,
-
Short IDs in Commands:
- When specifying container IDs in commands, the initial characters are enough as long as they're unique.
This lecture is a great primer for those beginning with Docker, providing essential commands and insights into container behavior and management.