How to Install Composer with Docker Exec

How to Install Composer with Docker Exec

I'm trying to install composer on docker container. I have a container laravel55 and I'm gonna to install composer insite it.

docker exec laravel55 curl --silent --show-error 
 | php

#result
Composer (version 1.6.5) successfully installed to: /root/docker- 
images/docker-php7-apache2/composer.phar
Use it: php composer.phar

Aftar installation, I'm trying to using composer but it doesn't work:

 docker exec -w /var/www/html laravel55 php composer.phar install

#result
Could not open input file: composer.phar

It seems that Composer had not installed! How can I install composer on a docker container?

1

2 Answers

Well with your command you're actually installing composer.phar locally on your host, you just execute the curl command inside the container. The part behind the pipe symbol | is not executed in your docker container but on your host. In your second command you switch your working directory to /var/www/html where you apparently expect the composer.phar but not in the first command.

So to make the whole command run in the container, you can try the following:

docker-compose exec -w /var/www/html laravel55 \
    sh -c "curl --silent --show-error  | php"
1

You could use official composer image from dockerhub and mount on it a volume from your app container

i.e

docker run -td --name my_app --volume /var/www myprivateregistry/myapp

docker run --rm --interactive --volumes-from my_app --volume /tmp:/tmp --workdir /var/www  composer install
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.