Cant launch chrome in docker linux container - linux

I have an asp.net core application that uses the jsreport nuget packages to run reports. I am attempting to deploy it with a linux docker container. I seem to be having trouble getting chrome to launch when I run a report. I am getting the error:
Failed to launch chrome! Running as root without --no-sandbox is not supported.
I have followed the directions on the .net local reporting page (https://jsreport.net/learn/dotnet-local) regarding docker, but I am still getting the error.
Here is my full docker file:
#use the .net core 2.1 runtime default image
FROM microsoft/dotnet:2.1-aspnetcore-runtime
#set the working directory to the server
WORKDIR /server
#copy all contents in the current directory to the container server directory
COPY . /server
#install node
RUN apt-get update -yq \
&& apt-get install curl gnupg -yq \
&& curl -sL https://deb.nodesource.com/setup_8.x | bash \
&& apt-get install nodejs -yq
#install jsreport-cli
RUN npm install jsreport-cli -g
#install chrome for jsreport linux
RUN apt-get update && \
apt-get install -y gnupg libgconf-2-4 wget && \
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
apt-get update && \
apt-get install -y google-chrome-unstable --no-install-recommends
ENV chrome:launchOptions:executablePath google-chrome-unstable
ENV chrome:launchOptions:args --no-sandbox
#expose port 80
EXPOSE 80
CMD dotnet Server.dll
Is there another step that I am missing somewhere?

Its little late but may be can help someone else.
For me, the only option that was needed to fix this issue in the docker container was to run chrome in a headless mode (so cause was in tests not in dockerfile).
ChromeOptions options = new ChromeOptions().setHeadless(true);
WebDriver driver = new ChromeDriver(options);
Results: Now tests run successfully, without any errors.

Expanding on Pramod's answer, my own issues were only solved by running with both the --headless and --no-sandbox flags.

Related

Install nodejs and npm in Dockerfile

The context
I have a Dockerfile to create an image that contains an apache webserver. However I also want to build my website using the Dockerfile so that the build process isn't dependent on the developers local environment. Note that the docker container is only going to be used for local development not for production.
The problem
I have this Dockerfile:
FROM httpd
RUN apt-get update -yq
RUN apt-get -yq install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get update -yq
RUN apt-get install -yq \
dh-autoreconf=19 \
ruby=1:2.5.* \
ruby-dev=1:2.5.* \
nodejs
I build it:
sudo docker build --no-cache .
The build completes successfully, here is part of the output:
Step 9/15 : RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
---> Running in e6c747221ac0
......
......
......
Removing intermediate container 5a07dd0b1e01
---> 6279003c1e80
Successfully built 6279003c1e80
However, when I run the image in a container using this:
sudo docker container run --rm -it --name=debug 6279003c1e80 /bin/bash
Then when doing apt-cache policy inside the container, it doesn't show the repository that should have been added with the curl command. Also when doing apt-cache policy nodejs it shows the old version is installed.
However when I then run the following inside the container:
curl -sL https://deb.nodesource.com/setup_12.x | bash
apt-cache policy
apt-cache policy nodejs
It shows me the repository is added and it shows the newer nodejs version is available.
So why is it that when using the curl command using RUN inside the docker file it doesn't seem to work, but when doing it manually in the container from a shell then it does work? And how can I get around this problem?
Updates
Note that to prevent caching issues I am using the --no-cache flag.
I also removed all containers and did sudo docker system prune and rebuild the image but without success.
I tried bundling everything in one RUN command as user "hmm" suggested (as this is best practice for apt commands):
RUN apt-get update -yq \
&& apt-get -yq install curl gnupg && \
&& curl -sL https://deb.nodesource.com/setup_12.x | bash \
&& apt-get update -yq \
&& apt-get install -yq \
dh-autoreconf=19 \
ruby=1:2.5.* \
ruby-dev=1:2.5.* \
nodejs \
&& rm -rf /var/lib/apt/lists/*
You're likely running into issues with cached layers. There's a long section in the Dockerfile best practices documentation on using apt-get. Probably worth a read.
The gist is that Docker doesn't recognize any difference between the first and second RUN apt-get update, nor does it know that apt-get install depends on a fresh apt-get update layer.
The solution is to combine all of that into a single RUN command (recommended) or disable the cache during the build process (docker build --no-cache).
RUN apt-get update -yq \
&& apt-get -yq install curl gnupg ca-certificates \
&& curl -L https://deb.nodesource.com/setup_12.x | bash \
&& apt-get update -yq \
&& apt-get install -yq \
dh-autoreconf=19 \
ruby=1:2.5.* \
ruby-dev=1:2.5.* \
nodejs
Edit: Running your Dockerfile locally, I noticed no output from the curl command. After removing the -s flag (fail silently), you can see it's failing due to not being able to verify the server's SSL certificate:
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
The solution to that issue is to install ca-certificates before running curl. I've updated the RUN command above.

How to refresh your shell when using a Dockerfile?

I am trying to build a Dockerfile that can make use of Azure functions. After unsuccessfully trying to build it using alpine:3.9 because of library issues, I swapped to ubuntu:18.04. Now I have a problem in that I can't install nvm (node version manager) in such a way that I can install node. My Dockerfile is below. I have managed to install nvm but now, while trying to use nvm, I cannot install the node version I want. The problem probably has to do with refreshing the shell but that is tricky to do as it appears that Docker continues to use the original shell it entered to run the next build stages. Any suggestions on how to refresh the shell so nvm can work effectively?
FROM ubuntu:18.04
RUN apt update && apt upgrade -y && apt install -qq -y --no-install-recommends \
python-pip \
python-setuptools \
wget \
build-essential \
libssl-dev
RUN pip install azure-cli
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
RUN . /root/.nvm/nvm.sh && nvm install 10.14.1 && node
ENTRYPOINT ["/bin/bash"]
After install nvm command put:
SHELL ["/bin/bash", "--login" , "-c"]
RUN nvm install 17
SHELL ["/bin/sh", "-c"]
Default shell is sh and first command switches it to bash. Parameter --login is required as you want to source .bashrc.
As all subsequent commands would be executed with changed shell it's good to switch it back to sh if you don't need it anymore.
You usually don't need version managers like nvm in a Docker image. Since a Docker image packages only a single application, and since it has its own isolated filesystem, you can just install the single version of Node you need.
The first thing I'd try is to just install whatever version of Node the standard Ubuntu package has (in Ubuntu 18.04, looks like 8.11). While there are some changes between Node versions, for the most part the language and core library have been pretty stable.
RUN apt update && apt-install nodejs
Or, if you need something newer, there are official Debian packages:
RUN curl -sSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - \
&& echo "deb https://deb.nodesource.com/node_10.x cosmic main" > /etc/apt/sources.list.d/nodesource.list \
&& apt update \
&& apt install nodejs
This will give you a current version of that major version of Node (as of this writing, 10.15.1).
If you really need that specific version of Node, there are official binary packages. I might write:
FROM ubuntu:18.04
ARG node_version=10.14.1
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
ca-certificates \
curl \
xz-utils
RUN cd /usr/local \
&& curl -o- https://nodejs.org/dist/v${node_version}/node-v${node_version}-linux-x64.tar.xz \
| tar xJf - --strip 1
...where the last couple of lines unpack the Node tarball directly into /usr/local.

Trouble installing NodeJS on Kali Linux docker image

I am trying to create a docker image based on the Kali Linux base image, and I need to install NodeJS as a dependency for my application.
Here's my Dockerfile:
FROM kalilinux/kali-linux-docker
RUN apt-get update -y && \
apt-get dist-upgrade -y && \
apt-get autoremove -y && \
apt-get clean -y
RUN apt-get install curl -y
RUN curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - \
&& apt-get install nodejs -y
RUN npm i -g npm
ENV NODE_ENV production
WORKDIR /root/app
COPY . .
RUN npm i
EXPOSE 4000
ENTRYPOINT ["npm", "start"]
However, I hit the following error trying to install NodeJS:
Step 4/11 : RUN curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - && apt-get install nodejs -y
---> Running in a63e56802eba
## Installing the NodeSource Node.js 8.x LTS Carbon repo...
## Inspecting system...
## You don't appear to be running an Enterprise Linux based system,
please contact NodeSource at https://github.com/nodesource/distributions/issues
if you think this is incorrect or would like your distribution to be considered
for support.
The command '/bin/sh -c curl --silent --location https://rpm.nodesource.com/setup_8.x | bash - && apt-get install nodejs -y' returned a non-zero code: 1
Admittedly I'm confused by a few things... namely that I was under the impression that NodeJS was already installed on Kali Linux (I have a VirtualBox VM using Debian 64-bit where it exists). I went as far as trying to install the kali-linux-all metapackage, but NodeJS/npm don't seem to exist.
Am I simply misunderstanding some basic premise of Docker and/or Kali Linux? Is there any other way to install NodeJS into my container?
I still don't fully understand why NodeJS is installed on my VM but not the base Kali docker image... but in any case I did manage to unblock myself.
First, I was pulling down a NodeJS installation script from nodesource which required rpm -- I found a different script that works without it. However the new script also required that I install gnupg.
Here's my updated Dockerfile:
FROM kalilinux/kali-linux-docker
RUN apt-get update -y && \
apt-get dist-upgrade -y && \
apt-get autoremove -y && \
apt-get clean -y
RUN apt-get -y install curl gnupg
RUN curl --silent --location https://deb.nodesource.com/setup_8.x | bash - \
&& apt-get install nodejs -y
RUN npm i -g npm
ENV NODE_ENV production
WORKDIR /root/app
COPY . .
RUN npm i
EXPOSE 4000
ENTRYPOINT ["npm", "start"]

Dockerfile ubuntu only installs node version 4.2

This dockerfile installs nodejs version 4.2 and I cant understand why. could someone please help me install node 9.2. i've tried taking out the -- no install-recommends command to no avail.
adding more text her because stack would not let me post this even though it is a very simple question that I've looked on the web for quite some time about to no avail.adding more text her because stack would not let me post this even though it is a very simple question that I've looked on the web for quite some time about to no avail.
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --no-install-recommends curl sudo
RUN curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
RUN apt-get install -y nodejs && \
apt-get install --yes build-essential
RUN apt-get install --yes npm
#VOLUME "/usr/local/app"
# Set up C++ dev env
RUN apt-get update && \
apt-get dist-upgrade -y && \
apt-get install gcc-multilib g++-multilib cmake wget -y && \
apt-get clean autoclean && \
apt-get autoremove -y
#wget -O /tmp/conan.deb -L https://github.com/conan-io/conan/releases/download/0.25.1/conan-ubuntu-64_0_25_1.deb && \
#dpkg -i /tmp/conan.deb
#ADD ./scripts/cmake-build.sh /build.sh
#RUN chmod +x /build.sh
#RUN /build.sh
RUN mkdir -p /usr/local/app
WORKDIR /usr/local/app
COPY package.json /usr/local/app
RUN ["npm", "install"]
COPY . .
RUN echo "/usr/local/app/dm" > /etc/ld.so.conf.d/mythrift.conf
RUN echo "/usr/lib/x86_64-linux-gnu" >> /etc/ld.so.conf.d/mythrift.conf
RUN echo "/usr/local/lib64" >> /etc/ld.so.conf.d/mythrift.conf
RUN ldconfig
RUN chmod +x dm/dm3
RUN ldd dm/dm3
RUN ["chmod", "+x", "dm/dm3"]
RUN ["chmod", "777", "policy"]
RUN ls -al .
RUN ["nodejs", "-v"]
CMD ["nodejs", "-v"]
EDIT
Apparently it's important for the OP to run exactly this version of ubuntu. Here's a sample that builds on top of FROM ubuntu:16.04:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --reinstall ca-certificates curl build-essential \
&& curl -s https://nodejs.org/dist/v9.9.0/node-v9.9.0-linux-x64.tar.xz \
-o node-v9.9.0-linux-x64.tar.xz && tar xf node-v9.9.0-linux-x64.tar.xz \
&& cd node-v9.9.0-linux-x64 && cp -r bin include lib share /usr/local \
&& rm -rf /node-v9.9.0-linux-x64.tar.xz /node-v9.9.0-linux-x64
CMD ["node", "-v"]
Build
docker build -t testing .
Test
docker run testing
v9.9.0
Note that this only takes care of the node related things and don't take into account all the other dependencies.
The reason you are getting node 4 is because apt-get only installs the default version of a package which will never be the cutting edge latest.
Whilst this issue is present in a Docker container, it is not specific to Docker as it will happen on any Ubuntu installation, both inside or outside of Docker.
To get the latest version you have 2 options.
(1) Install using a PPA:
cd ~
curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
nodejs -v
(2) Install using Node Version Manager (nvm)
The latter is great because it lets you install multiple versions of Node and jump between them very quickly.
Here's a link to an amazing Digital Ocean article on this very topic:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04
Here's a link to NVM ... https://github.com/creationix/nvm

Install node in Dockerfile?

I am user of AWS elastic beanstalk, and I have a little problem. I want to build my CSS files with less+node. But I don`t know how to install node in my dockerfile, when building with jenkins.
Here is installation packages what I am using in my docker. I will be glad for any suggestions.
FROM php:5.6-apache
# Install PHP5 and modules along with composer binary
RUN apt-get update
RUN apt-get -y install \
curl \
default-jdk \
git \
libcurl4-openssl-dev \
libpq-dev \
libmcrypt-dev \
libpq5 \
npm \
node \
zlib1g-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install curl json mbstring opcache pdo_mysql zip gd exif sockets mcrypt
# Install pecl
RUN pecl install -o -f memcache-beta \
&& rm -rf /tmp/pear \
&& echo 'extension=memcache.so' > /usr/local/etc/php/conf.d/memcache.ini
After this I am runing my entrypoint.sh with code
#!/usr/bin/env sh
composer run-script post-install-cmd --no-interaction
chmod 0777 -R /var/app/app/cache
chmod 0777 -R /var/app/app/logs
exec apache2-foreground
But then I`ve got this error
Error Output: [2016-04-04 11:23:44] assetic.ERROR: The template ":tmp:module.html.twig" contains an error: A template that extends another one cannot have a body in ":tmp:module.ht
ml.twig" at line 7.
But when I install inside the Docker container node this way
apt-get install git-core curl build-essential openssl libssl-dev
git clone https://github.com/nodejs/node.git
cd node
./configure
make
sudo make install
node -v
I can build my CSS. So question is..how this installation above make install inside my Dockerfile when I am building it with Jenkins?
I think this works slightly better.
ENV NODE_VERSION=16.13.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version
Note that nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
Running apt-get install node does not install Node.js, because that's not the package you're asking for.
If you run apt-cache info node you can see that what you are installing is a "Amateur Packet Radio Node program (transitional package)"
You should follow the Node.js install instructions to install via package manager.
Or if you like building from git, you can just do that inside Docker:
RUN apt-get install -y git-core curl build-essential openssl libssl-dev \
&& git clone https://github.com/nodejs/node.git \
&& cd node \
&& ./configure \
&& make \
&& sudo make install
According to the following answer, I would suggest using npm via the n package, that lets you choose the nodejs version, or use the latest tag or the lts tag. For example for latest:
RUN apt-get update && apt-get install -y \
software-properties-common \
npm
RUN npm install npm#latest -g && \
npm install n -g && \
n latest
Just 2 lines
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
Get the node image and put it at the top of your dockerfile:
FROM node:[tag_name] AS [alias_name]
Verify the version by adding following code:
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
Then add the following code every time you need to use nodejs in a container:
COPY --from=[alias_name] . .
From the codes above, replace the following with:
[tag_name] - the tag value of the node image you want to use. Visit https://hub.docker.com/_/node?tab=tags for the list of available tags.
[alias_name] - your preferred image name to use in your dockerfile.
Example:
FROM node:latest AS node_base
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
FROM php:5.6-apache
COPY --from=node_base . .
### OTHER CODE GOES HERE
Binary download without any compilation
FROM ubuntu
RUN apt-get update && apt-get install -y \
ca-certificates \
curl
ARG NODE_VERSION=14.16.0
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
ARG NODE_HOME=/opt/$NODE_PACKAGE
ENV NODE_PATH $NODE_HOME/lib/node_modules
ENV PATH $NODE_HOME/bin:$PATH
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/
# comes with npm
# RUN npm install -g typescript
I am using following Dockerfile to setup node version 8.10.0.
Here I have used NVM (Node Version Manager ), so we can choose which node version should be installed on that container. Please use absolute path of npm when installing node modules (eg: /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot#latest -g)
FROM ubuntu:18.04
ENV NODE_VERSION=8.10.0
RUN apt-get update && \
apt-get install wget curl ca-certificates rsync -y
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm /usr/bin/
RUN /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot#latest -g
Note: This is a cropped Dockerfile.
The short answer, for example, install v14.17.1
ENV PATH="/opt/node-v14.17.1-linux-x64/bin:${PATH}"
RUN curl https://nodejs.org/dist/v14.17.1/node-v14.17.1-linux-x64.tar.gz |tar xzf - -C /opt/
list of all available versions can be found here -> https://nodejs.org/dist/
Directly into /usr/local so it's already in your $PATH
ARG NODE_VERSION=8.10.0
RUN curl https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz | tar -xz -C /usr/local --strip-components 1
The accepted answer gives the link to the installation instructions for all systems, but it won't run out of the box since you often (e.g. for ubuntu) don't have all required dependencies installed (namely curl and sudo).
So here's for example how you'd do it for ubuntu:
FROM ubuntu
# Core dependencies
RUN apt-get update && apt-get install -y curl sudo
# Node
# Uncomment your target version
# RUN curl -fsSL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
RUN sudo apt-get install -y nodejs
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
then build with
docker build . --progress=plain
to see the output of the echo statements. Of course you could also leave away the echo statements and run it regularly with docker build ., after you've made sure everything is working as intended.
You can also leave away the installation of sudo, but then you'll have to get rid of the sudo occurrences in the script.
FROM ubuntu:20.04
# all necessaries for next RUN
RUN set -e; \
apt-get update && \
apt-get install -qqy --no-install-recommends \
curl wget nano gnupg2 software-properties-common && \
rm -rf /var/lib/apt/lists;
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
# uncomment for checking versions
# Step 4/10 : RUN apt-cache show nodejs | grep Version;return 1;
# ---> Running in xxxxxxxxx
# Version: 14.18.2-deb-1nodesource1
# Version: 10.19.0~dfsg-3ubuntu1
#RUN apt-cache show nodejs | grep Version;return 1;
RUN set -e; \
apt-get update && \
apt-get install -qqy \
nodejs && \
rm -rf /var/lib/apt/lists;
# uncomment for check
# RUN node -v

Resources