简体   繁体   English

使用 docker-compose 创建的容器已退出

[英]Containers created with docker-compose exited

Whenever I try docker-compose up -d , my container exits with code 0 .每当我尝试docker-compose up -d时,我的容器都会以代码0退出。

I have following docker-compose.yml :我有以下docker-compose.yml

    version: "3"
    services:
      ansible:
        container_name: controller
        image: centos_ansible
        build:
          context: centos_ansible
        networks:
          - net
    
    networks:
      net:

The Dockerfile inside centos_ansible : centos_ansible 中的centos_ansible

    FROM centos
    RUN yum install python3 -y && \
    yum install epel-release -y && \
    yum install ansible -y

The problem is that you do not specify nothing to do to your container and, as a consequence, it exits immediately.问题是您没有指定对容器不做任何事情,因此它会立即退出。

You have several ways to tell the container what to do.您有几种方法可以告诉容器该做什么。 Please, see this great SO question for further information.请参阅这个很棒的 SO 问题以获取更多信息。

As you are using docker compose, you can indicate the command the container must execute:当您使用 docker 组合时,您可以指示容器必须执行的command

services:
  ansible:
    container_name: controller
    image: centos_ansible
    command: ansible-playbook playground.yml
    build:
      context: centos_ansible
    networks:
      - net

networks:
  net:

It seems you are trying to run ansible from docker.您似乎正在尝试从 docker 运行ansible Please, see this article, perhaps it will give you some additional ideas for your problem.请看这篇文章,也许它会给你一些额外的想法来解决你的问题。

To keep your container running, you need to tell it to do something.为了让你的容器保持运行,你需要告诉它做一些事情。

You can do this, as another answer mentioned, by adding an ENTRYPOINT to your Dockerfile.正如另一个答案所提到的,您可以通过在 Dockerfile 中添加一个ENTRYPOINT来做到这一点。 Alternatively, from this answer , you can add a command like this, or do something more useful:或者,从这个答案中,您可以添加这样的命令,或者做一些更有用的事情:

version: "3"
services:
  ansible:
    container_name: controller
    image: centos_ansible
    build:
      context: centos_ansible
    networks:
      - net
    command: tail -f /dev/null

networks:
  net:

Or if you'll need get a shell into the container, you can add the tty: true option to your service.或者,如果您需要将 shell 放入容器中,您可以将tty: true选项添加到您的服务中。

Your docker container only keeps running if there is something to run inside your container.您的 docker 容器只有在容器内有东西要运行时才会继续运行。 Try adding an ENTRYPOINT to your Dockerfile!尝试将ENTRYPOINT添加到您的 Dockerfile 中! ( https://docs.docker.com/engine/reference/builder/#entrypoint ) https://docs.docker.com/engine/reference/builder/#entrypoint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM