skip to Main Content

Whenever I try docker-compose up -d, my container exits with code 0.

I have following 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:

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

3

Answers


  1. 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. 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.

    Login or Signup to reply.
  2. Your docker container only keeps running if there is something to run inside your container. Try adding an ENTRYPOINT to your Dockerfile! (https://docs.docker.com/engine/reference/builder/#entrypoint)

    Login or Signup to reply.
  3. 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.

    As you are using docker compose, you can indicate the command the container must execute:

    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. Please, see this article, perhaps it will give you some additional ideas for your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search