kunal kashyap
kunal kashyap

Reputation: 265

docker-compose up is starting the container and immediately stopping them

I am trying to build services using docker-compose but containers started using docker-compose is stopping immediately as compose script is over. But when i am creating container using docker run for the same service image it is working.

Here is my compose script:

version: '2.1'

networks:
 composetestnetwork:
  driver: bridge
   ipam:
    driver: default
    config:
     - subnet: 172.19.0.0/16
       gateway: 172.19.0.1

services:
 composetestdb:
  build:
   context: .
   dockerfile: Dockerfile-testdb
  cap_add:
   - ALL
  container_name: my-db-compose-container
  labels:
   db.description: "This is a test db"
  volumes:
   - c:/Users:/data
  networks:
   composetestnetwork:
    ipv4_address: 172.19.0.5
  ports:
   - "3306:3306"
  command: 'bash'

What can be done to fix this.

Upvotes: 24

Views: 26568

Answers (4)

PJ127
PJ127

Reputation: 1258

In Windows Containers, it seems the script stops even with

stdin_open: true
tty: true
command: powershell -ExecutionPolicy Bypass -File C:\\cicd\\install\\samba.ps1

or

command: powershell -ExecutionPolicy Bypass -File C:\\cicd\\install\\samba.ps1; powershell

Note that:

command: powershell

works, but the script is not run.

So I appended:

powershell.exe

to the end of my ps1 script file, and the script is run and the container does not stop after docker compose up.

Upvotes: 0

max
max

Reputation: 55

I think if no process keeps the container alive then it stops.

You could add an infinite loop (as a workaround) to your service in your docker file so it stays up:

command: sh -c "while true; do sleep 3600; done"

Upvotes: -4

Rifton007
Rifton007

Reputation: 371

I have the same problem, but tty : true doesn't resolve the problem for me.

This is my docker-compose file :

---
version: "3"
services :
  data:
    container_name: data
    environment:
      - MYSQL_ROOT_PASSWORD=0283
    image: mysql:5.7
    restart: always
    volumes:
      - "./database:/var/lib/mysql"
    ports:
      - "3306:3306"
  mail:
    container_name: mail
    image: djfarrelly/maildev
    ports:
      - "1080:80"
      - "1025:25"
  web:
    container_name: web
    image: "web:php7.1a"
    links:
      - mail
      - data
    ports:
      - "80:80"
    volumes:
      - "./www:/var/www/html"
    working_dir : /var/www/html
    tty: true
    command:
      echo "test"
      #bash -c "php composer.phar self-update"

After command, Docker return me : "the container is exited (0)" If I comment these lines, the container starts fine.

Windows 10 Pro
Docker version 18.09.2, build 6247962
Docker-compose version 1.23.2, build 1110ad01

Thanks,

Upvotes: 0

Dan Lowe
Dan Lowe

Reputation: 56687

If you use bash as the command, but without a terminal, it will exit immediately. This is because when bash starts up, if there is no terminal attached, and it has no script or other command to execute, it has nothing to do, so it exits (by design).

You can either use a different command, or attach a terminal with tty: true as part of the definition of this service.

services:
  composetestdb:
    tty: true
    ...

Upvotes: 63

Related Questions