pop
pop

Reputation: 139

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 <[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:

Upvotes: 2

Views: 25815

Answers (4)

Momen
Momen

Reputation: 45

There are many ways out there. I think, most easy way is to run:

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Now, You can copy your project to the desired directory and run composer install.

Example Dockerfile:

# Copy the application code from the current directory to nginx root directory
COPY --chown=www-data:www-data . /var/www/html

# Docker see's only to the changes at build time
COPY --chown=www-data:www-data composer.json /var/www/html/

# Set project owner && permissions
RUN chown -R 1000:1000 /var/www/html/ && chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

# Set the working directory to run next commands(RUN/CMD/COPY/ADD)
WORKDIR /var/www/html/

# Install project dependencies
RUN composer install --no-interaction --prefer-dist

Upvotes: 0

Leonel Kahameni
Leonel Kahameni

Reputation: 933

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'

Upvotes: 1

Luiz Felipe Lima
Luiz Felipe Lima

Reputation: 71

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.

Upvotes: 3

prithajnath
prithajnath

Reputation: 2115

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

Upvotes: 9

Related Questions