CentOS 8.x create and use containers with podman
Home > CentOS > CentOS 8.x > Virtualization > podman > Create and use containers with podman
Check running containers
- Get information on running containers using:
podman ps
- To get information on all containers including stopped containers use:
podman ps --all
Start new containers
- Start container with interactive shell:
podman run -it centos bash #Then exit from container shell
where:
- -i (--interactive)
- is for keeping STDIN open even if not attached
- -t (--tty)
- is for allocating a pseudo-TTY for container
- To give name to containers use:
podman run -it --name web1 centos bash
Then name can be used in future podman commands such as rm or attach
- Give hostname to OS inside the container use:
podman run -itd -h <hostname> <image>
For example
podman run -itd -h admin1 docker.io/library/rockylinux:9.0
Delete containers
- To delete container using container ID use:
podman ps --all #List containers podman rm <container-id>
Note that only stopped containers can be deleted. To stop running container before deleting use:
podman stop <container-id>
Tag images
We can tag images to identify them easily using:
podman tag <image-id> <tag>
Search for images
Searching images in podman
podman search httpd
Get more image details
To see more details of image use:
podman inspect <image-id or image-tag>
This lists information such as:
- Various tags of the image
- Image sha256 checksum
- WorkDir where we can find all files related to the image
See container logs
- To see logs of container use:
podman logs <container-id>
Note that this works even if there is no /var/log/messages file inside the container
- To follow logs in realtime use:
podman logs -f <container-id>
- To see last few lines of container logs use:
podman logs --tail 10 <container-id>
See list of process of running containers
To see running process inside container use:
podman top <container-id>
Get shell (attach) for running containers
To get shell (attach) for running containers use:
podman attach <container-id>
To come out of shell (detach) without existing use 'Ctrl+p+q'
Refer:
- https://computingforgeeks.com/using-podman-and-libpod-to-run-docker-containers/
- https://www.linuxtechi.com/run-containers-podman-centos-8-rhel-8/
Accessing container files in base host
In base host do
df -h
and you can see container folder mounted as part of output such as:
overlay 222G 99G 113G 47% /mnt/data1/container-files/var-lib-containers/storage/overlay/405546df7603ca9e16d8a69a26cd7e44129b4f24312a8b1bc25841620b089ac5/merged
Then you can create new files in corresponding ../diff folder such as:
touch /mnt/data1/container-files/var-lib-containers/storage/overlay/405546df7603ca9e16d8a69a26cd7e44129b4f24312a8b1bc25841620b089ac5/diff/a.txt
All the files of diff along with base files of image get merged and are shown in:
ls /mnt/data1/container-files/var-lib-containers/storage/overlay/405546df7603ca9e16d8a69a26cd7e44129b4f24312a8b1bc25841620b089ac5/merged/*.txt
You can do "ls /*.txt" in container to see a.txt file.
Install packages in podman containers from base host
To install packages in podman containers having same OS (eg centos8) use:
yum -y install --installroot=/mnt/data1/container-files/var-lib-containers/storage/overlay/8e81551668e86c7cac3e425f0f9d6207736c9a43d53fe5f9abdc9d3b07fc8e56/merged bind-utils
where the installroot path can be found using:
podman inspect <container-id-or-name> | grep -i merged
Home > CentOS > CentOS 8.x > Virtualization > podman > Create and use containers with podman