Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
Blackbam Avatar asked Dec 20 '17 09:12

Blackbam


People also ask

Which Dockerfile instruction can be used to install packages?

To install packages in a docker container, the packages should be defined in the Dockerfile. If you want to install packages in the Container, use the RUN statement followed by exact download command . You can update the Dockerfile with latest list of packages at anytime and build again to create new image out of it.


2 Answers

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

like image 151
sh4 Avatar answered Sep 21 '22 05:09

sh4


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.

like image 30
Corradone Avatar answered Sep 23 '22 05:09

Corradone