Running Containers
Overview
Teaching: 15 min
Exercises: 5 minQuestions
How are containers run?
How do you monitor containers?
How are containers exited?
How are containers restarted?
Objectives
Run containers
Understand container state
Stop and restart containers
To use an image as a particular instance on a host machine, you run it as a container. You can run in either a detached or foreground (interactive) mode.
Run the image we pulled as a container with an interactive bash terminal:
podman run -it almalinux:9 /bin/bash
The -i option here enables the interactive session, the -t option gives access to a terminal and the /bin/bash command makes the container start up in a bash session.
You are now inside the container in an interactive bash session. Try listing the files
ls
afs bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
and check the host to see that you are not in your local host system
hostname
<generated hostname>
Further, check the os-release to see that you are actually inside a release of AlmaLinux
cat /etc/os-release
NAME="AlmaLinux"
VERSION="9.7 (Moss Jungle Cat)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.7"
PLATFORM_ID="platform:el9"
PRETTY_NAME="AlmaLinux 9.7 (Moss Jungle Cat)"
ANSI_COLOR="0;34"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:almalinux:almalinux:9::baseos"
HOME_URL="https://almalinux.org/"
DOCUMENTATION_URL="https://wiki.almalinux.org/"
BUG_REPORT_URL="https://bugs.almalinux.org/"
ALMALINUX_MANTISBT_PROJECT="AlmaLinux-9"
ALMALINUX_MANTISBT_PROJECT_VERSION="9.7"
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.7"
SUPPORT_END=2032-06-01
Monitoring Containers
Open up a new terminal tab on the host machine and list the containers that are currently running:
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<generated id> <image:tag> "/bin/bash" n minutes ago Up n minutes <generated name>
Notice that the name of your container is some randomly generated name. To make the name more helpful, rename the running container
podman rename <CONTAINER ID> my-example
and then verify it has been renamed
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<generated id> <image:tag> "/bin/bash" n minutes ago Up n minutes my-example
Renaming by name
You can also identify containers to rename by their current name
podman rename <NAME> my-example
Alternatively, you can also give the container a name at creation, using the --name option:
podman run -it --name my-fancy-name almalinux:9 /bin/bash
This way, it has a custom chosen name to start with, which you can use later on to interact with it.
Exiting and restarting containers
As a test, go back into the terminal used for your container, and create a file in the container
touch test.txt
In the container exit at the command line
exit
You are returned to your shell. If you list the containers you will notice that none are running
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
but you can see all containers that have been run and not removed with
podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
<generated id> <image:tag> "/bin/bash" n minutes ago Exited (0) t seconds ago my-example
To restart your exited container start it again and then attach it interactively to your shell
podman start <CONTAINER ID>
podman attach <CONTAINER ID>
execcommandThe attach command used here is a handy shortcut to interactively access a running container with the same start command (in this case
/bin/bash) that it was originally run with.In case you’d like some more flexibility, the exec command lets you run any command in the container, with options similar to the run command to enable an interactive (
-i) session, etc.For example, the
execequivalent toattaching in our case would look like:podman start <CONTAINER ID> podman exec -it <CONTAINER ID> /bin/bash
Starting and attaching by name
You can also start and attach containers by their name
podman start <NAME> podman attach <NAME>
Check that test.txt still exists
ls
afs bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys test.txt tmp usr var
So this shows us that we can exit containers for arbitrary lengths of time and then return to our working environment inside of them as desired.
Clean up a container
If you want a container to be cleaned up — that is, deleted — after you exit it then run with the
--rmoption flagpodman run --rm -it <IMAGE> /bin/bash
Key Points
Run containers with
podman run <image-id>Monitor containers with
podman psExit interactive sessions using the
exitcommandRestart stopped containers with
podman start