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
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
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
Watch
Ta-da*2!
I hope this helps someone.
Top comments (10)
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:
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, thecomposer
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...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.
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
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?
php --
-- For what ?
-- Для чего ?
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.
Helped me debug a dockerfile, so thank you.
Easy to understand, thank you :)
Thanks, It was really useful
What kind of sorcery ist thist? Me gusta!