laraveldockerdocker-composevirtual-machinelaradock

How do I install composer in container of docker?


I am new at docker and docker-compose and I am developing a Laravel-project on docker and docker-compose with Laradock as following a tutorial(not sure whether It is a correct way or not to refer this situation though).

I want to install the composer in this environment to be able to use the composer command.

As a matter of fact, I wanted to do seeding to put data into DB that I made by php artisan make:migrate but this error appeared.

include(/var/www/laravel_practice/vendor/composer/../../database/seeds/AdminsTableSeeder.php): failed to open stream: No such file or directory

So I googled this script to find a solution that will solve the error then I found it. It says, "Do composer dump-autoload and try seeding again", so I followed it then this error appeared.

bash: composer: command not found

Because I have not installed composer into docker-container. My docker's condition is like this now. ・workspace
・mysql
・apache
・php-fpm
Since I have not installed the composer, I have to install it into docker-container to solve the problem, BUT I have no idea how to install it into docker-container.

So could anyone tell me how to install composer into docker-container? Thank you.

here is the laradock/mysql/Dockerfile and laravelProject/docker-compose.yml.

ARG MYSQL_VERSION=5.7
FROM mysql:${MYSQL_VERSION}

LABEL maintainer="Mahmoud Zalt <mahmoud@zalt.me>"

#####################################
# Set Timezone
#####################################

ARG TZ=UTC
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && chown -R mysql:root /var/lib/mysql/

COPY my.cnf /etc/mysql/conf.d/my.cnf

CMD ["mysqld"]

EXPOSE 3306
version: '2'
services:
  db:
    image: mysql:5.7
    ports:
      - "6603:3306"
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=laravelProject
      - LANG=C.UTF-8
    volumes:
      - db:/var/lib/mysql
    command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION --character-set-server=utf8 --collation-server=utf8_unicode_ci

  web:
    image: arbiedev/php-nginx:7.1.8
    ports:
      - "8080:80"
    volumes:
      - ./www:/var/www
      - ./nginx.conf:/etc/nginx/sites-enabled/default

volumes:
  db:


Solution

  • You can build your own image and use it in your Docker compose file.

    FROM php:7.2-alpine3.8
    
    RUN apk update
    RUN apk add bash
    RUN apk add curl
    
    # INSTALL COMPOSER
    RUN curl -s https://getcomposer.org/installer | php
    RUN alias composer='php composer.phar'
    
    # INSTALL NGINX
    RUN apk add nginx
    

    I used the PHP alpine image as my base image because it's lightweight, so you might have to install other dependencies yourself. In your docker-compose file

    web:
      build: path/to/your/Dockerfile/directory
      image: your-image-tag
      ports:
        - "8080:80"
      volumes:
        - ./www:/var/www
        - ./nginx.conf:/etc/nginx/sites-enabled/default