Managing Docker Containers
Now that you know how to list your Docker containers, let's explore some common container management tasks.
Starting and Stopping Containers
To start a stopped container, use the following command:
docker start my-container
To stop a running container, use the following command:
docker stop my-container
Restarting Containers
If you need to restart a running container, you can use the docker restart
command:
docker restart my-container
This will stop the container and then start it again.
Removing Containers
To remove a container from your system, you can use the docker rm
command:
docker rm my-container
This will remove the container, but not the image it was created from.
Executing Commands in Containers
Sometimes, you may need to execute a command inside a running container. You can do this using the docker exec
command:
docker exec -it my-container /bin/bash
This will start an interactive shell session inside the container.
Managing Container Resources
You can also manage the resources (CPU, memory, etc.) allocated to a container using various options when creating or running the container. For example, to limit a container to using a maximum of 2 CPU cores and 512MB of RAM, you can use the following command:
docker run -c 2 -m 512m my-image
By understanding these container management tasks, you'll be able to effectively control and maintain your Docker containers on your system.