How do I install composer in container of docker?

11,145

Solution 1

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

Solution 2

You could do something like this:

FROM php:8.0.2-apache
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y mariadb-client libxml2-dev
RUN apt-get autoremove -y && apt-get autoclean
RUN docker-php-ext-install mysqli pdo pdo_mysql xml
COPY --from=composer /usr/bin/composer /usr/bin/composer

the argument COPY --from= should solve your problem.

Solution 3

FROM php:7.3-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN alias composer='php /usr/bin/composer'
Share:
11,145
Author by

pop

Updated on June 22, 2022

Comments

  • pop 8 months

    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 <[email protected]>"
    #####################################
    # 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:
    
    • prithajnath
      prithajnath almost 4 years
    • pop almost 4 years
      @prithajnath Thank you for your advice. I modified it and how is it now?
    • prithajnath
      prithajnath almost 4 years
      Much better. Do you happen to have the relevant Dockerfile that you used to create your image/container?
    • Efrat Levitan
      Efrat Levitan almost 4 years
      what is your base image- can you share your Dockerfile?
    • pop almost 4 years
      @prithajnath I added information about it.
  • pop almost 4 years
    Thank you so much!!!! I have a question. You said like, "write these code in docker-compose file". Does it mean "docker-compose.yml"?
  • prithajnath
    prithajnath almost 4 years
    @pop Yep. I meant the docker-compse.yml file