Podman/Docker - Keep a container running

Note: Replace podman with docker, if you are using Docker.

So. I’ve been using a Ubuntu container for various tests and I want to keep it alive to SSH/bash into it from time to time. There are some hacky solutions, but with this example, it should work for most images:

# Start the container with `-d` and `-i`!
$ podman run --rm --name keepingitrunning -d -i localhost/itt-ubu:24.04
7713969081f0d18a02b7dbf57bc8d02933f1fc3ebaa3aae98b0d56e585cde12b

$ podman exec -it keepingitrunning /bin/bash
root@7713969081f0:/home/nonroot# whoami
root
root@7713969081f0:/home/nonroot# exit
exit

$ podman container ps | grep keepingitrunning
7713969081f0  localhost/itt-ubu:24.04           /bin/bash             31 seconds ago  Up 32 seconds      keepingitrunning

This allows us to keep the container up and running!

--interactive, -i
       When set to true, keep stdin open even if not attached. The default is false.

--detach, -d
    Detached mode: run the container in the background and print the new container ID.  The  de‐
    fault is false.

Alternatives

There are some more hacky solutions, that work in edge cases.

--tty, -t

It allocates a pseudo-TTY to keep the container up and running. It is commonly used to run container interactivly with -ti.

Here is a remark from the manual:

NOTE: The --tty flag prevents redirection  of  standard  output.   It  combines  STDOUT  and
       STDERR,  it  can  insert control characters, and it can hang pipes. This option is only used
       when run interactively in a terminal.

Keeping the container busy

Simply said, container stop when there is nothing more to do. To prevent this, simply read /dev/null, or some of the following examples:

Read from /dev/null:
podman run -d localhost/itt-ubu:24.04 tail -f /dev/null
sleep for ever:
podman run -d localhost/itt-ubu:24.04 sleep infinity
Open an empty shell:
podman run -d localhost/itt-ubu:24.04 /bin/sh

There are more options, like infinite while-loop, an open network socket, cat command, etc - but you get the gist.

That said, some of those never really worked for me.



Most recent Articles:
  • Notice Board 003: Progress is Progress
  • How to: Cisco ISE backup to SFTP repository with public key authentication
  • Dummy IP & MAC Addresses for Documentation & Sanitization
  • Deploying ISSO Commenting System for Static Content using Docker
  • Generate a Vanity v3 Hidden Service Onion Address with mkp224o