-2

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
bguiz
  • 27,371
  • 47
  • 154
  • 243
caggri
  • 5
  • 5

3 Answers3

1

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.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
0

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.

cam
  • 4,409
  • 2
  • 24
  • 34
0

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)

Garuno
  • 1,935
  • 1
  • 9
  • 20