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?
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With