'Is it possible to install curl and composer with a command in docker-compose.yml file

Is it possible to install Curl and Composer in a command that executes in the command section of a docker-compose.yml ? I don't want to build a new Image for that.

...
image: "customimage"
command: bash -c "apt update && apt -y upgrade
  && apt install curl
  && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer"
network_mode: bridge
ports:
  - "80:80"
  - "443:443"
links:
...

I tried this approach but it didn't work.



Solution 1:[1]

In general, you should always try to do installation steps in a Dockerfile if you can. Putting complex setup in a docker-compose.yml file is, well, complex, and anything in a Compose command: override will get repeated every time the container starts up.

The flip side of this is that you can start your Dockerfile FROM any image you want, even if it's hosted on some other registry; you're not limited to the core Docker Hub images.

So for what you show, you could write a basic Dockerfile like

FROM customimage # could be registry.example.com/remote-image:tag
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y \
      curl
RUN curl -sS https://getcomposer.org/installer \
  | php -- --install-dir=/usr/local/bin --filename=composer
# ... more COPY and RUN commands ...

Then your docker-compose.yml file would just need to reference this image build

version: '3.8'
services:
  myapp:
    # building the local image downloads the base image,
    # does the OS-level package upgrades, and installs Composer
    build: .
    # use the default command: from the image CMD
    ports:
      - "80:80"
      - "443:443"

There's not any particular downside to building a new image here. You might consider building the image in your CI system as you would any other runnable artifact, but it's definitely possible to distribute this Dockerfile and Compose file together as instructions to build and run the image.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze