Skip to content

Instantly share code, notes, and snippets.

@gauravkaila
Last active May 20, 2023 05:47
Show Gist options
  • Save gauravkaila/aa8e4c4b53ccb3f3b6769d3b5da1d430 to your computer and use it in GitHub Desktop.
Save gauravkaila/aa8e4c4b53ccb3f3b6769d3b5da1d430 to your computer and use it in GitHub Desktop.
Install Docker and nvidia-docker on Ubuntu-16.04
#!/bin/bash
# add the GPG key for the official Docker repository to the system
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# add the Docker repository to APT sources
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# update the package database with the Docker packages from the newly added repo
sudo apt-get update
# install from the Docker repo instead of the default Ubuntu 16.04 repo
apt-cache policy docker-ce
# install docker
sudo apt-get install -y docker-ce
# check if the docker daemon is running
sudo systemctl status docker
# add user to docker group
sudo usermod -aG docker ${USER}
su - ${USER}
id -nG
# Install nvidia-runtime
# Download nvidia-runtime packages
curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-container-runtime/ubuntu16.04/amd64/nvidia-container-runtime.list | \
sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update
# Install nvidia-runtime
sudo apt-get install nvidia-container-runtime
# Docker Engine setup
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --host=fd:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# Daemon configuration file
sudo tee /etc/docker/daemon.json <<EOF
{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
sudo pkill -SIGHUP dockerd
# Command line
sudo dockerd --add-runtime=nvidia=/usr/bin/nvidia-container-runtime [...]
# If you have nvidia-docker 1.0 installed: we need to remove it and all existing GPU containers
docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo apt-get purge -y nvidia-docker
# Add the package repositories
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu16.04/amd64/nvidia-docker.list | \
sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
# Install nvidia-docker2 and reload the Docker daemon configuration
sudo apt-get install -y nvidia-docker
sudo pkill -SIGHUP dockerd
# Test nvidia-smi with the latest official CUDA image
docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
sudo rm -rf /var/run/docker/
sudo rm -rf /etc/docker/
sudo apt-get purge -y docker-engine docker docker.io docker-ce
@xml94
Copy link

xml94 commented Feb 11, 2020

Thanks for your code.
There is an error. Could me help me?

Job for nvidia-docker.service failed because the control process exited with error code. See "systemctl status nvidia-docker.service" and "journalctl -xe" for details.
nvidia-docker.service couldn't start.
Processing triggers for ureadahead (0.100.0-19.1) ...
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "process_linux.go:449: container init caused "process_linux.go:432: running prestart hook 1 caused \"error running hook: exit status 1, stdout: , stderr: nvidia-container-cli: initialization error: driver error: failed to process request\\n\""": unknown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment