Getting stuck on an unresponsive Docker Container that’s not stopping no matter what you try? We’re here with a bunch of solutions!
If you are someone who started a docker container but your containers have refused to shut down, fear not, for you have come to the perfect place! 😎
Today we will be going over multiple ways to end containers using different commands and even go as far as to set up a service of dockers using the dockers compose tool, which we will shut down later on. 😇
Table of Contents
4 Ways to Stop Docker Containers and Fix Docker Container Not Stopping
Method 1: Ending Containers with the Docker Stop Containers Command
The first method to stop the docker container is through the docker stop containers command. If you have the name or an ID of a running container, you can use them with the docker stop containers command to stop the container.
# Stop the container through the name docker stop mystifying_hofstadter # Stop the container by using the ID docker stop fb88ea50709
This command can also be used for multiple container IDs at once if you have more than one docker you want to stop. For this, first, you need to list all the containers via the docker ps command and then pass them through the docker stop command.😱
# List every running container docker ps # Stop the following Docker containers via the container ID docker stop a427asda3c5558 e9db12316f24c 31123ad5999e
If you want to stop all the running containers without retrieving the container IDs manually, use the docker ps-q command. The -q parameter of the docker ps command only outputs the IDs of the containers.🎉
# Create three containers with a semi-colon separating each command docker run -d -t ubuntu; docker run -d -t ubuntu; docker run -d -t ubuntu # List every running container, so we can get their IDs docker ps -q # Retrieve all running container IDs and pass those to docker stop docker stop $(docker ps -q)
Method 2: Stopping a Container Using Process Signals
Previously we looked at the docker stop command to stop a container, but sometimes we may not want that behaviour; rather, we can instruct docker to use alternative stop signals. This can be achieved through the docker run command –stop-signal flag.
With this, you can specify the stop signal when starting a container. The flag will set the signal you will need to send to a running container in order to stop it.
docker run -d -t --stop-signal SIGQUIT ubuntu
In this code, we see the flag taking the value SIGQUIT; this tells the docker to send a SIGQUIT signal that it wants to stop the container. This example also uses a few other parameters:👩💻
- d- runs the container in detached mode. Due to this, the user cannot immediately interact with the container as it is running in the background.
- t – allocates a Pseudo-TTY (PTY) console. This emulates a real terminal console in the container, which stops the Ubuntu container to ensure it doesn’t exit immediately; the Ubuntu container in the mentioned example requires you to have a PTY console if you want the application to work in the background.
- “Image name” – This shows which container we provision the docker image from; in this case, it’s the ubuntu image.
Exiting Containers Immediately With Docker Kill
Sometimes you will want to exit containers instantly without giving them any grace period; we have the docker kill command.
docker kill 2aa318273db5
This example shows that a container with an ID 2aa333373db5 has been passed through the docker kill command. This container will immediately exit via the SIGKILL signal, and as a result, it will not have any grace period.☠️
Method 3: Stopping and Removing a Container With Docker RM
The docker RN command can remove and force-stop a container with minimal impact. The main process receives a SIGKILL signal from Docker, which also removes the container from the available containers list present within your Docker installation.
docker rm kind_galileo
In the example above, we can see that the docker RM command is being used to remove the container named kind_galileo, but in order for us to remove the container, it needs to be stopped. To check the status of a docker container, you may use the docker ps -a command.
docker rm --force laughing_elion
In the example above, we see that the docker RN command and –force command are used to stop and remove a container in a single operation. The force command stops the container while the docker RM command removes the container entirely.😎
Method 4: Stopping Docker Containers With Docker Compose
Typically docker commands are only used on a single container. However, you can create multiple services with the aid of containers using the docker-compose tool, which allows you to configure a service containing multiple containers to work together.
This is used to stop a whole service of containers rather than stopping each container one by one. 🥳
If you have a service running multiple containers [docker-composestop] stops the whole service without removing the containers.
This whole process is listed below:
- First, create a directory in order to store your main configuration file:
- Next, create the docker-compose.yml file that contains the following configuration:
*The below Docker Compose file constructs a service with two Ubuntu containers.* # First Ubuntu Container container1: image: ubuntu tty: true # Second Ubuntu Container container2: image: ubuntu tty: true
- In a terminal session, move to your custom docker configuration file directory and run the docker-compose up -d in order to generate and start the Docker service.
- Lastly, you can issue the command docker-compose stop to end the service and containers in the same terminal session and directory.
Docker containers are a great tool for creating unique development and production environments. You have learned multiple ways to stop one or more docker containers through this article. You also learned about stop signals and how they may be used to stop containers using appropriate commands.🤓
With his information, you now know the ways to stop docker containers and even do a docker-compose service, activate it, and finally, shut it down. We hope this helped and you may be able to stop any new container you may come across.
Lastly, let us know in the comments:
- Were you able to stop your Docker container?
- Have you found a simpler method to zap shut Docker containers that can do all this and more with less effort?
- What kind of project are you intending to create using Dockers?
- Are there any other points or useful information you believe we should’ve mentioned?
Feel free to share this article amongst your friends and peers so that they can code faster, smarter and simpler!