tips for docker

2017年07月14日

  1. docker cp host_file container:file_path 在容器和主机之间复制文件,可以用来临时替换entrypoint实现进入一个无限重启的容器以调试

  2. docker exec -it container /bin/bash 在主机上执行另一条命令

setup nvidia-cuda-jupyter

#!/bin/bash

## setup the docker nvidia runtime

wget https://raw.githubusercontent.com/NVIDIA/libnvidia-container/gh-pages/centos7/x86_64/libnvidia-container-tools-1.0.0-0.1.rc.2.x86_64.rpm
wget https://raw.githubusercontent.com/NVIDIA/libnvidia-container/gh-pages/centos7/x86_64/libnvidia-container1-1.0.0-0.1.rc.2.x86_64.rpm
wget https://raw.githubusercontent.com/NVIDIA/nvidia-docker/gh-pages/centos7/x86_64/nvidia-docker2-2.0.3-1.docker17.03.2.ce.noarch.rpm
wget https://raw.githubusercontent.com/NVIDIA/nvidia-container-runtime/gh-pages/centos7/x86_64/nvidia-container-runtime-2.0.0-1.docker17.03.2.x86_64.rpm
wget https://raw.githubusercontent.com/NVIDIA/nvidia-container-runtime/gh-pages/centos7/x86_64/nvidia-container-runtime-hook-1.2.1-1.x86_64.rpm

sudo rpm -ivh libnvidia-container1-1.0.0-0.1.rc.2.x86_64.rpm 
sudo rpm -ivh libnvidia-container-tools-1.0.0-0.1.rc.2.x86_64.rpm 
sudo rpm -ivh nvidia-container-runtime-hook-1.2.1-1.x86_64.rpm 
sudo rpm -ivh nvidia-container-runtime-2.0.0-1.docker17.03.2.x86_64.rpm 
sudo rpm -ivh nvidia-docker2-2.0.3-1.docker17.03.2.ce.noarch.rpm 

## test cuda within docker
cat nvidia.cuda.docker.image | docker load

sudo systemctl restart docker
docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
                                 
## import our own jupyter docker
cat jupyter.docker.image | docker load

## export some conda related files and create a new docker image 
docker run -t --rm jupyter/tensorflow-notebook tar -cf -  /opt/conda /usr/local/bin > jupyter.tar
wget http://security.ubuntu.com/ubuntu/pool/main/s/sudo/sudo_1.8.16-0ubuntu1.4_amd64.deb

cat > Dockerfile << 'EOF'                                 
FROM nvidia/cuda:9.0-devel-ubuntu16.04

ADD jupyter.tar /

ENV CONDA_DIR=/opt/conda SHELL=/bin/bash NB_USER=jovyan NB_UID=1000 NB_GID=100 PATH=/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/jovyan
RUN useradd -m -s /bin/bash -N -u $NB_UID $NB_USER \
   &&  mkdir -p $CONDA_DIR \
   &&  chown $NB_USER:$NB_GID $CONDA_DIR \
   &&  chmod g+w /etc/passwd /etc/group \
   &&  fix-permissions $HOME \
   &&  fix-permissions $CONDA_DIR

COPY sudo_1.8.16-0ubuntu1.4_amd64.deb /
RUN dpkg -i /sudo_1.8.16-0ubuntu1.4_amd64.deb

EXPOSE 8888/tcp
WORKDIR /home/jovyan

ENTRYPOINT ["tini","--"]
CMD ["start-notebook.sh", "--ip", "0.0.0.0"]
EOF

docker build -t jupyter-cuda  .

## run the server
docker run -d --restart=always --runtime=nvidia -p 8888:8888 -p 8097:8097 -p 8080:8080 -p 8822:22 -v /DATA/gcc.home/jupyter_home:/home/jovyan:rshared -v data_science_tmp:/tmp -e NB_UID=555 -e NB_GID=555 -e GRANT_SUDO=yes --user root  --name jupyter jupyter-cuda start-notebook.sh --ip=0.0.0.0