javascriptnode.jsdockerfileapify

Apify how can docker run


With Node/Apify/Puppeteer how can I containerize my app and test run on local? Here is my setup.

Dockerfile

# First, specify the base Docker image. 
# Note that you can use any other image from Docker Hub.
FROM node:14-alpine

# Resolves Error: EACCES: permission denied
USER root

# Second, copy just package.json 
COPY package.json ./

# Install NPM packages, skip optional and development dependencies to
RUN npm --quiet set progress=false \
 && npm install --only=prod --no-optional \
 && echo "Installed NPM packages:" \
 && npm list || true \
 && echo "Node.js version:" \
 && node --version \
 && echo "NPM version:" \
 && npm --version

# Next, copy the remaining files and directories with the source code.

COPY --chown=myuser . ./

# CMD node main.js

package.json

{
  "name": "actor-quick-start",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "apify": "^2.3.0",
    "cheerio": "^1.0.0-rc.11",
    "moment": "^2.29.3",
    "node-fetch": "^3.2.6",
    "puppeteer": "*"
  },
  "scripts": {
    "start": "node main.js"
  }
}

I was able to run on local.

$ node main.js
Launch script...
INFO  System info {"apifyVersion":"2.3.2","apifyClientVersion":"2.5.2","osType":"Darwin","nodeVersion":"v14.20.0"}
WARN  Neither APIFY_LOCAL_STORAGE_DIR nor APIFY_TOKEN environment variable is set, defaulting to APIFY_LOCAL_STORAGE_DIR="/Users/sdhangwattan/Documents/personal/testapp/my-first-act/apify_storage"
Launching Puppeteer...

However, when I build my app as docker image and ran it I get this exception

C02V25E3HV2Q:my-first-act sdhangwattan$ docker run myfirstact:0.0.2
/usr/src/app/node_modules/bindings/bindings.js:121
        throw e;
        ^

Error: Error loading shared library /usr/src/app/node_modules/better-sqlite3-with-prebuilds/build/Release/better_sqlite3.node: Exec format error
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1189:18)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at bindings (/usr/src/app/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/usr/src/app/node_modules/better-sqlite3-with-prebuilds/lib/database.js:9:24)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  code: 'ERR_DLOPEN_FAILED'

Is this an issue with my base image? I've tried with Apify's base image and was getting similar results. apify/actor-node:14&16

Please help advice!


Solution

  • The line COPY --chown=myuser . ./ might be the issue. Unless you have a .dockerignore file with node_modules, your local node_modules will be copied into the container resulting in wrong downloaded libraries (for example if you're developing on a windows machine, no Linux libraries are downloaded).