selenium commands | Cell 2 | | Search

This Dockerfile uses the selenium standalone chrome debug image as its base, exposes ports 4200, 4444, and 3000, and sets up directories and environment variables for a Chrome browser testing environment. It also modifies the entry_point.sh script to modify Chrome preferences and change the exit behavior to report a cleaner shutdown.

Run example

npm run import -- "Run selenium inside of docker"

Run selenium inside of docker

FROM selenium/standalone-chrome-debug

EXPOSE 4200
EXPOSE 4444
EXPOSE 3000

ENV DBUS_SESSION_BUS_ADDRESS /dev/null
ENV CHROME_USER_DATA_DIR /usr/profile
WORKDIR /home/seluser

# I added this part?
USER root
RUN mkdir /usr/profile
RUN mkdir /usr/downloads
RUN chown seluser:seluser -R /usr/profile
RUN chown seluser:seluser -R /usr/downloads
RUN chmod 777 -R /usr/profile
RUN chmod 777 -R /usr/downloads
#VOLUME /usr/profile
#VOLUME /data/downloads

RUN sed -i '/wait \$NODE_PID/ised -i -e s/exit_type":"Crashed/exit_type":"None/g /usr/profile/Default/Preferences &' /opt/bin/entry_point.sh
RUN sed -i '/wait \$NODE_PID/ised -i -e s/exited_cleanly":false/exited_cleanly":true/g /usr/profile/Default/Preferences &' /opt/bin/entry_point.sh

What the code could have been:

# Use official selenium image
FROM selenium/standalone-chrome-debug as base

# Expose required ports
EXPOSE 4200
EXPOSE 4444
EXPOSE 3000

# Set environment variables
ENV DBUS_SESSION_BUS_ADDRESS /dev/null
ENV CHROME_USER_DATA_DIR /usr/profile

# Set working directory
WORKDIR /home/seluser

# Copy permissions and ownership
COPY permissions.sh /permissions.sh
RUN sh /permissions.sh

# Configure Chrome User Data
RUN sed -i '/wait \$NODE_PID/ised -i -e s/exit_type":"Crashed/exit_type":"None/g /usr/profile/Default/Preferences' /opt/bin/entry_point.sh
RUN sed -i '/wait \$NODE_PID/ised -i -e s/exited_cleanly":false/exited_cleanly":true/g /usr/profile/Default/Preferences' /opt/bin/entry_point.sh

# Create directories
RUN mkdir /usr/profile
RUN mkdir /usr/downloads

# Configure permissions
COPY permissions.sh /permissions.sh
RUN sh /permissions.sh

# Use a new user and root as builder
USER seluser
RUN chown seluser:seluser -R /usr/profile
RUN chown seluser:seluser -R /usr/downloads
RUN chmod 777 -R /usr/profile
RUN chmod 777 -R /usr/downloads

# Use Alpine for size reduction
FROM alpine:latest

# Copy necessary directories
COPY --from=0 /usr/profile /usr/profile
COPY --from=0 /usr/downloads /usr/downloads

# Set default command
CMD ["tail", "-f", "/dev/null"]

Dockerfile Breakdown

Base Image and Ports

Environment Variables

Directory Setup

Script Modifications