Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have dotnet core 2.2 (aspnet core) app running in Docker container. I'm using the simplest possible Dockerfile you can find in any basic tutorial:
use
microsoft/dotnet:2.2-sdk
as base image
copy *.csproj
restore packages
build
publish to /app folder
use
microsoft/dotnet:2.2.1-aspnetcore-runtime
to run the app from /app folder
Now I'd like to grab some data from another website. It is a SPA, so I need to use a browser to render the page first - I decided to use Selenium with ChromeDriver because I'm already a little bit familiar with them.
I've added
Selenium.WebDriver v3.141
and
Selenium.WebDriver.ChromeDriver v73.0
to my project, set Selenium there. Locally on Windows it works fine. But when I run this via Docker I'm getting:
The file /app/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
So now I'm wondering how can I run Selenium + single instance Chrome (there is no need to set up Selenium Grid for my purpose) with dotnet core 2.2 in Docker.
I suppose I need to create custom Dockerfile which:
installs selenium, chrome and all their dependencies
installs dotnet
does the same as my current Dockerfile to build and run my app
But I'm not really sure how to do this. Especially how to "nest" Dockerfiles.
Should I do this composition in a single Dockerfile? Should I create Dockerfile for Selenium + ChromeDriver and use it as base image for next step?
–
–
TL;DR; You have to install chrome into the docker image by putting the
commands in the Docker file.
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch
# Install Chrome
RUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
hicolor-icon-theme \
libcanberra-gtk* \
libgl1-mesa-dri \
libgl1-mesa-glx \
libpango1.0-0 \
libpulse0 \
libv4l-0 \
fonts-symbola \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /var/lib/apt/lists/*
# Add your dotnet core project build stuff here
Easier solution - I pushed this as a docker image in my docker hub repo so you can use it as your base image. See this example of my dotnet core 2.2
FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS build WORKDIR /src
COPY ["MyProject.csproj", ""]
RUN dotnet restore "MyProject.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "MyProject.csproj" -c Prod -o /app
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Prod -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]
How did this happen?
Basically created a new project in visual studio for dotnet core 2.2 mvc with docker support.
Intentions are to run my dotnet core app in a linux container
Assumed that by installing nuget packages Selenium.Support, Selenium.WebDriver, Selenium.WebDriver.ChromeDriver anything I needed would be included in the docker container automatically since Selenium.WebDriver supports .NetStandard 2.0 (BTW the others don't, just realized that)
Turns out you have to install chrome into the docker image by putting the commands in the Docker file.
I've explained the whole learning process here including how I found this working code: https://hub.docker.com/r/masteroleary/selenium-dotnetcore2.2-linux
–
–
Since the appearance in dotnet core of self-contained applications I think a better approach is to use the official selenium docker:
https://hub.docker.com/r/selenium/standalone-chrome
and build the application self contained.
Here is my dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /app
COPY . ./
RUN dotnet publish MyApp.csproj -c Release -o out --self-contained -r linux-x64 /p:PublishTrimmed=true
FROM selenium/standalone-chrome
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["./MyApp"]
this is updated version for dotnet 6.0
also This is a multi stage Docker File and help with faster build
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
RUN apt update && apt install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
hicolor-icon-theme \
libcanberra-gtk* \
libgl1-mesa-dri \
libgl1-mesa-glx \
libpango1.0-0 \
libpulse0 \
libv4l-0 \
fonts-symbola \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
/* The .net App Docker Configuration */
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN echo ls -a
ENTRYPOINT ["dotnet", "{Entry Point File Name}.dll"]
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.