I am using a separate nodejs container as in How to use a separate node container in your ddev setup?, but I'd really like to add the gatsby-cli npm package to it, and maybe add sudo as well. I know how to add a custom Dockerfile to the web service and do these things but how can I do it with a custom service?
You can do the same things ddev does to add a custom Dockerfile, and add a build
stanza and a .ddev/<servicename>-build
directory with the needed files.
So for a .ddev/docker-compose.node.yaml file:
services:
node:
build:
context: "${DDEV_APPROOT}/.ddev/node-build"
dockerfile: "${DDEV_APPROOT}/.ddev/node-build/Dockerfile"
args:
BASE_IMAGE: "node"
image: "node-${DDEV_SITENAME}-built"
user: "node"
restart: "no"
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.platform: ddev
com.ddev.app-type: php
com.ddev.approot: $DDEV_APPROOT
volumes:
- "../:/var/www/html:cached"
working_dir: /var/www/html
command: ["tail", "-f", "/dev/null"]
And then mkdir .ddev/node-build
and create .ddev/node-build/Dockerfile with
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests bash sudo
COPY sudoers.d/ddev /etc/sudoers.d
RUN npm install -g gatsby-cli
and .ddev/node-build/sudoers.d/ddev with
ALL ALL=NOPASSWD: ALL
The result in this case is you get gatsby-cli installed via npm, and also get bash and passwordless sudo installed. This is just an example, there is plenty more that can be done, of course.
This saves the trouble of creating and maintaining a custom Docker image and pushing it up to hub.docker.com.