Search code examples
dockerdocker-composepackage-managers

How to install packages from Docker compose?


Hi there I am new to Docker. I have an docker-compose.yml which looks like this:

version: "3"

services:
  lmm-website:
    image: lmm/lamp:php${PHP_VERSION:-71}
    container_name: ${CONTAINER_NAME:-lmm-website}
    environment:
      HOME: /home/user
    command: supervisord -n
    volumes:
      - ..:/builds/lmm/website
      - db_website:/var/lib/mysql
    ports:
      - 8765:80
      - 12121:443
      - 3309:3306
    networks:
      - ntw

volumes:
  db_website:

networks:
  ntw:

I want to install the Yarn package manager from within the docker-compose file:

sudo apt-get update && sudo apt-get install yarn

I could not figure out how to declare this, I have tried

command: supervisord -n && sudo apt-get update && sudo apt-get install yarn

which fails silently. How do I declare this correctly? Or is docker-compose.yml the wrong place for this?


Solution

  • Why not use Dockerfile which is specifically designed for this task?

    Change your "image" property to "build" property to link a Dockerfile.

    Your docker-compose.yml would look like this:

    services:
      lmm-website:
        build: 
          context: .
          dockerfile: Dockerfile
        container_name: ${CONTAINER_NAME:-lmm-website}
        environment:
          HOME: /home/user
        command: supervisord -n
          volumes:
            - ..:/builds/lmm/website
            - db_website:/var/lib/mysql
        ports:
          - 8765:80
          - 12121:443
          - 3309:3306
        networks:
          - ntw
    
    volumes:
      db_website:
    
    networks:
    

    Then create a text file named Dockerfile in the same path as docker-compose.yml with the following content:

    FROM lmm/lamp:php${PHP_VERSION:-71}
    
    RUN apt-get update && apt-get install -y bash
    

    You can add as much SO commands as you want using Dockerfile's RUN (cp, mv, ls, bash...), apart from other Dockerfile capabilities like ADD, COPY, etc.

    +info:

    https://docs.docker.com/engine/reference/builder/

    +live-example:

    I made a github project called hello-docker-react. As it's name says is a docker-react box, and can serve you as an example as I am installing yarn plus other tools using the procedure I explained above.

    In addition to that, I also start yarn using an entrypoint bash script linked to docker-compose.yml file using docker-compose entrypoint property.

    https://github.com/lopezator/hello-docker-react