DEV Community

Russell Jones
Russell Jones

Posted on

Install composer in custom Docker image

Before Docker 17.05 dropped mid-2017, installing software inside a custom Docker image followed the same process as installing it on the host.

For example, you can install composer to /usr/local/bin on a desktop by running the following curl command as root.

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Enter fullscreen mode Exit fullscreen mode

To install it in a custom Docker image just prepend "RUN" and stick it into a Dockerfile:

# Dockerfile
FROM php:7.4.1-fpm
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Enter fullscreen mode Exit fullscreen mode

Watch

Ta-da!

It works, but it relies on curl and an internet connection. Docker 17.05 brought a cleaner, less curl-y, internet-y way.

Multi-stage builds and COPY

Simply replace the RUN instruction with the COPY instruction seen below.

You can see that we are copying from the composer image from /usr/bin/composer to /usr/bin/composer in the new image.

Now let's see it in action, (as documented at https://hub.docker.com/_/composer):

# Dockerfile
FROM php:7.4.1-fpm
# Install Composer
COPY --from=composer /usr/bin/composer /usr/bin/composer
Enter fullscreen mode Exit fullscreen mode

Watch

Ta-da*2!

I hope this helps someone.

Top comments (10)

Collapse
 
danburt profile image
dan-burt

I have followed the above guides. It stops once composer is installed - and I can confirm it is running in my own container.

How do I now deploy packages within that container?

In my case, I require 2 packages:

  • mongodb/mongodb
  • vlucas/phpdotenv

My issue is that I would like to map an existing directory that contains my existing PHP pages & scripts in to /var/www/html. If I try this, the composer packages are not installed - I expect because they are overwritten with the mapping?

If I omit the directory mapping, I can see the relevant composer files in the root folder (/var/www/html). But now I have no PHP content...

Collapse
 
enigmaticnerd profile image
Santiago Medina • Edited

Hello and thanks for the article. I'm getting this error when I try to building with github actions. Anyone know what's happening?

Digest: sha256:ec4242a8c5bcc570948ef4558c3befa85c7550f14380910868f9282c19a33771
Status: Downloaded newer image for composer:2.2.7
install: unrecognized option '--ignore-platform-reqs'
Try 'install --help' for more information.
make: *** [Makefile:115: composer-install] Error 1
Error: Process completed with exit code 2.

Collapse
 
enigmaticnerd profile image
Santiago Medina

This is my command on Makefile

.PHONY: composer
composer composer-install composer-update composer-require composer-require-module composer-dump: composer-env-file
@docker run --rm $(INTERACTIVE) --volume $(current-dir):/app --user $(id -u):$(id -g) \
composer:2.2.7 $(CMD) \
--ignore-platform-reqs \
--no-ansi

Collapse
 
codinghubes profile image
M.-Hubertus. F.

Hi,
I don't understand why to use composer in a container..
So if I start a CaktPHP-Project, I need to have Composer in my local dev directory, right?

Collapse
 
genbodev profile image
Elovskiy Igor

php --
-- For what ?
-- Для чего ?

Collapse
 
lebinhan profile image
Lê Bình An

Love this article my friend. Docker's document was overwhelming for beginner like me and your world saved me a lot of time learning it.

Collapse
 
0ctavia profile image
Octa

Helped me debug a dockerfile, so thank you.

Collapse
 
tieutantan profile image
Tran Ngoc Tan

Easy to understand, thank you :)

Collapse
 
gumonet profile image
Angel Gutierrez

Thanks, It was really useful

Collapse
 
nguimjeu profile image
Nguimjeu

What kind of sorcery ist thist? Me gusta!