How to manage Docker containers

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.

Manage Docker Containers
Docker

Articles related to Docker Series

  1. How to install Docker on Red Hat Enterprise Linux and CentOS Linux 7
  2. How to search for docker images and launch a container
  3. How to connect Docker containers and expose the network
  4. 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
Starting docker containers
Starting Docker containers

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
Listing running and stopped docker containers
Listing running and stopped docker containers

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
Executing command in docker containers
Executing a command in Docker containers

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)
Stopping all docker containers
Stopping all docker containers
  • ps To list all running container
  • -q To print only container ID
  1. First list all running containers
  2. Stop all running containers using its ID
  3. Again list the running containers ( Now all are stopped )
  4. 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
Restarting docker containers
Restarting Docker containers

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.

Pausing and resuming docker containers
Pausing and resuming docker containers

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
Killing Docker container
Killing Docker container

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 containers
Removing Docker containers

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
Removing docker images
Removing docker images

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
Removing all docker containers and images
Removing all Docker containers and 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.