BLOG

Podman: Run container in pods

July 7, 2020

/

Brandon Tsai

Pods

The Pod concept was introduced by Kubernetes. A pod is a group of containers that operate together. Podman uses a similar concept to manage a group of containers on a local server. All containers inside the pod share the same network namespace, so they can easily talk to each other over the localhost without the need to export any extra ports. You can refer to this link Podman: Managing pods and containers in a local container runtime for more technical details about Podman. In this article, we will focus on how to run and manage pods on the local server.

Create pod manually

The first thing to do is the creation of a new pod.

# sudo podman pod create -n my-app -p 8081:80

And then add a container to a pod

sudo podman run -dt --pod my-app -v /opt/http:/usr/share/nginx/html:ro --security-opt="seccomp=unconfined" --name hello-nginx nginx

Notice that you cannot run a container that binds a port to a container that runs in a pod. You have to bind the port to the pod instead, and there is an issue when you try to export multiple ports in a pod.

You can list all pods by running podman pod ps

# sudo podman pod ps
POD ID         NAME     STATUS    CREATED          # OF CONTAINERS   INFRA ID
75d943416fc8   my-app   Created   26 minutes ago   1                 30138c8d0d1c

If you stop a pod, all containers in the pod will stop as well.

$ sudo podman pod stop my-app
a2edfd1095760b1e2946271184743cce6f621665878b618ddc83d73b295070ba
$ sudo podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS                    PORTS                 NAMES
cacdc75990b0  docker.io/library/nginx:latest  nginx -g daemon o...  2 minutes ago  Exited (0) 7 seconds ago  0.0.0.0:8082->80/tcp  hello-nginx
4dce350e01cf  k8s.gcr.io/pause:3.1                                  3 minutes ago  Exited (0) 7 seconds ago  0.0.0.0:8082->80/tcp  a2edfd109576-infra

Similarly, starting a pod will start all containers in the pod.

$ sudo podman pod start my-app
a2edfd1095760b1e2946271184743cce6f621665878b618ddc83d73b295070ba
$ sudo podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS                 NAMES
cacdc75990b0  docker.io/library/nginx:latest  nginx -g daemon o...  4 minutes ago  Up 5 seconds ago  0.0.0.0:8082->80/tcp  hello-nginx
4dce350e01cf  k8s.gcr.io/pause:3.1

Create Pod by Kubernetes style YAML file.

https://mkdev.me/en/posts/dockerless-part-3-moving-development-environment-to-containers-with-podman

Podman supports setting a pod via Kubernetes-compatible pod definition YAML file. You can mount a volume by using hostPath

# my-app.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: ng1
    image: nginx
    ports:
      - containerPort: 8001
        hostPort: 8001
        protocol: TCP
    volumeMounts:
      - name: html1-volume
        mountPath: /opt/html
      - name: config1-volume
        mountPath: /etc/nginx/conf.d
  - name: ng2
    image: nginx
    ports:
      - containerPort: 8002
        hostPort: 8002
        protocol: TCP
    volumeMounts:
      - name: html2-volume
        mountPath: /opt/html
      - name: config2-volume
        mountPath: /etc/nginx/conf.d
  volumes:
    - name: html1-volume
      hostPath:
        path: /opt/myapp/html1
        type: Directory
    - name: config1-volume
      hostPath:
        path: /opt/myapp/config1
        type: Directory
    - name: html2-volume
      hostPath:
        path: /opt/myapp/html2
        type: Directory
    - name: config2-volume
      hostPath:
        path: /opt/myapp/config2
        type: Directory

To create a new pod with YAML file

$ sudo podman play kube ./my-app.yaml

Check all containers are running

$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS                        PORTS                             NAMES
2268e5ab9b61  nginx                           nginx -g daemon o...  2 minutes ago  Up 2 minutes ago              0.0.0.0:8001-8002->8001-8002/tcp  ng2
19dba831eeae  nginx                           nginx -g daemon o...  2 minutes ago  Up 2 minutes ago              0.0.0.0:8001-8002->8001-8002/tcp  ng1
42c150972ddb  k8s.gcr.io/pause:3.1                                  2 minutes ago  Up 2 minutes ago              0.0.0.0:8001-8002->8001-8002/tcp  4ae6b24effb5-infra

Notice that all the containers in a pod will share the same local IP 127.0.0.1. They must be running on different ports, otherwise, some container will fail to start due to the port conflict.

Conclusion

The ability for Podman to handle Kubernetes-compatible pod deployment is a clear differentiator to other container runtimes. For the Kubernetes users, they should be comfortable implementing the YAML file to manage a group of containers locally.

However, compared to docker-compose, Podman Pod can not be used to build multiple images at the same time. There is a third-party tool podman-compose that might offer this functionality, but I would suggest implementing a script for building images and use Podman Pod for containers management.


Share

Contact Us

Icon

Address
Level 8
11-17 York Street
Sydney NSW 2000

Icon

Phone Number
+61 2 8294 8067

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

© 2017-2024 Darumatic Pty Ltd. All Rights Reserved.