Skip to content

Instantly share code, notes, and snippets.

@liropa
Last active October 10, 2023 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liropa/7ab51d4e605b6852774688314288f88c to your computer and use it in GitHub Desktop.
Save liropa/7ab51d4e605b6852774688314288f88c to your computer and use it in GitHub Desktop.
Dockerfile for algs4 Coursera course environment

Setup

You can save this Dockerfile and then build the image with:

docker build -t algs4env .

After building the image, you can run a container with:

docker run -it --rm -v $(pwd):/workspace algs4env

This command maps your current directory (on your host) to /workspace in the container. This means you can edit files on your host system using your preferred editor/IDE, but then compile and run them inside the container environment, which has Java 11.0 and the algs4 library set up.

Findbugs / PMD / Checkstyle

After building this Dockerfile, you can use the Checkstyle and Findbugs versions provided by the algs4 course. Note that the configurations are also downloaded and you can reference them when running these tools:

For Checkstyle:

checkstyle-algs4 <java-file-to-check>

For Findbugs:

findbugs-algs4 <class or jar file here>
# Use the official OpenJDK 11 as a parent image
FROM openjdk:11-jdk-slim
# Set environment variables for the algs4 classpath
ENV ALGS4_HOME /usr/local/algs4
ENV CLASSPATH $ALGS4_HOME/algs4.jar:.
# Create directory for the algs4 library
RUN mkdir -p $ALGS4_HOME
# Install wget to fetch resources
RUN apt-get update && apt-get install -y wget unzip
# Fetch the algs4 library
RUN wget -O $ALGS4_HOME/algs4.jar "https://algs4.cs.princeton.edu/code/algs4.jar"
# Optional: You might also want the standard input library if you're working with the course exercises
RUN wget -O $ALGS4_HOME/stdlib.jar "https://introcs.cs.princeton.edu/java/stdlib/stdlib.jar" && \
echo "export CLASSPATH=$ALGS4_HOME/stdlib.jar:$ALGS4_HOME/algs4.jar:." >> ~/.bashrc
# Download and setup FindBugs, Checkstyle, PMD, and their configurations
RUN wget -O $ALGS4_HOME/checkstyle.zip "https://algs4.cs.princeton.edu/linux/checkstyle.zip" && \
wget -O $ALGS4_HOME/findbugs.zip "https://algs4.cs.princeton.edu/linux/findbugs.zip" && \
wget -O $ALGS4_HOME/pmd.zip "https://algs4.cs.princeton.edu/linux/pmd.zip" && \
wget -O $ALGS4_HOME/findbugs.xml "https://algs4.cs.princeton.edu/linux/findbugs.xml" && \
wget -O $ALGS4_HOME/checkstyle-algs4.xml "https://algs4.cs.princeton.edu/linux/checkstyle-algs4.xml" && \
wget -O $ALGS4_HOME/checkstyle-suppressions.xml "https://algs4.cs.princeton.edu/linux/checkstyle-suppressions.xml" && \
wget -O $ALGS4_HOME/pmd.xml "https://algs4.cs.princeton.edu/linux/pmd.xml" && \
unzip $ALGS4_HOME/checkstyle.zip -d $ALGS4_HOME && \
unzip $ALGS4_HOME/findbugs.zip -d $ALGS4_HOME && \
unzip $ALGS4_HOME/pmd.zip -d $ALGS4_HOME && \
rm $ALGS4_HOME/checkstyle.zip && \
rm $ALGS4_HOME/findbugs.zip && \
rm $ALGS4_HOME/pmd.zip
# Add wrapper scripts for Findbugs, PMD, and Checkstyle
RUN wget -O /usr/local/bin/findbugs-algs4 "https://algs4.cs.princeton.edu/linux/findbugs-algs4" && \
wget -O /usr/local/bin/checkstyle-algs4 "https://algs4.cs.princeton.edu/linux/checkstyle-algs4" && \
wget -O /usr/local/bin/pmd-algs4 "https://algs4.cs.princeton.edu/linux/pmd-algs4" && \
chmod +x /usr/local/bin/findbugs-algs4 && \
chmod +x /usr/local/bin/checkstyle-algs4 && \
chmod +x /usr/local/bin/pmd-algs4
# Setup wrapper scripts for javac-algs4 and java-algs4 commands
RUN echo 'javac -cp .:$ALGS4_HOME/algs4.jar:$ALGS4_HOME/stdlib.jar "$@"' > /usr/local/bin/javac-algs4 && \
chmod +x /usr/local/bin/javac-algs4
RUN echo 'java -cp .:$ALGS4_HOME/algs4.jar:$ALGS4_HOME/stdlib.jar "$@"' > /usr/local/bin/java-algs4 && \
chmod +x /usr/local/bin/java-algs4
# Cleanup
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Set the working directory for when the container launches
WORKDIR /workspace
# Entry command, start bash
CMD ["bash"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment