riju.srk
riju.srk

Reputation: 219

Docker container exited after running command

I have a Docker Compose file which creates 3 containers and I need to add some setup tasks after these containers are up. My thought is I am going to use Dockers command statement to complete my setup task.

version: "3"

services:
  mariadb:
    image: wodby/mariadb:$MARIADB_TAG
    container_name: "${PROJECT_NAME}_mariadb"
    stop_grace_period: 30s
    environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD
    volumes:
      - ./mariadb-init:/docker-entrypoint-initdb.d 

  php:
    image: wodby/drupal-php:$PHP_TAG
    container_name: "${PROJECT_NAME}_php"
    environment:    
      DB_HOST: $DB_HOST
      DB_PORT: $DB_PORT
      DB_USER: $DB_USER
      DB_PASSWORD: $DB_PASSWORD
      DB_NAME: $DB_NAME
      DB_DRIVER: $DB_DRIVER
      PHP_FPM_USER: wodby
      PHP_FPM_GROUP: wodby
      COLUMNS: 80 # Set 80 columns for docker exec -it.
    volumes:
      - ./Insider:/var/www/html
    command: ls

  nginx:
    image: wodby/nginx:$NGINX_TAG
    container_name: "${PROJECT_NAME}_nginx"
    depends_on:
      - php
    environment:
      NGINX_STATIC_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/web
      NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
    ports:
      - '8000:80'
    volumes:
      - ./Insider:/var/www/html
    labels:
      - "traefik.http.routers.${PROJECT_NAME}_nginx.rule=Host(`${PROJECT_BASE_URL}`)" 

Now the problem is when I remove this command: ls everything works perfectly, but with this, in place, both PHP and NGINX container get exited after I run docker-compose up -d when I check log of stopped PHP container I can get output from ls, but not sure why my container get exited.

I am new to Docker so please do not mind if it is a silly question.

Upvotes: 0

Views: 366

Answers (1)

Adiii
Adiii

Reputation: 59946

The life of container is the life of command, so your container will exit when it run command: ls, where the Nginx container is depended on php, and PHP will exit after executing ls.

    depends_on:
      - php

do not override the command for PHP. remove the command and it should work.

Upvotes: 2

Related Questions