Table of Contents
Introduction Manage Docker Containers
Manage Docker Containers: In this guide we are about to cover how to manage Docker containers by starting, stopping, resuming and removing. At last, we are about to see how to manage Docker images.
Those who are new to docker follow all step by performing each in a test environment will help to improve yourself in docker container management.
Articles related to Docker Series
- How to install Docker on Red Hat Enterprise Linux and CentOS Linux 7
- How to search for docker images and launch a container
- How to connect Docker containers and expose the network
- How to manage Docker containers
Step 1: Creating and working with Docker containers
Before starting with below guide let us start a few numbers of containers to perform our workout. Already we have existing nginx images let start four numbers of containers in a random name.
# docker run -d --name linuxsysadmins.com_test nginx # docker run -d --name linuxsysadmins.com_dev nginx # docker run -d --name linuxsysadmins.com_prod1 nginx # docker run -d --name linuxsysadmins.com_prod2 nginx
In our Docker server, we may have few stopped and running containers, if we need to know only with running containers use “ps“. Maybe if we required to list all running and stopped containers use “ps -a“.
# docker ps # docker ps -a
In case you need to cat anyone of the file or to check the environment variable in a running container without login, Use “cat” command with “exec” option as follows. Using exec option with “Docker” command which will help us to execute commands in remote containers.
# docker exec linuxsysadmins.com_prod2 cat /etc/hosts # docker exec linuxsysadmins.com_prod2 env
Step 2: Stopping, restarting and kill a docker container
If a stop required for any one of the containers it can be done using the “stop” option and list to verify by running “docker ps“.
# docker stop linuxsysadmins.com_dev nginx # docker ps # docker ps -a
In case if you need to stop all running containers it can be accomplished in a single go. To stop all at once first we need to fetch all running “CONTAINER ID” with “ps” option to combine in a variable and issue stop command. This will stop all running containers by using their ID. Stopping container is similar to grace shutdown.
# docker stop $(docker ps -q)
- ps To list all running container
- -q To print only container ID
- First list all running containers
- Stop all running containers using its ID
- Again list the running containers ( Now all are stopped )
- Now list to see all stopped containers.
To restart a container we can use the “restart” option with “docker” command. First let us verify the uptime of linuxsysadmins.com_test container and initiate a restart, by following check for uptime again.
# docker restart linuxsysadmins.com_test # docker ps
Pause and unpause a running container
It is possible to pause all processes within a running container using “pause“. To resume the paused processes use “unpause” option with docker command.
Killing or PowerOFF a running container
This is similar to press and holding a power button of a Server or pull out the power cable from a server.
# docker kill linuxsysadmins.com_prod2 # docker kill -s SIGKILL linuxsysadmins.com_prod2
Step 3: Removing Docker containers and images.
Now let us see how to remove a container and docker images. Before removing with containers we need to stop it.
# docker ps # docker stop linuxsysadmins.com_test # docker rm linuxsysadmins.com_test # docker ps -a
Removing Docker images
Finally, let see how to delete a docker image using “rmi” option.
We can’t remove a docker image if any one of the running container using it. Before starting with removing an image we need to stop all containers running from it.
This will throw an error.
# docker images # docker ps # docker rmi nginx
Stop the container and remove the image “nginx”.
First, let us stop all containers and remove it.
# docker stop $(docker ps -aq) # docker rm $(docker ps -aq)
By the following list all running and stopped containers to verify.
# docker ps # docker ps -a
At last, remove the docker image and list to verify.
# docker rmi nginx # docker images
Conclusion
We have the walkthrough a detailed guide on how to manage Docker containers by starting, stopping, pausing and deleting etc. In the next guide let us come up with interesting topics about docker. Till then subscribe to our newsletter to receive updates about other articles.