Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docker): run in docker, and build scripts for docker #1291

Merged
merged 8 commits into from Dec 11, 2020

Conversation

egidijus
Copy link
Contributor

@egidijus egidijus commented Dec 9, 2020

no reason to grant excessive permissions to your docker container, this runs headless, no errors from chromium or puppetteer.

Description

This also prints the config for puppetteer, makes it easier to troubleshoot:

streetmerchant-shop-bot    | [8:08:40 PM] warn :: ℹ printing puppeteer configs:: {
streetmerchant-shop-bot    |   "0": "--disable-dev-shm-usage",
streetmerchant-shop-bot    |   "1": "--no-sandbox",
streetmerchant-shop-bot    |   "2": "--disable-setuid-sandbox",
streetmerchant-shop-bot    |   "3": "--headless",
streetmerchant-shop-bot    |   "4": "--disable-gpu"
streetmerchant-shop-bot    | }

no more this error when running in docker

streetmerchant-shop-bot    | }
streetmerchant-shop-bot    | [8:05:39 PM] error :: ✖ something bad happened, resetting streetmerchant in 5 seconds Failed to launch the browser process!
streetmerchant-shop-bot    | Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
streetmerchant-shop-bot    | [1208/200539.568829:FATAL:zygote_host_impl_linux.cc(191)] Check failed: ReceiveFixedMessage(fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid). 
streetmerchant-shop-bot    | Received signal 6
streetmerchant-shop-bot    |   r8: 00007ddd4844  r9: 00007f3d3f6f40 r10: 0000000000000008 r11: 0000000000000246
streetmerchant-shop-bot    |  r12: 00007ffd80 r13: 00007ffddbc r14: 00007f30c4ad2170 r15: 00000000000000a0
streetmerchant-shop-bot    |   di: 0000000000000002  si: 00007dd0  bp: 00007ddd4710  bx: 0000000000000000
streetmerchant-shop-bot    |   dx: 0000000000000000  ax: 0000000000000000  cx: 00007f30cd8a71  sp: 00007ffc55d708
streetmerchant-shop-bot    |   ip: 00007d8a71 efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000
streetmerchant-shop-bot    |  trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000
streetmerchant-shop-bot    | [end of stack trace]
streetmerchant-shop-bot    | Calling _exit(1). Core file will not be generated.
streetmerchant-shop-bot    | 
streetmerchant-shop-bot    | 
streetmerchant-shop-bot    | TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
streetmerchant-shop-bot    |  {
streetmerchant-shop-bot    |   "stack": "Error: Failed to launch the browser process!\nFailed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted\n[1208/200539d8829:FATAL:zygote_host_impl_linux.cc(191)] Check failed: ReceiveFixedMessage(fds[0], kZygoteBootMessage, sizeof(kZygoteBootMessage), &boot_pid). \nReceived signal 6\n  r8: 00007d52d4844  r9: 00007d63f6f40 r10: 0000000000000008 r11: 0000000000000246\n r12: 0000d80 r13: 00007d4dbc r14: 0000d170 r15: 00000000000000a0\n  di: 0000000000000002  si: 0000d4710  bp: 00007d4710  bx: 0000000000000000\n  dx: 0000000000000000  ax: 0000000000000000  cx: 00007d71  sp: 0000d8\n  ip: 0000d8a71 efl: 0000000000000246 cgf: 002b000000000033 erf: 0000000000000000\n trp: 0000000000000000 msk: 0000000000000000 cr2: 0000000000000000\n[end of stack trace]\nCalling _exit(1). Core file will not be generated.\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n\n    at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)\n    at ChildProcess.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:184:79)\n    at ChildProcess.emit (events.js:327:22)\n    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)"
streetmerchant-shop-bot    | }

Testing

Running make will do everything for you

New dependencies

  • Make
  • docker-compose

…is runs headless, no errors from chromium or puppetteer
@egidijus egidijus requested a review from jef as a code owner December 9, 2020 09:07
@egidijus egidijus changed the title no reason to grant excessive permissions to your docker container, th… fix: run in docker, and build scripts for docker Dec 9, 2020
@jef
Copy link
Owner

jef commented Dec 9, 2020

Love this! Does docker-compose come with installs on Windows? I know for macOS and Linux you need to install separately, so it would need to be a prerequisite.

@joshstubble
Copy link

Docker Compose does come installed in the docker desktop app on windows

Dockerfile Outdated Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
@jef
Copy link
Owner

jef commented Dec 9, 2020

