Skip to content

Commit

Permalink
Merge pull request #203 from cs50/docker
Browse files Browse the repository at this point in the history
Adds support for Docker outside of Docker
  • Loading branch information
dmalan authored Jan 29, 2024
2 parents 1a40872 + 404957e commit 9c5d8a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
25 changes: 22 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,26 @@ RUN apt update && \
# Install CS50 library
RUN curl https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | bash && \
apt update && \
apt install --yes libcs50
apt install --yes \
libcs50
# Install Docker CLI
# https://docs.docker.com/engine/install/ubuntu/
# https://docs.docker.com/engine/install/linux-postinstall/
RUN apt update && \
apt install --no-install-recommends --no-install-suggests --yes \
ca-certificates \
curl \
socat && \
install -d /etc/apt/keyrings -m 0755 && \
curl --fail --location --show-error --silent https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && \
chmod a+r /etc/apt/keyrings/docker.asc && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
apt update && \
sudo apt install --no-install-recommends --no-install-suggests --yes \
docker-ce-cli && \
groupadd docker
# Install Python packages
Expand Down Expand Up @@ -260,7 +279,8 @@ RUN useradd --home-dir /home/ubuntu --shell /bin/bash ubuntu && \
echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
echo "Defaults umask_override" >> /etc/sudoers && \
echo "Defaults umask=0022" >> /etc/sudoers && \
sed --expression="s/^Defaults\tsecure_path=.*/Defaults\t!secure_path/" --in-place /etc/sudoers
sed --expression="s/^Defaults\tsecure_path=.*/Defaults\t!secure_path/" --in-place /etc/sudoers && \
usermod --append --groups docker ubuntu
# Version the image (and any descendants)
Expand All @@ -271,7 +291,6 @@ ONBUILD ARG VCS_REF
ONBUILD RUN echo "$VCS_REF" >> /etc/issue
ONBUILD USER ubuntu
# Set user
USER ubuntu
WORKDIR /home/ubuntu
Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ default: run

build:
docker build --build-arg VCS_REF="$(shell git rev-parse HEAD)" --tag cs50/cli .
$(MAKE) squash

depends:
pip3 install docker-squash

rebuild:
docker build --no-cache --tag cs50/cli .
$(MAKE) squash

run:
docker run --env LANG="$(LANG)" --interactive --publish-all --rm --security-opt seccomp=unconfined --tty --volume "$(PWD)":/home/ubuntu cs50/cli bash --login || true
docker run --env LANG="$(LANG)" --interactive --publish-all --rm --security-opt seccomp=unconfined --tty --volume "$(PWD)":/home/ubuntu --volume /var/run/docker.sock:/var/run/docker-host.sock cs50/cli bash --login || true

squash: depends
docker images cs50/cli
docker-squash --tag cs50/cli cs50/cli
docker images cs50/cli
39 changes: 39 additions & 0 deletions etc/init.d/docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# Adapted from https://github.com/devcontainers/features/blob/main/src/docker-outside-of-docker/install.sh.
# If docker-ce is installed, shadows its own.

SOURCE_SOCKET=/var/run/docker-host.sock
TARGET_SOCKET=/var/run/docker.sock
SOCAT_PID=/tmp/docker.pid

case "$1" in
start)
echo "Starting Docker"
socat UNIX-LISTEN:${TARGET_SOCKET},fork,group=docker,mode=660 UNIX-CONNECT:${SOURCE_SOCKET} & echo "$!" > "$SOCAT_PID"
;;
stop)
echo "Stopping Docker"
kill -9 $(cat "$SOCAT_PID" 2> /dev/null) 2> /dev/null
rm --force "$SOCAT_PID" "$TARGET_SOCKET"
;;
restart)
$0 stop
$0 start
;;
status)
if [[ ! -f "${SOCAT_PID}" ]] || ! ps -p $(cat "$SOCAT_PID" 2> /dev/null) &> /dev/null; then
echo "Docker is not running"
exit 1
else
echo "Docker is running"
exit 0
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac

exit 0
2 changes: 1 addition & 1 deletion etc/profile.d/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if [ "$(whoami)" != "root" ]; then
export PROMPT_COMMAND='history -a' # Store Bash History Immediately

# Java
export JAVA_HOME="/opt/jdk-21.0.1"
export JAVA_HOME="/opt/jdk"

# Make
export CC="clang"
Expand Down

0 comments on commit 9c5d8a5

Please sign in to comment.