ltdev
ltdev

Reputation: 4457

How can I use the official composer image within docker-compose?

This is my docker-compose file

version: '3'

services:
    app:
        image: php7.1-apache-stretch
        container_name: php-app
        volumes:
            - .:/var/www/html
        ports:
            - 8080:80
    composer:
        image: composer:1.8
        container_name: composer-app
        volumes:
            - .:/var/www/html

However when I run docker-compose up the only running container is the php-app. How can I run the composer within the docker-compose file and communicate with my app container?

Upvotes: 6

Views: 4158

Answers (1)

Mihai
Mihai

Reputation: 10727

Ideally you use the Composer image in a multistage build to avoid having to install it and all its dependencies on the host.

To do that create a Dockerfile like this:

FROM composer:1.8 AS composer

FROM php:7.1-apache-stretch

COPY --from=composer /usr/bin/composer /usr/bin/composer

WORKDIR /app

COPY . .

# Place here your command that uses Composer
RUN composer install

Note: You need to review the RUN command since I am not a PHP developer .... ;)

Now the docker-compose.yml looks something like this:

version: "3"

services:
  app:
    build: .
    image: php-app:1.0
    container_name: "php-app"
    ports: 
      - "8080:80"

You don't need the volume anymore since the files are copied inside the image at build time.

Build the image and run the service:

docker-compose up --build -d

Upvotes: 3

Related Questions