Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a docker container from within an alpine image

Tags:

docker

alpine

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?

like image 678
Joan Avatar asked Jul 15 '17 23:07

Joan


People also ask

How do I stop a docker container?

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.

How do I stop docker from using images?

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.

How do you stop the container in detached mode?

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.

How do I run an alpine image in Docker?

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.

How do I install Firefox on an alpine Docker container?

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.

How to remove a docker image from all the running containers?

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.

How to save a docker container?

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.


2 Answers

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

like image 150
Fabien Avatar answered Oct 03 '22 06:10

Fabien


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.

like image 44
Robert Avatar answered Oct 03 '22 06:10

Robert