Start container for x minutes

Is it possible to launch the container for X minutes ?
i would like to activate the vpn container for 10 minutes and next the 10 minutes the container stop automatically.

This needs to be handled outside of docker.

You could create a simple bash script that:

  • starts the container
  • sleeps 10 minutes
  • stops the container

it’s a solution thanks
but no param in docker ?

No. Docker has no option to stop a container after a certain time. You need to script that.

when i execute the start and stop, the container is a exited status
is it possible to down of ram the container ?.
When i start the server the container is not in the list of executing container because the status restart mode is off

Please explain. A stopped container does not occupy ram.

Makes sense, doesn’t it? If you want your container to be started with the server, then you could use a systemd unit or use whatever your os supports during startup to run commands.

You could use something like this:

Create file /etc/systemd/system/docker.mycontainer.service, where mycontainer can be changed to whatever you like. It does not even have to match the description inside the unit block:

ExecStart=/usr/bin/docker run --rm --name %n myimage

--rm remove the container if stopped.
--name %n will use the description as container name. You want it to be a single word
myimage needs to be replaced with the image you want to use.

Then start the systemd unit: systemctl start docker.mycontainer. To make it start each time the server starts use systemctl enable docker.mycontainer

Note: the service WILL be marked as failed after it reached the RuntimeMaxSec, and the container will be deleted then.

Note2: all of the above needs to be done as root.

Please bear in mind that this is only to give you an idea how a solution could look like. If it doesn’t work on the spot or doesn’t work as you want it, please research the topic. Starting processes on server start is not a problem in the docker domain, it is a problem of the os domain.