You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行podman-compose run时,单服务为何生成两个容器?

为什么podman-compose run -d fooapp-db会生成两个相同配置的容器?

环境与配置

对应的docker-compose.yml文件内容如下:

version: "3.9"

services:
  fooapp-db:
    image: "postgres:15-alpine"
    environment:
      POSTGRES_USER: <username>
      POSTGRES_PASSWORD: <password>
      POSTGRES_DB: <dbname>
    ports:
      - 5432:5432
    volumes:
      - <local mount point>:/var/lib/postgresql/data/

执行情况

执行podman-compose run -d因缺少服务名失败,执行podman-compose run -d fooapp-db后输出如下:

['podman', '--version', '']
using podman version: 4.3.1
** excluding:  set()
['podman', 'network', 'exists', 'postgres_default']
podman run --name=postgres_fooapp-db_1 -d --label io.podman.compose.config-hash=123 --label io.podman.compose.project=postgres --label io.podman.compose.version=0.0.1 --label com.docker.compose.project=postgres --label com.docker.compose.project.working_dir=<redacted path> --label com.docker.compose.project.config_files=docker-compose.yml --label com.docker.compose.container-number=1 --label com.docker.compose.service=fooapp-db -e POSTGRES_USER=<username> -e POSTGRES_PASSWORD=<password> -e POSTGRES_DB=<dbname> -v <local mount point>:/var/lib/postgresql/data/ --net postgres_default --network-alias fooapp-db -p 5432:5432 postgres:15-alpine
e91c5fb6d3a862e565e544925a86df81bc253e6746ed781e962705a0dd7c8ef7
exit code: 0
['podman', 'network', 'exists', 'postgres_default']
podman run --name=postgres_fooapp-db_tmp58199 -d --label io.podman.compose.config-hash=123 --label io.podman.compose.project=postgres --label io.podman.compose.version=0.0.1 --label com.docker.compose.project=postgres --label com.docker.compose.project.working_dir=<redacted path> --label com.docker.compose.project.config_files=docker-compose.yml --label com.docker.compose.container-number=1 --label com.docker.compose.service=fooapp-db -e POSTGRES_USER=<username> -e POSTGRES_PASSWORD=<password> -e POSTGRES_DB=<dbname> -v <local mount point>:/var/lib/postgresql/data/ --net postgres_default --network-alias fooapp-db --tty postgres:15-alpine
c3e1da76625557b5d7944459444086a84f5564374f730260e697931b628c37b5
exit code: 0

原因分析

这是podman-compose与docker-compose在run命令实现上的行为差异导致的:

  • podman-compose run的逻辑是先启动compose配置中定义的常规服务容器(即名称带_1后缀的容器),确保服务的基础环境(比如网络、存储卷等)就绪。
  • 之后,它会创建一个临时容器(名称带_tmpXXXX后缀的容器),用于执行用户指定的命令。但你没有额外指定要执行的命令,临时容器就使用了postgres镜像的默认启动命令,最终两个容器以完全相同的配置运行起来。

如果只想启动单个服务容器,应该使用podman-compose up -d fooapp-db命令,而非run命令。

内容的提问来源于stack exchange,提问作者Yanick Rochon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:27:28