On second thought, I'm a little hesitant on the docker-compose. Not that it's bad to have or even gives a quality of life, but I think the Makefile alone is sufficient. And that may even be something I don't necessarily want to maintain either, along with docker-compose.

I'd also like to add that it's somewhat verbose given we can use env-file with docker-run. The main benefit of using docker-compose is utilizing multiple containers and/or advanced volumes and networks -- neither of which streetmerchant has. That's my knee jerk reaction.

That being said, I really like the changes and it seems to be working great on my end. I appreciate your contribution and would like to hear your thoughts!

Thanks.

@jef jef mentioned this pull request Dec 9, 2020
@egidijus
Copy link
Contributor Author

On second thought, I'm a little hesitant on the docker-compose. Not that it's bad to have or even gives a quality of life, but I think the Makefile alone is sufficient. And that may even be something I don't necessarily want to maintain either, along with docker-compose.

I'd also like to add that it's somewhat verbose given we can use env-file with docker-run. The main benefit of using docker-compose is utilizing multiple containers and/or advanced volumes and networks -- neither of which streetmerchant has. That's my knee jerk reaction.

That being said, I really like the changes and it seems to be working great on my end. I appreciate your contribution and would like to hear your thoughts!

Thanks.

🤷‍♀️
In my experience if you are working locally, even if it's a single container docker-compose build/up > plain docker run .

I could change the docker-compose.yml to v 2.0, you could use the docker-compose.yml to spin things up on portainer, some people run that on their NAS devices.

Obviously I like docker-compose and Makefiles, because it makes it super easy to get started without even reading any docs 😁

I see makefiles and docker-compose as build tools, once you get them going, they kinda work without much attention (caveat: if you have complex systems, your build tools will get complex, this is not complex).

Copy link
Owner

@jef jef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see where you're coming from. If you don't mind though, I'll make a couple suggestions and you let me know what you think.

I really appreciate your contribution and your time for the input and feedback!

Makefile Outdated Show resolved Hide resolved
@@ -0,0 +1,14 @@
version: '3.0'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version: '3.0'
version: '3.8'

Let's pin to the latest version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested if this docker-compose is compatible with v3.8 compose file.
these versions are for the docker-compose.yml file, across the versions some add more features at the cost of mandatory complexity.

We can stick with v3.0 of docker-compose file until the end of days (caveat: if we do not need any functionality from greater versions, and do not mind slight change of syntax).

https://docs.docker.com/compose/compose-file/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! I can get with that. Since we're pinning to 3.0, that makes sense to me.

docker-compose.yml Outdated Show resolved Hide resolved
docker-compose.yml Outdated Show resolved Hide resolved
egidijus and others added 3 commits December 11, 2020 09:13
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
Copy link
Owner

@jef jef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work!!! I really appreciate your feedback and the time you took to implement this.

@jef jef changed the title fix: run in docker, and build scripts for docker fix(docker): run in docker, and build scripts for docker Dec 11, 2020
@jef jef merged commit c74ea28 into jef:main Dec 11, 2020
ven0ms99 pushed a commit to ven0ms99/streetmerchant that referenced this pull request Dec 12, 2020
Updates unnecessary need to grant special privileges when running in Docker.
@rchenzheng
Copy link
Contributor

Thanks @jef @egidijus

@TDCroPower
Copy link

TDCroPower commented Dec 19, 2020

the Docker image was unfortunately only built for amd64 and therefore does not work on my Helios64 with rockchip64 (arm64/v8) architecture.
Do you have a tip how I can rewrite the Dockerfile so that I can build it on the Helios64 as well?

here the output from Helios64...

root@helios64:/opt/docker/streetmerchant# docker run -it --rm \
>   --env-file ./dotenv \
>   ghcr.io/jef/streetmerchant:nightly
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
standard_init_linux.go:219: exec user process caused: exec format error
root@helios64:/opt/docker/streetmerchant# uname -a
Linux helios64 5.9.14-rockchip64 #20.11.4 SMP PREEMPT Tue Dec 15 08:52:20 CET 2020 aarch64 GNU/Linux
root@helios64:/opt/docker/streetmerchant#

edit:
I have customized your Dockerfile with another node image and copied the files manually into the build directory.

My customized Dockerfile with "node:14.15.3-buster" instead of "node:14.15.0-alpine3.12".
is the change necessary?

FROM node:14.15.3-buster AS builder

LABEL org.opencontainers.image.source="https://github.com/jef/streetmerchant"

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

WORKDIR /build

COPY package.json package.json
COPY package-lock.json package-lock.json
COPY tsconfig.json tsconfig.json
RUN npm ci

