composer

Estimated reading time: 5 minutes

Composer is a dependency manager written in and for PHP.

GitHub repo: https://github.com/composer/docker

Library reference

This content is imported from the official Docker Library docs, and is provided by the original uploader. You can view the Docker Store page for this image at https://store.docker.com/images/composer

Supported tags and respective Dockerfile links

Quick reference

What is Composer?

Composer is a tool for dependency management in PHP, written in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

You can read more about Composer in our official documentation.

logo

Using

Run the composer image:

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    composer install

You can mount the Composer home directory from your host inside the Container to share caching and configuration files:

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --volume $COMPOSER_HOME:/tmp \
    composer install

By default, Composer runs as root inside the container. This can lead to permission issues on your host filesystem. You can run Composer as your local user:

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --user $(id -u):$(id -g) \
    composer install

When you need to access private repositories, you will either need to share your configured credentials, or mount your ssh-agent socket inside the running container:

Note: This currently does not work on OSX, see docker/for-mac#410.

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --volume $SSH_AUTH_SOCK:/ssh-auth.sock \
    --env SSH_AUTH_SOCK=/ssh-auth.sock \
    composer install

When combining the use of private repositories with running Composer as another (local) user, you might run into non-existant user errors (thrown by ssh). To work around this, simply mount the host passwd and group files (read-only) into the container:

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    --volume $SSH_AUTH_SOCK:/ssh-auth.sock \
    --volume /etc/passwd:/etc/passwd:ro \
    --volume /etc/group:/etc/group:ro \
    --user $(id -u):$(id -g) \
    --env SSH_AUTH_SOCK=/ssh-auth.sock \
    composer install

Suggestions

PHP Extensions

We aim to deliver an image that is as lean as possible, built for running Composer only.

Sometimes dependencies or Composer scripts require the availability of certain PHP extensions. You can work around this as follows:

  • Pass the --ignore-platform-reqs and --no-scripts flags to install or update:

    docker run --rm --interactive --tty \
        --volume $PWD:/app \
        composer install --ignore-platform-reqs --no-scripts
    
  • Create your own image (possibly by extending FROM composer).

Note: Docker introduced multi-stage builds in 17.05:

  • Create your own image, and copy Composer from the official image into it:

    COPY --from=composer:1.5 /usr/bin/composer /usr/bin/composer
    

It is highly recommended that you create a “build” image that extends from your baseline production image. Binaries such as Composer should not end up in your production environment.

Local runtime/binary

If you want to be able to run composer as if it was installed on your host locally, you can define the following function in your ~/.bashrc, ~/.zshrc or similar:

composer () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --user $(id -u):$(id -g) \
        --volume /etc/passwd:/etc/passwd:ro \
        --volume /etc/group:/etc/group:ro \
        --volume $(pwd):/app \
        composer "$@"
}

License

View license information for the software contained in this image.

As with all Docker images, these likely also contain other software which may be under other licenses (such as Bash, etc from the base distribution, along with any direct or indirect dependencies of the primary software being contained).

Some additional license information which was able to be auto-detected might be found in the repo-info repository’s composer/ directory.

As for any pre-built image usage, it is the image user’s responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within.

library, sample, composer