Take ubuntu16.04, cuda10.1 as examples
1. Preparation
- Install the graphics card driver corresponding to the cuda version in ubuntu, refer to the link
- Installed docker and toolkits, refer to the link
- Use docker without sudo, refer to the link
- Test whether docker's --gpus parameter is available, not available refer to the link above to reinstall docker
docker run --help | grep -i gpus
2. Pull the official image
You need to pull a mirror that encapsulates cuda and cudnn from the official as the initial mirror
- Go to docker hub to find the required mirror
Ctrl+ fsearch keywords, such as cuda10.1+cudnn8, search10.1-cudnn8
- Copy the docker pull command and pull the image in the terminal
docker pull nvidia/cuda:10.1-cudnn8-devel-ubuntu16.04
Download may be very slow, speed up docker pull in advance, refer to the link
3. Create a container
- View currently existing mirrors
docker images
nvidia/cuda is the image just pulled, pay attention to the REPOSITORY , TAG , IMAGE ID of the image
- Create a container by mirroring, and mount the local folder, the folder path is written according to the actual situation
docker run --gpus all -it -v /home/kkjsk/projects:/projects 1e33220823f3(容器ID) /bin/bash
Create container by image ID
- or
docker run --gpus all -it -v /home/name/projects:/projects nvidia/cuda:10.1-cudnn8-devel-ubutnu16.04(REPOSITORY:TAG) /bin/bash
Create a container with REPOSITORY and TAG
/ home / name / projects: / projects means that the local / home / name / projects path mounted to the container / projects , the name can own
Successfully created and run the container, and enter ls on the command line, you can see the mounted folder
- Check whether the graphics card driver can be called
nvidia-smi
- Check if cuda is available
nvcc -V
No problem, the container can successfully call the graphics card and use cuda
4. Install python
Python is not installed in the official image, and there is no pip, so you need to install the required python version in the container yourself. Install python by referring to the source code
- Pay attention to downloadPlace the source code package under the mounted folder, Otherwise it cannot be found in the container
- Install python in the container,Remove sudo from the command line
- Some posts will modify the python installation path, it is not recommended to modify it in the docker container
5. Packaging
You can package a basic environment directly, or you can package the dependent packages needed by the code after you install them, so that the packaging is convenient for transplantation
Save the container as a new image and package the image
- Start another terminal to view the existing container
docker ps -a
- Create a new image through the container
docker commit b0a9e5a5a411(容器ID) kkjsk/docker:python(新镜像的DREPOSITORY:TAG)
- View the newly created image
docker images
You can see that the mirror kkjsk/docker:python was successfully created
- Save the image as a local compressed package,Suffix tar
docker save kkjsk/docker:python(REPOSITORY:TAG) cuda10.1_docker.tar(压缩包名)
Then you can see the saved compressed package locally
6. Import the image
When using the packaged image on other computers, you need to import the image through the compressed package first
docker load --input cuda10.1_docker.tar