Managing Docker Containers
Listing Docker Containers
To list all running Docker containers, you can use the docker ps
command:
docker ps
This will display a table with information about the running containers, including the container ID, the image used, the command being executed, the creation time, the status, and the ports.
To list all containers, including stopped ones, you can use the docker ps -a
command:
docker ps -a
Stopping and Starting Containers
To stop a running container, you can use the docker stop
command followed by the container ID or name:
docker stop container_id
To start a stopped container, you can use the docker start
command:
docker start container_id
Removing Containers
To remove a container, you can use the docker rm
command:
docker rm container_id
This will remove the container, but not the image it was created from.
Inspecting Containers
To get detailed information about a container, you can use the docker inspect
command:
docker inspect container_id
This will output a JSON object with information about the container, including its configuration, network settings, and more.
Monitoring Containers
To monitor the resource usage and performance of a container, you can use the docker stats
command:
docker stats container_id
This will display a live stream of CPU, memory, network, and I/O usage for the specified container.
Managing Container Logs
To view the logs of a container, you can use the docker logs
command:
docker logs container_id
This will display the standard output and standard error logs of the container.
Tagging and Pushing Images
Once you have built a Docker image, you can tag it with a name and push it to a registry, such as Docker Hub, so that others can use it.
## Tag the image
docker tag image_name:tag username/image_name:tag
## Push the image to Docker Hub
docker push username/image_name:tag
This allows you to share your custom Docker images with others or use them in different environments.