Skip to content

Commit

Permalink
feat(docker): add Dockerfile for CUDA 12.1.0 on Ubuntu 22.04
Browse files Browse the repository at this point in the history
  • Loading branch information
entelecheia committed May 5, 2024
1 parent 1e132e6 commit 0fe9308
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .copier-docker-config.cuda-12.1.0-ubuntu22.04.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Changes here will be overwritten by Copier; do NOT edit manually
_commit: v0.30.1
_src_path: gh:entelecheia/hyperfast-docker-template
app_dirname: app
app_install_root: /opt
author: Young Joon Lee
build_images_from_dockerfile: true
container_service_name: app
container_workspace_root: /data
copy_scripts_dir: true
cuda_device_id: all
docker_apt_packages: fontconfig fonts-nanum
docker_build_from: nvcr.io/nvidia/cuda:12.1.0-devel-ubuntu22.04
docker_container_uid: 9001
docker_container_username: app
docker_image_variant_name: cuda-12.1.0-ubuntu22.04
docker_image_version_variable_name: IMAGE_VERSION
docker_name_prefix: DEVCON
docker_project_name: devcon
docker_registry: ghcr.io
docker_run_command: zsh
docker_service_name: workspace
docker_timezone: Asia/Seoul
docker_username: entelecheia
email: entelecheia@hotmail.com
enable_nvidia_gpu: true
friendly_name: Development Containers
github_repo_name: dev-containers
github_username: entelecheia
install_dotfiles: false
jupyter_host_port: 18998
jupyter_port: 8585
jupyter_token: __juypter_token_(change_me)__
launch_scripts: launch.sh
main_branch: main
project_description: Development containers seek to find ways to develop, build, test,
and deploy software in a containerized environment without the need to install
any software on your local machine.
project_license: MIT
project_short_description: Development containers, or dev containers, are Docker containers
that are specifically configured to provide a fully featured development environment.
ssh_host_port: 2229
ssh_port: 22
use_deploy_workflows: true
use_jupyter: false
use_semantic_versioning_for_image: true
use_ssh_service: true
use_web_service: true
web_service_host_port: 19090
web_service_port: 8080

10 changes: 10 additions & 0 deletions .docker/.ids/cuda-12.1.0-ubuntu22.04.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DOCKER_PROJECT_ID="cuda-12.1.0-ubuntu22.04"

HOST_WORKSPACE_LOCATION=${WORKSPACE_LOCATION:-"$PWD/workspace/$PROJECT_ID"}
HOST_WORKSPACE_ROOT=${WORKSPACE_ROOT:-"$HOST_WORKSPACE_LOCATION/workspace"}
HOST_SCRIPTS_DIR="$PWD/.docker/scripts"
HOST_SSH_DIR="$HOST_WORKSPACE_LOCATION/.ssh"
HOST_CACHE_DIR="$HOST_WORKSPACE_LOCATION/.cache"
HOST_HF_HOME=${HF_HOME:-"${HOST_WORKSPACE_LOCATION}/.cache/huggingface"}
HOST_GH_CONFIG_DIR="$HOST_WORKSPACE_LOCATION/.config/gh"
HOST_PASSAGE_DIR="$HOST_WORKSPACE_LOCATION/.passage"
6 changes: 6 additions & 0 deletions .docker/.ids/kmu.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ HOST_CACHE_DIR=""
HOST_HF_HOME=""
HOST_GH_CONFIG_DIR=""
HOST_PASSAGE_DIR=""
CONTAINER_CUDA_DEVICE_ID="4,5,6,7"
CONTAINER_WORKSPACE_ROOT="/data"
CONTAINER_USERNAME="kmu"
USER_GID="1111"
USER_UID="1111"
DEVCON_HOST_SSH_PORT="2229"
72 changes: 72 additions & 0 deletions .docker/Dockerfile.cuda-12.1.0-ubuntu22.04
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Sets the base image for subsequent instructions
FROM nvcr.io/nvidia/cuda:12.1.0-devel-ubuntu22.04 AS builder

