运行多个podman容器,例如docker-compose

时间:2019-07-05 06:53:15

标签: docker docker-compose containers podman

我在 podman 中找到了一些可以替换 docker-compose 的库,但是它仍在开发中,所以我的问题是如何一起运行多个容器,目前我正在使用我的bash脚本来运行所有脚本,但这只是第一次不更新容器而已。

首先,我希望使用 podman 中的任何方式,而不是使用其他工具。

库(正在开发中)-> https://github.com/muayyad-alsadi/podman-compose

1 个答案:

答案 0 :(得分:1)

我认为 Kubernetes Pod concept 正是您所需要的,或者至少它允许您按照既定的标准同时运行多个容器

我的第一种方法和你一样,把所有的事情都作为一个命令来执行,比如:

# Create a pod, publishing port 8080/TCP from internal 80/TCP
$ podman pod create \
  --name my-pod \
  --publish 8080:80/TCP \
  --publish 8113:113/TCP

# Create a first container inside the pod
$ podman run --detach \
  --pod my-pod \
  --name cont1-name \
  --env MY_VAR="my val" \
  nginxdemos/hello

# Create a second container inside the pod
$ podman run --detach \
  --pod my-pod \
  --name cont2-name \
  --env MY_VAR="my val" \
  greboid/nullidentd

# Check by
$ podman container ls; podman pod ls

现在您有了一个 Pod,您可以使用 podman generate kube my-pod > my-pod.yaml 将其导出为 Pod 清单。

一旦您尝试自己的示例,您就会发现并非所有内容都按您的预期导出(例如网络或卷),但至少它可以作为您继续工作的基础。

假设相同的示例,在 YAML Pod 清单中,它看起来像这样 my-pod.yaml

# Created with podman-2.2.1
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: my-pod
  name: my-pod
spec:
  containers:
  # Create the first container: Dummy identd server on 113/TCP
  - name: cont2-name
    image: docker.io/greboid/nullidentd:latest
    command: [ "/usr/sbin/inetd", "-i" ]
    env:
    - name: MY_VAR
      value: my val
    # Ensure not to overlap other 'containerPort' values within this pod
    ports:
    - containerPort: 113
      hostPort: 8113
      protocol: TCP
    workingDir: /
  # Create a second container.
  - name: cont1-name
    image: docker.io/nginxdemos/hello:latest
    command: [ "nginx", "-g", "daemon off;" ]
    env:
    - name: MY_VAR
      value: my val
    # Ensure not to overlap other 'containerPort' values within this pod
    ports:
    - containerPort: 80
      hostPort: 8080
      protocol: TCP
    workingDir: /
  restartPolicy: Never
status: {}

当这个文件像这样使用时:

# Use a Kubernetes-compatible Pod manifest to create and run a pod
$ podman play kube my-pod.yaml

# Check
$ podman container ls; podman pod ls

# Output
CONTAINER ID  IMAGE                                COMMAND               CREATED         STATUS            PORTS                                        NAMES
1a53a5c0f076  docker.io/nginxdemos/hello:latest    nginx -g daemon o...  8 seconds ago   Up 6 seconds ago  0.0.0.0:8080->80/tcp, 0.0.0.0:8113->113/tcp  my-pod-cont1-name
351065b66b55  docker.io/greboid/nullidentd:latest  /usr/sbin/inetd -...  10 seconds ago  Up 6 seconds ago  0.0.0.0:8080->80/tcp, 0.0.0.0:8113->113/tcp  my-pod-cont2-name
e61c68752e35  k8s.gcr.io/pause:3.2                                       14 seconds ago  Up 7 seconds ago  0.0.0.0:8080->80/tcp, 0.0.0.0:8113->113/tcp  b586ca581129-infra
POD ID        NAME    STATUS   CREATED         INFRA ID      # OF CONTAINERS
b586ca581129  my-pod  Running  14 seconds ago  e61c68752e35  3

您将能够在 8080 处访问 nginx 服务的“Hello World”,在 8113 处访问虚拟 identd 服务器。

相关问题