Marius M
Marius M

Reputation: 21

container for PHP's composer package-manager shows message / error: "docker_composer_1 exited with code 0" with docker-compose

I'm trying to create a composer container (Dockerfile) and use this container in my docker-compose.yml but it looks like the container doesn't start.

I'm facing the following message every time I run docker-compose up

docker_composer_1 exited with code 0

These are my Dockerfile and docker-compose.yml files:

Dockerfile

#composer
FROM composer
EXPOSE 9090

docker-compose.yml

version: '3'

services:
  php-fpm:
    build:
      context: ./php-fpm
    volumes:
      - ../src:/var/www

  nginx:
    build:
      context: ./nginx
    ports:
      - "80:80"
    volumes:
      - ../src/html:/var/www/html
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php-fpm

  database:
    build:
      context: ./database
    ports:
      - "3306:3306"
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=docker
    volumes:
      - ./database/data.sql:/docker-entrypoint-initdb.d/data.sql

  phpmyadmin:
    build:
      context: ./phpmyadmin
    environment:
      - PMA_HOST=database
      - PMA_PORT=3306
      - MYSQL_ROOT_PASSWORD=docker
    ports:
      - "8080:80"
    depends_on:
      - database

  composer:
    build:
      context: ./composer
    volumes:
      - ../src/html:/var/www/html
    ports:
      - "9090:80"

I don't want to install Composer into php container. I don't see this to be an elegant solution.

How can I solve this issue?

Upvotes: 1

Views: 508

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

composer is a package-manager and - as opposed to a "service" - does not run as a never-ending process.

composer commands are meant to be run once, perform a task and end with a status-code after they have finished their task (i.e. install or update packages, show package information, ...). This status code is 0 if the command ran successfully and non-zero if it failed.

Conclusion:

The message that the container excited with status code 0 is not an indication of failure but the expected behavior of every composer [..] command.

When your composer container is started, it executes the default command for the container which is configured by CMD in a Dockerfile and falls back to the ENTRYPOINT which is the composer executable without any arguments.

You can verify this behavior by running:

docker-compose logs composer 

composer is just a utility and as such should not be defined as a "service" in your docker-compose.yml. You can leave it in there for the convenience of being able to run quick one-off commands like:

docker-compose run composer install 

... but you'll have to live with the notice that the command has exited.

General Advice:

docker-compose is not a task-runner and it's not designed for one-off commands. Several proposals to implement features to support such functionality have been rejected by the maintainers.

Example(s):

  • implement an option to skip auto-starting of certain containers with docker-compose up -> See this issue

Upvotes: 1

Related Questions