46

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?

Blackbam
  • 17,496
  • 26
  • 97
  • 150

4 Answers4

66

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

sh4
  • 1,217
  • 13
  • 20
4

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.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Corradone
  • 41
  • 2
  • My containers continue running long after they've executed whatever I've specified in the "command" section. – Uncle Iroh Oct 12 '21 at 18:00
2

Try this

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

Because sudo doesn't work in docker.

sanath meti
  • 5,179
  • 1
  • 21
  • 30
-1

My first time trying to help out:

would like you to give it a try (I found it on the internet)

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