Docker使用笔记

Installation

Docker is available in two editions:

  • Community Edition(CE)
  • Enterprise Edition(EE)

Before installation, remove old version :

1
sudo apt-get remove docker docker-engine docker.io

Then insall a edition follow the official document. If you want to use NVIDIA GPU, you should also install nvidia-docker

Build Docker Image

There two ways to build a docker image: writing a dockerfile or operating inside a base docker image.

Writing a docker image

  • A example dockerfile is as follow:

    1
    2
    3
    4
    5
    6
    7
    # base image
    FROM nvida/cuda:8.0-cudnn5-devel-ubuntu16.04

    # operate inside the base image
    RUN apt-get update && apt-get install -y \
    build-essential \
    ...
  • After finish the dockerfile, we can build our own docker image:

    1
    nvidia-docker build -t <IMAGE NAME> -f <DOCKERFILE NAME>
  • If no error occurs, we should see our IMAGE NAME through

    1
    nvidia-docker images
  • After we build the image, we can save the image to a tar file:

    1
    nvidia-docker save <IMAGE NAME or ID> > <TAR FILE NAME>

Operating inside a base docker image

  • Get a base image from docker hub, here we use a ubuntu16.04 with cuda8-cudnn5 image

    1
    nvidia-docker pull nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04
  • Run the base image in a container and do some experiments

    1
    nvidia-docker run -it <BASE IMAGE NAME or ID>
  • After doing some experiments inside the container, we can do commits to save the modifications into a new image

    1
    2
    nvidia-docker ps -a # to show all containers
    nvidia-docker commit <CONTAINER ID> <IMAGE NAME>:<TAG NAME> # if no TAG NAME provided, TAG NAME is set to latest

Run a docker

1
2
3
4
nvidia-docker load -i <TAR FILE NAME> # load a docker image from a tar file
nvidia-docker images # check the loaded image id or name
nvidia-docker run -it -v <HOST DIR>:<DIR INSIDE CONTAINER> <IMAGE NAME or ID> --name <CONTAINER NAME> # -v mount the host dir to container
exit # exit the container

If you want to run the container again:

1
2
3
nvidia-docker ps -a # check the container id
nvidia-docker start <CONTAINER ID or NAME>
nvidia-docker attach <CONAINER ID or NAME> # we are inside the container again

Extra note

  • copy files between host and container

    1
    2
    nvidia-docker cp <FILENAME> <CONTAINER NAME or ID>:/<FILENAME>
    nvidia-docker cp <CONTAINER NAME or ID>:/<FILENAME> <FILENAME>
  • remove container

    1
    nvidia-docer rm <CONTAINER NAME or ID>
  • remove image

    1
    nvidia-docer rmi <IMAGE NAME or ID>