aws cli Image 从 docker compose 运行并在一秒后关闭

aws cli Image rrun from docker compose and it's shutdon after one second

我尝试从 docker-compose 运行 来自亚马逊的 aws cli 图像。

version: '3.1'
services:
    web:
        image: amazon/aws-cli:latest
        stdin_open: true # equivalent of -i
        tty: true        # equivalent of -t
        deploy:
          mode: replicated
          replicas: 1
          resources:
            limits:
              cpus: "2"
              memory: 2048M
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
            window: 120s
        environment:
          - HTTP_PROXY=http://ipadresses:port
          - HTTPS_PROXY=https://ipadresses:port
        ports:
            - "8080:8080"

一秒钟后,图像立即停止 运行ning。我从亚马逊查找了官方文档,但找不到我的问题的答案:为什么它停止得这么快有人能帮我理解这种行为吗?

amazon/aws-cli:latest 是一个 docker 镜像,只提供 aws 命令作为起始命令,这意味着,如果你不覆盖 docker 执行的命令,容器将执行aws 命令并停止执行。

要使用 docker 图像针对 aws 执行命令,您需要提供命令,例如在此撰写文件中,我通过向文件中添加 command: help 来执行 aws help

    version: '3.1'
services:
    web:
        image: amazon/aws-cli:latest
        stdin_open: true # equivalent of -i
        tty: true        # equivalent of -t
        command: help
        deploy:
          mode: replicated
          replicas: 1
          resources:
            limits:
              cpus: "2"
              memory: 2048M
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
            window: 120s
        environment:
          - HTTP_PROXY=http://ipadresses:port
          - HTTPS_PROXY=https://ipadresses:port
        ports:
            - "8080:8080"

或者您可以覆盖 entrypoint 以强制容器保持活动状态,因此之后您可以对您的容器执行命令,例如

version: '3.1'
services:
    web:
        image: amazon/aws-cli:latest
        stdin_open: true # equivalent of -i
        tty: true        # equivalent of -t
        entrypoint: tail -f /dev/null
        deploy:
          mode: replicated
          replicas: 1
          resources:
            limits:
              cpus: "2"
              memory: 2048M
          restart_policy:
            condition: on-failure
            delay: 5s
            max_attempts: 3
            window: 120s
        environment:
          - HTTP_PROXY=http://ipadresses:port
          - HTTPS_PROXY=https://ipadresses:port
        ports:
            - "8080:8080"

在你 运行 这个组合文件之后(在另一个终端),你可以使用 docker ps 检查创建的容器的名称,然后使用 docker exec -it <your_container_name> aws help 对它执行命令(注意在这种情况下,我将命令 aws help 发送到 运行ning 容器)