I'm trying to stop a docker container from within an alpine image:
> docker run -ti alpine sh
/ # poweroff
/ # poweroff -f
poweroff: Operation not permitted
/ # halt
/ # halt -f
halt: Operation not permitted
/ # whoami
root
Do you see what is the issue with this?
To stop one or more running Docker containers, you can use the docker stop command. The syntax is simple: $ docker stop [OPTIONS] CONTAINER [CONTAINER...] You can specify one or more containers to stop.
To stop a container you use the docker stop command and pass the name of the container and the number of seconds before a container is killed. The default number of seconds the command will wait before the killing is 10 seconds.
To stop this container, press Ctrl+C in the terminal where it was started. Alternatively, you may start a container in detached mode using the -d option. To see all running containers, use the docker ps command.
To run the Alpine Image Docker Container, you can use the Docker run command. Once the Image is loaded, it opens up the shell for you automatically. To install python 3 inside the Alpine Container, you can use the apk add command inside the shell. You can install the My-SQL client using the following command.
Creating an Alpine Docker Container. 1 Step 1: Pull and Run the Alpine Image. 2 Step 2: Install Python 3. 3 Step 3: Install MySQL inside the Container. 4 Step 4: Install Firefox. 5 Step 5: Commit the changes in the Image.
Imagine a scenario where you want to remove a docker image but you’ll have to stop all the associated running containers. You may provide the container names or IDs one by one but that’s time consuming. What you can do is to filter all the running containers based on their base image.
The Docker commands to do this are quite simple. To save a Docker container, we just need to use the docker commit command like this: You can see there is a new image there. It does not have a repository or tag, but it exists. This is an image created from the running container. Let’s tag it so it will be easier to find later.
You cannot stop a docker image this way.
First, if poweroff
had to function (and it did in the past, due to an issue) it would shutdown the entire computer, because of how the poweroff
binary is working and power halting mechanic is designed on Linux and hardware.
What you have to do in order to properly shutdown your container is to quit the entrypoint (exit
in shell), or send a signal to this process (eg: docker stop
sends SIGTERM
to the running entrypoint before killing it after a period of grace).
If you really want to shutdown the host computer from within a container (why would you ever want to do that?), you can activate the --privileged
option which will give all power to your root within the container, and then do:
echo 1 > /proc/sys/kernel/sysrq; echo o > /proc/sysrq-trigger
Be careful, this will really shut down the host, and in a brutal manner. Writing 1
in sysrq
will activate sysrq
kernel features, which allows to make keyboard requests to the kernel with the SysRq key but also through the sysrq-trigger
file. o
means poweroff.
Fedora Project - Sysrq
You need to terminate the process sh
, simply with this:
exit
From within the container. Think a container as an isolated process, not as a virtual machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With