skip to Main Content

Below is my dockerfile. After the dependencies are installed, I want to delete a specific binary (ffmpeg) from the node_modules folder on the container, and then reinstall it using the install.js file that exists under the same folder in node_modules.

FROM node:16-alpine

WORKDIR /web

COPY package.json package-lock.json ./

ARG NODE_ENV
ENV NODE_ENV ${NODE_ENV:-development}

ARG _ENV
ENV _ENV ${_ENV:-dev}

RUN npm install

RUN rm /web/node_modules/ffmpeg-static/ffmpeg
RUN node /web/node_modules/ffmpeg-static/install.js

COPY . .

EXPOSE 8081
ENTRYPOINT [ "npm" ]

I want the rm and node commands to take effect on the container after the startup and when all the dependencies are installed and copied to the container, but it doesn’t happen. I feel like the commands after RUN are only executed during the build process, not after the container starts up.

After the container is up, when I ssh to it and execute the two commands above directly (RUN rm... and RUN node...), my changes are taking effect and everything works perfectly. I basically want these commands to automatically run after the container is up and running.

The docker-compose.yaml file looks like this:

version: '3'
services:
  serverless:
    build: web
    user: root
    ports:
      - '8081:8081'

    env_file:
      - env/web.env
    environment:
      - _ENV=dev
    command:
      - run
      - start
    volumes:
      - './web:/web'
      - './env/mount:/secrets/'

2

Answers


  1. if this dockerfile builds, it means you have a package-lock.json. That is evidence that npm install was executed in the root directory for that image, which means that node_modules exists locally and is being copied in the last copy.

    You can avoid this by creating a .dockerignore file that includes files and directories (aka node_modules) that you’d like to exclude from being passed to the Docker build context, which will make sure that they don’t get copied to your final image.

    You can follow this example:
    https://stackoverflow.com/a/43747867/3669093

    Update

    Dockerfile: Replace the ENTRYPOINT with the following

    ADD ./start.sh /start.sh
    CMD ["/start.sh"]
    

    Start.sh

    rm /web/node_modules/ffmpeg-static/ffmpeg
    node /web/node_modules/ffmpeg-static/install.js
    
    npm
    
    Login or Signup to reply.
  2. Try rm -rf /web/node_modules/ffmpeg-static/ffmpeg

    I assume that is directory, not a file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search