How to install packages from Docker compose?

You can only do it with a Dockerfile, because the command operator in docker-compose.yml only keeps the container alive during the time the command is executed, and then it stops.


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