COPY src/ src/
RUN npm run build
RUN npm prune --production

FROM node:14.15.3-buster

RUN apk add --no-cache chromium

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \
	DOCKER=true

RUN addgroup -S appuser && adduser -S -g appuser appuser \
	&& mkdir -p /home/appuser/Downloads /app \
	&& chown -R appuser:appuser /home/appuser \
	&& chown -R appuser:appuser /app

USER appuser

WORKDIR /app

COPY --from=builder /build/node_modules/ node_modules/
COPY --from=builder /build/build/ build/
COPY web/ web/
COPY package.json package.json
COPY version.txt version.txt

ENTRYPOINT ["npm", "run"]
CMD ["start:production"]

however there seems to be a problem installing chromium or?

root@helios64:/opt/docker/streetmerchant# docker build -t streetmerchant .
Sending build context to Docker daemon  1.111MB
Step 1/24 : FROM node:14.15.3-buster AS builder
 ---> 0fff5459f31f
Step 2/24 : LABEL org.opencontainers.image.source="https://github.com/jef/streetmerchant"
 ---> Using cache
 ---> 0af35811e896
Step 3/24 : ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
 ---> Using cache
 ---> eab7baf56d05
Step 4/24 : WORKDIR /build
 ---> Using cache
 ---> 263f477ff905
Step 5/24 : COPY package.json package.json
 ---> 948ec20ecd9a
Step 6/24 : COPY package-lock.json package-lock.json
 ---> afe96990d670
Step 7/24 : COPY tsconfig.json tsconfig.json
 ---> d423520b99e7
Step 8/24 : RUN npm ci
 ---> Running in de41b35e2907

> twitch@4.3.6 postinstall /build/node_modules/twitch
> node ./scripts/npm/postinstall.js


> top-user-agents@1.0.19 postinstall /build/node_modules/top-user-agents
> node scripts/postinstall

TypeError: Cannot read property 'host' of undefined
    at got.extend.hooks.beforeRequest (/build/node_modules/hooman/hooman.js:38:48)
    at Immediate.<anonymous> (/build/node_modules/got/source/request-as-event-emitter.js:300:11)

> puppeteer@5.5.0 install /build/node_modules/puppeteer
> node install.js

FROM node:14.15.3-buster AS builder

LABEL org.opencontainers.image.source="https://github.com/jef/streetmerchant"

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

WORKDIR /build

COPY package.json package.json
COPY package-lock.json package-lock.json
COPY tsconfig.json tsconfig.json
RUN npm ci

COPY src/ src/
RUN npm run build
RUN npm prune --production

FROM node:14.15.3-buster

RUN apk add --no-cache chromium

ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \
        DOCKER=true

RUN addgroup -S appuser && adduser -S -g appuser appuser \
        && mkdir -p /home/appuser/Downloads /app \
        && chown -R appuser:appuser /home/appuser \
        && chown -R appuser:appuser /app

USER appuser

WORKDIR /app

COPY --from=builder /build/node_modules/ node_modules/
COPY --from=builder /build/build/ build/
COPY web/ web/
COPY package.json package.json
COPY version.txt version.txt

ENTRYPOINT ["npm", "run"]
CMD ["start:production"]
~
~
"Dockerfile" 41L, 918C                                                                                                                      1,24          All
**INFO** Skipping browser download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" environment variable was found.

> nodemon@2.0.6 postinstall /build/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

added 1236 packages in 38.771s
Removing intermediate container de41b35e2907
 ---> f012d754ab62
Step 9/24 : COPY src/ src/
 ---> 0032adf94bdc
Step 10/24 : RUN npm run build
 ---> Running in e57c956e2f37

> streetmerchant@ build /build
> tsc

Removing intermediate container e57c956e2f37
 ---> 1a183b3784a8
Step 11/24 : RUN npm prune --production
 ---> Running in 70889104db9a
npm WARN hooman@1.2.6 requires a peer of got@>=11.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN twilio@3.54.1 requires a peer of @types/express@^4.17.7 but none is installed. You must install peer dependencies yourself.
npm WARN twilio@3.54.1 requires a peer of @types/qs@6.9.4 but none is installed. You must install peer dependencies yourself.

removed 672 packages, updated 251 packages and audited 818 packages in 31.192s

63 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Removing intermediate container 70889104db9a
 ---> abed1acbf70c
Step 12/24 : FROM node:14.15.3-buster
 ---> 0fff5459f31f
Step 13/24 : RUN apk add --no-cache chromium
 ---> Running in 9f776797238b