# Sets labels for the image
LABEL org.opencontainers.image.source="https://github.com/entelecheia/dev-containers"
LABEL org.opencontainers.image.description="Development containers, or dev containers, are Docker containers that are specifically configured to provide a fully featured development environment."
LABEL org.opencontainers.image.licenses="MIT"

# Setting this argument prevents interactive prompts during the build process
ARG DEBIAN_FRONTEND=noninteractive
# Updates the image and installs necessary packages
RUN apt-get update --fix-missing \
&& apt-get install -y curl wget jq sudo gosu git build-essential software-properties-common \
locales locales-all fontconfig fonts-nanum \
tzdata openssh-server \
# Cleans up unnecessary packages to reduce image size
&& apt-get autoremove -y \
&& apt-get clean -y

# Sets Python environment variables
ENV PIP_DEFAULT_TIMEOUT 100
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Setting ARGs and ENVs for the app
ARG ARG_WORKSPACE_ROOT="/workspace"
ENV WORKSPACE_ROOT $ARG_WORKSPACE_ROOT
# Sets up the workspace for the user
RUN mkdir -p $WORKSPACE_ROOT

# Sets the working directory to workspace root
WORKDIR $WORKSPACE_ROOT

# Sets the time zone within the container
ENV TZ="Asia/Seoul"
# Sets up the locale to en_US.UTF-8
RUN localedef -v -c -i en_US -f UTF-8 en_US.UTF-8 || true

# Start SSH server
RUN mkdir -p /var/run/sshd
# RUN echo 'root:password' | chpasswd
# RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# RUN sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
RUN service ssh start

# Setting ARGs and ENVs for user creation and workspace setup
ARG ARG_USERNAME="app"
ARG ARG_USER_UID=9001
ARG ARG_USER_GID=$ARG_USER_UID
ENV USERNAME $ARG_USERNAME
ENV USER_UID $ARG_USER_UID
ENV USER_GID $ARG_USER_GID

# Creates a non-root user with sudo privileges
# check if user exists and if not, create user
RUN if id -u $USERNAME >/dev/null 2>&1; then \
# if the current user's user id is different from the specified user id, change the user id of the current user to the specified user id
if [ "$USER_UID" -ne "$(id -u $USERNAME)" ]; then \
usermod --uid $USER_UID $USERNAME; \
chown --recursive $USER_UID:$USER_UID $WORKSPACE_ROOT; \
chown --recursive $USER_UID:$USER_UID $APP_INSTALL_ROOT; \
fi; \
else \
groupadd --gid $USER_GID $USERNAME && \
adduser --uid $USER_UID --gid $USER_GID --force-badname --disabled-password --gecos "" $USERNAME && \
echo "$USERNAME:$USERNAME" | chpasswd && \
adduser $USERNAME sudo && \
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME && \
chmod 0440 /etc/sudoers.d/$USERNAME; \
fi

51 changes: 51 additions & 0 deletions .docker/docker-compose.cuda-12.1.0-ubuntu22.04.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: "3"

services:
# Defines a service name
workspace:
build:
# Sets the build context to the current directory
context: .
# Specifies the Dockerfile to use for the build
dockerfile: .docker/Dockerfile.cuda-12.1.0-ubuntu22.04
# Specifies build-time variables (ARGs)
args:
ARG_BUILD_FROM: $BUILD_FROM
ARG_USERNAME: $CONTAINER_USERNAME
ARG_USER_UID: $CONTAINER_USER_UID
ARG_USER_GID: $CONTAINER_USER_GID
ARG_WORKSPACE_ROOT: $CONTAINER_WORKSPACE_ROOT
# Sets the image name for the built image
image: $IMAGE_NAME:$IMAGE_TAG
# Sets the hostname of the container
hostname: $CONTAINER_HOSTNAME
tty: true
# command:
# # Specifies the command to be executed when the container is run
# - bash
# set the environment variables
ulimits:
# Sets the stack size and memory lock limits
stack: 67108864
memlock: -1
# Allows the container to use the host's IPC namespace
ipc: $CONTAINER_IPC
ports:
# Maps the container's SSH and Web service ports to the host's ports
- "$HOST_SSH_PORT:$CONTAINER_SSH_PORT"
volumes:
# Maps directories from the host to the container
- "$HOST_WORKSPACE_ROOT:$CONTAINER_WORKSPACE_ROOT"
deploy:
resources:
reservations:
devices:
# Reserves the specified GPU for the container
- driver: nvidia
device_ids: ["${CONTAINER_CUDA_DEVICE_ID}"]
capabilities: [gpu]
networks:
default:
# Sets the name of the default network and makes it external
name: $CONTAINER_NETWORK_NAME
external: true
20 changes: 20 additions & 0 deletions .docker/docker.cuda-12.1.0-ubuntu22.04.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#########################################################
# Configuration parameters for the docker project #
# Change the variables below to your need: #
#########################################################

#######################################################################################
# Please do not make any changes below this line if you don't know what you are doing #
# change the variables above to your need #
#######################################################################################
# docker build: Configuration parameters for building the Docker image
IMAGE_VARIANT=${IMAGE_VARIANT:-"cuda-12.1.0-ubuntu22.04"} # The variant of the Docker image.
IMAGE_TAG="${IMAGE_VERSION}-${IMAGE_VARIANT}" # The tag of the Docker image
IMAGE_NAME="${CONTAINER_REGISTRY}/${DOCKER_USERNAME}/${DOCKER_PROJECT_NAME}" # The full name of the Docker image
BUILD_FROM="nvcr.io/nvidia/cuda:12.1.0-devel-ubuntu22.04" # The base image for the Docker build

# docker run: Configuration parameters for running the Docker container
CONTAINER_LAUNCH_SCRIPT="${CONTAINER_WORKSPACE_ROOT}/scripts/launch.sh" # The name of the launch script
CONTAINER_CUDA_DEVICE_ID=${DEVCON_CUDA_DEVICE_ID:-"all"} # The ID of the CUDA device to use, e.g. all, 0, 1, 2, etc.
CONTAINER_SSH_PORT=${SSH_PORT:-"22"} # The SSH port in the Docker container
HOST_SSH_PORT=${DEVCON_HOST_SSH_PORT:-"2229"} # The SSH port on the host machine to be mapped to the container's SSH port
87 changes: 87 additions & 0 deletions .github/workflows/deploy-cuda-12.1.0-ubuntu22.04-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#
name: deploy-cuda-12.1.0-ubuntu22.04-image

# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
workflow_call:
workflow_dispatch:
push:
branches:
- docker*
paths:
- ".docker/**/*.cuda-12.1.0-ubuntu22.04*"
- ".docker/docker.version"

# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env:
REGISTRY: ghcr.io

# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
jobs:
build-and-push-image:
runs-on: ubuntu-latest
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
#
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Secure disk space for the build
run: bash .github/scripts/free-disk-space.sh

- name: Version from version file
uses: c-py/action-dotenv-to-setenv@v5
with:
env-file: ./.docker/docker.version

- name: Common environment Variables from Dotenv
uses: c-py/action-dotenv-to-setenv@v5
with:
# use branch name as suffix for dotfile
env-file: ./.docker/docker.common.env

- name: Environment Variables from Dotenv
uses: c-py/action-dotenv-to-setenv@v5
with:
# use branch name as suffix for dotfile
env-file: ./.docker/docker.cuda-12.1.0-ubuntu22.04.env

# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to the Container registry
uses: docker/login-action@v3.1.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5.5.1
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ env.IMAGE_VERSION }}-${{ env.IMAGE_VARIANT }}
# set latest tag for docker branch
type=raw,value=latest-${{ env.IMAGE_VARIANT }}
type=raw,value=latest
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
- name: Build and push Docker image
uses: docker/build-push-action@v5.3.0
with:
context: .
file: ./.docker/Dockerfile.cuda-12.1.0-ubuntu22.04
build-args: |
ARG_USERNAME=${{ env.CONTAINER_USERNAME }}
ARG_USER_UID=${{ env.CONTAINER_USER_UID }}
ARG_USER_GID=${{ env.CONTAINER_USER_GID }}
ARG_WORKSPACE_ROOT=${{ env.CONTAINER_WORKSPACE_ROOT }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

0 comments on commit 0fe9308

Please sign in to comment.