Run angular e2e/karma tests in a Docker container
382
Run angular karma/e2e tests in a docker container. I struggled a lot to make Angular CI pipeline work. Hope my solution helps someone!
Works for me with the following software versions:
You must add the following to your karma.config.js file:
module.exports = function (config) {
config.set({
// ...
browsers: ['Chrome', 'ChromeDocker'],
customLaunchers: {
ChromeDocker: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
// ...
});
};
Then, you can run your tests (inside the Docker container), using the command:
ng test --watch=false --browsers=ChromeDocker
There is no special configuration needed for protractorjs to work inside the container.
If you wish to change customize the image (change nodejs or chrome version), here is the Dockerfile:
FROM node:13.14.0
ENV CHROME_PACKAGE="google-chrome-stable_83.0.4103.97-1_amd64.deb"
WORKDIR /tmp
# Install dependencies
RUN echo "deb http://ftp.debian.org/debian stretch-backports main" >> /etc/apt/sources.list
RUN apt-get update -y
RUN apt-get install -y wget sudo
RUN apt-get install -y -t stretch-backports openjdk-8-jre libfontconfig1
# Install Chrome
RUN wget https://github.com/webnicer/chrome-downloads/raw/master/x64.deb/${CHROME_PACKAGE}
RUN dpkg --unpack ${CHROME_PACKAGE}
RUN apt-get install -f -y
RUN apt-get clean -y
RUN rm -rf /var/lib/apt/list
RUN rm ${CHROME_PACKAGE}
# Fix for the issue with Selenium, as described here:
# https://github.com/SeleniumHQ/docker-selenium/issues/87
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null SCREEN_RES=1280x1024x24
RUN echo "Defaults env_keep += \"DBUS_SESSION_BUS_ADDRESS\"" > /etc/sudoers.d/environment
# For protractor
RUN mv /usr/bin/google-chrome /usr/bin/google-chrome-bin
RUN echo "#!/bin/bash" > /usr/bin/google-chrome
RUN echo "/usr/bin/google-chrome-bin --headless --no-sandbox \"\$@\"" >> /usr/bin/google-chrome
RUN chmod +x /usr/bin/google-chrome
# For karma
ENV CHROME_BIN "/opt/google/chrome/chrome"
# Workspace
WORKDIR /home/root/
Many people discuss of the security treat caused by using chrome with the "--no-sandbox" flag. It is indeed not secure in your own working environment. But the Docker container being itself a sandbox, i believe this configuration is fine on a development server which doesn't map volumes or ports to the real system. Be careful though, this is just a personal reflexion made by someone who doesn't know much what he is talking about. Make your own research if you are doing something serious!
Content type
Image
Digest
Size
635.1 MB
Last updated
over 6 years ago
docker pull edoulamenace/angular