Peza
Peza

Reputation: 1437

ffmpeg install within existing Node.js docker image

I need to use ffmpeg in a Node.js application that runs in a docker container (created using docker-compose). I'm very new to Docker, and would like to know how to command Docker to install ffmpeg when creating the image.

DockerFile

FROM node:carbon
WORKDIR /usr/src/app

# where available (npm@5+)
COPY package*.json ./
RUN npm install -g nodemon
RUN npm install --only=production
COPY . .

EXPOSE 3000
CMD [ "npm", "start" ] 

package.json:

{
  "name": "radcast-apis",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "dev": "nodemon --inspect-brk=0.0.0.0:5858 ./bin/www"
  },
  "dependencies": {
    "audioconcat": "^0.1.3",
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "express": "~4.16.0",
    "firebase-admin": "^5.12.1",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "morgan": "~1.9.0"
  },
  "devDependencies": {
    "nodemon": "^1.11.0"
  }
}

docker-compose.yml:

version: "2"
services:
  web:
    volumes:
    - "./app:/src/app"
    build: .
    command: npm run dev
    ports:
    - "3000:3000"
    - "5858:5858"

Upvotes: 10

Views: 12008

Answers (3)

Ranjitha Shenoy
Ranjitha Shenoy

Reputation: 71

You may use the below docker file for installing FFmpeg packages if you are on alpine.

FROM node:8.11-alpine

WORKDIR /usr/src/app

ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

COPY package*.json /usr/src/app/
RUN npm install
RUN apk update
RUN apk add
RUN apk add ffmpeg

COPY . /usr/src/app

ENV PORT 5000
EXPOSE $PORT
CMD [ "npm", "start" ]

Upvotes: 7

posit labs
posit labs

Reputation: 9471

ffmpeg-static will work fine, but it means that every time you change package.json, or anything above the COPY command for package.json, you'll have to wait for the npm install command to re-download the bins.

There is another method, using multi-stage builds. This method won't require re-downloading or rebuilding ffmpeg. There are prebuilt ffmpeg images at jrottenberg/ffmpeg.

For alpine, your image would look like this...

FROM jrottenberg/ffmpeg:3.3-alpine
FROM keymetrics/node:8-alpine

# copy ffmpeg bins from first image
COPY --from=0 / /

Related question: Copy ffmpeg bins in multistage docker build

Upvotes: 12

Peza
Peza

Reputation: 1437

If it helps anyone, I figured out a way.

  • Use ffmpeg-static by adding the entry to package.json "ffmpeg-static": "^2.3.0", this makes the binary files available in the docker container.
  • By using ffmpeg_static = require('ffmpeg-static') and then inspecting the path property on ffmpeg_static, you can see where the binaries are in the container.
  • Add this path to an ENV variable: ENV PATH="/your/path/to/node_modules/ffmpeg-static/bin/linux/x64:${PATH}"

That worked a trick for us! The answer that saved us was an analogous use-case for firebase cloud functions - here.

Upvotes: 8

Related Questions