Skip to content

Instantly share code, notes, and snippets.

@vishnuexe
Last active February 9, 2024 06:23
Show Gist options
  • Save vishnuexe/9b18a2b5d0e1eb281dfa4eb55e851cf8 to your computer and use it in GitHub Desktop.
Save vishnuexe/9b18a2b5d0e1eb281dfa4eb55e851cf8 to your computer and use it in GitHub Desktop.

(# Docker Manual)

Step 1 : Install Docker

#Docker Installation 

$ sudo apt-get update

$ curl https://get.docker.com | sh \
  && sudo systemctl --now enable docker

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
  	&& curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  	&& curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
        	sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
        	sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

Step 2 - Install Nvidia - Docker

#Nvidia-Docker Installation

$ sudo apt-get update

$ sudo apt-get install -y nvidia-docker2

$ sudo systemctl restart docker

Step 3 - Verify Install

#Running sample docker

$ sudo docker run --rm --gpus all nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi

If you see this in your terminal after you have executed the command above, you have successfully installed nvidia-docker

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.60.11    Driver Version: 525.60.11    CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0  On |                  N/A |
| 30%   34C    P8    25W / 320W |   1071MiB / 10240MiB |      1%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
| No running processes found                                                  |
+-----------------------------------------------------------------------------+

Step 4 - Using a docker

Find the required docker tag - preferably from docker hub

#pulling docker image
$ sudo docker pull [docker_image]
#starting the container with gpu access in interactive mode
$ sudo docker run --gpus all -it [docker_image] bash

Example for pytorch docker

$ sudo docker pull pytorch/pytorch
$ sudo docker run --gpus all -it pytorch/pytorch bash

If you see this in your terminal after executing the above command, then you are inside the container and it works same as your normal terminal/command prompt with all the standard commands!!

root@<container-id>:/workspace# 

Now you can copy your codes or download codes from github and execute it as you would in a normal terminal/command prompt. Example:

root@<container-id>:/workspace#git clone <git repo>
root@<container-id>:/workspace#python script.py

To exit from docker just type exit and hit enter.

root@<container-id>:/workspace#exit
user@yoursystem:~$ 

Basic Commands (to be used when outside docker)

To List all dockers images in the local machine

docker images    

Check status of containers

docker ps -a

To retain all files and packages in the docker even after exiting

docker commit <container_id> <new-name>

Delete container

docker rm <container_id>

Delete all container/images/volumes at once

docker rm -f $(docker ps -a -q)             #delete all containers
docker volume rm $(docker volume ls -q)     #delete all volumes
docker rmi $(docker images -a -q)           #delete all images

Copy from Local disk to docker

docker cp <location in local system> container-id:<location in container>

Example for moving foo.txt from local system to docker container

sudo docker cp ~/foo.txt thw8hb78bu:/workspace/

Copy from docker to Local disk

sudo docker cp container-id:<location in container> <location in local system> 

List images

docker images

To initiate Jupyter Notebook from inside a docker

Step 1: Run this in your host system

docker run --gpus all -it -p 8888:8888 image:version

Step 2: Run this inside docker

jupyter notebook --ip 0.0.0.0 --port 8888 --no-browser --allow-root

for jupyter notebook and

jupyter lab --ip 0.0.0.0 --port 8888 --no-browser --allow-root

for jupyter lab

Step 3: Access the notebook through your desktop browser on http://localhost:8888 The notebook will prompt you for a token which was generated when you create the notebook which you will find from step 2.

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