Managing and Interacting with Containers
Now that you've learned how to start Docker containers, it's time to explore the various ways to manage and interact with them. Docker provides a rich set of commands and options to control the lifecycle and behavior of your containers.
Listing Running Containers
To view the list of currently running containers, you can use the docker ps
command:
## List all running containers
docker ps
## List all containers (running and stopped)
docker ps -a
This will display information about the running containers, such as the container ID, image, command, creation time, and status.
Stopping and Starting Containers
You can stop a running container using the docker stop
command:
## Stop a running container
docker stop my-app
To start a stopped container, you can use the docker start
command:
## Start a stopped container
docker start my-app
Removing Containers
If you no longer need a container, you can remove it using the docker rm
command:
## Remove a container
docker rm my-app
## Remove all stopped containers
docker rm $(docker ps -a -q)
Monitoring Containers
To monitor the logs and output of a running container, you can use the docker logs
command:
## View the logs of a container
docker logs my-app
You can also use the docker stats
command to view real-time performance metrics for your running containers:
## View performance statistics for running containers
docker stats
Copying Files to and from Containers
You can copy files between the host system and a running container using the docker cp
command:
## Copy a file from the host to the container
docker cp /path/on/host my-app:/path/in/container
## Copy a file from the container to the host
docker cp my-app:/path/in/container /path/on/host
By understanding these container management and interaction commands, you can effectively control and monitor the lifecycle of your Docker containers.