/bin/sh: 1: apk: not found
The command '/bin/sh -c apk add --no-cache chromium' returned a non-zero code: 127
root@helios64:/opt/docker/streetmerchant#

edit3:
ok it works with Docker + Portainer (for the docker-compose file) on Helios64.
I have build the image with your Dockerfile and set this into the Stacks...

version: "2"
services:
  streetmerchant:
    image: streetmerchant:latest
    container_name: streetmerchant
    volumes:
      - /opt/docker/streetmerchant/dotenv:/app/dotenv
    network_mode: host
    restart: unless-stopped

and here the log of the running container...

> streetmerchant@ start:production /app
> node build/index.js

STREETMERCHANT
3.3.0

[5:34:25 AM] info :: ℹ selected stores: alternate, amazon-de, amazon-de-warehouse, amazon-es, amazon-fr, amazon-it, cyberport, euronics-de, expert, otto, proshop-de, saturn, spielegrotte
[5:34:25 AM] info :: ℹ selected series: sonyps5c, sonyps5de
[5:34:29 AM] info :: ℹ puppeteer config:  {
  "0": "--disable-dev-shm-usage",
  "1": "--no-sandbox",
  "2": "--disable-setuid-sandbox",
  "3": "--headless",
  "4": "--disable-gpu"
}

[5:34:36 AM] warn :: ✖ [amazon-de-warehouse] [sony (sonyps5c)] ps5 console :: STATUS CODE ERROR 503
[5:34:37 AM] info :: ✖ [spielegrotte] [sony (sonyps5c)] ps5 console :: OUT OF STOCK
[5:34:39 AM] info :: ✖ [amazon-es] [sony (sonyps5c)] ps5 console :: OUT OF STOCK
[5:34:41 AM] warn :: ✖ [amazon-it] [sony (sonyps5c)] ps5 console :: CAPTCHA
[5:34:42 AM] info :: ✖ [spielegrotte] [sony (sonyps5de)] ps5 digital :: OUT OF STOCK
[5:34:43 AM] info :: ✖ [amazon-fr] [sony (sonyps5c)] ps5 console :: OUT OF STOCK
[5:34:44 AM] warn :: ✖ [amazon-de] [sony (sonyps5c)] ps5 console :: CAPTCHA
[5:34:44 AM] info :: ✖ [amazon-es] [sony (sonyps5de)] ps5 digital :: OUT OF STOCK
[5:34:46 AM] info :: ✖ [amazon-fr] [sony (sonyps5de)] ps5 digital :: OUT OF STOCK

@kiowadriver
Copy link

I am able to run fine without the SYS_ADMIN parameter (less of a security risk).

I have included some build instructions in my fork which i am keeping up to date with the main branch with some minor tweaks for unraid usage:
https://github.com/kiowadriver/streetmerchant/blob/main/README.md

@rchenzheng
Copy link
Contributor

I am able to run fine without the SYS_ADMIN parameter (less of a security risk).

I have included some build instructions in my fork which i am keeping up to date with the main branch with some minor tweaks for unraid usage:
https://github.com/kiowadriver/streetmerchant/blob/main/README.md

It has already been merged

/close

@TDCroPower
Copy link

TDCroPower commented Dec 21, 2020

i first had the problem on my helios with aarch64 and was able to work around it.
Now I wanted to switch to the OrangePi to run the script on the side, but there is again the problem with the architecture because this uses armv7l and there is no chromium in the alpine package manager...

aarch64...
https://pkgs.alpinelinux.org/packages?name=chromium&branch=edge&arch=aarch64

armv7l...
https://pkgs.alpinelinux.org/packages?name=chromium&branch=edge&arch=armv7

should I rather create a new issue for this or can we discuss it further here?

@jef
Copy link
Owner

jef commented Dec 21, 2020

If you're having problems with install the Chromium browser, it's because of the package manager used in alpine is apk and debian uses apt. You'll need to replace those lines.

I prefer that we keep this image using alpine, as it should work on most systems.

If you'd like make this work for your ARM architecture, I think you should change the image to use arm64v8/node:14.15.0-alpine3.12.

@TDCroPower
Copy link

TDCroPower commented Dec 21, 2020

@jef the problem no longer exists for the Helios64 arm64v8, there it worked in the end, because for this there is the Chromium package at alpine.
However, there is no Chromium for armv7l at alpine, see link above.
Would need an alternative image or another variant to install Chromium in the image.

I am currently testing to rewrite the Dockerfile so that I work with buster-slim, since there should be the Chromium package on all architectures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants