Featured image of post Create a Docker container with external SSH access and Nvidia GPU support

Create a Docker container with external SSH access and Nvidia GPU support

📌 Introduction

Set up a Docker container and allow external SSH connections to use and enable Nvidia GPU.

建立 Docker 的容器並允許外部 SSH 連線使用及啟用 Nvidia GPU。

💻 Code

Create this Dockerfile.

# user nvidia image (https://hub.docker.com/r/nvidia/cuda/tags)
FROM nvidia/cuda:12.3.1-base-ubuntu20.04

# Istall SSH and sudo
RUN apt-get update && apt-get install -y openssh-server sudo

# Set up the directories for SSH service
RUN mkdir /var/run/sshd

# Create a new user 'user', set a password, and grant sudo privileges.
RUN useradd -m user && echo "user:password" | chpasswd && adduser user sudo

# Configure SSH to allow password login
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config

# Open port 22 for SSH
EXPOSE 22

# Start the SSH service
CMD ["/usr/sbin/sshd", "-D"]

Use this script build and run docker container.

$ docker build -t user-image-nvidia .
$ docker run -it --gpus all --name user-container-nvidia -p 2222:22 -d user-image-nvidia

🗒️ Notes

--hostname foo can chage VM’s hostname.