Docker实践

本文是由ICLR2019 Neural persistence论文中的项目出发接触 docker. 参考了菜鸟教程.

Docker 架构

Docker架构模式为客户端-服务器. 通过Docker镜像创建Docker容器.

ubuntu18.04 安装 docker

直接从软件源中安装

1
sudo apt install docker-ce

添加当前用户到 docker 用户组,可以不用 sudo 运行 docker

1
2
sudo groupadd docker
sudo usermod -aG docker $USER

Docker 使用

Hello World

docker run命令在容器内运行程序

1
docker run ubuntu /bin/echo "Hello world"

其中ubuntu为指定要运行的镜像.

交互式容器

1
docker run -it ubuntu /bin/bash

-i允许交互, -t新容器内制定终端

后台容器

1
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"

通过docker ps查看进程

docker logs查看输出

docker stop停止进程

Docker 镜像使用

镜像列表

docker images列出本地镜像

获取镜像

docker pull ubuntu

查找镜像

docker search ubuntu

Dockerfile 构建镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
FROM tensorflow/tensorflow:1.6.0-py3
RUN mkdir /Software
WORKDIR /Software

# Install Aleph dependencies
RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y cmake git python3-dev
RUN pip3 install pytest
RUN git clone https://github.com/pybind/pybind11.git pybind11
RUN mkdir pybind11/build
WORKDIR pybind11/build
RUN cmake ..
#RUN make -j 4
RUN make -j 4 install

# Install Aleph python module
RUN apt-get install -y libboost-dev libboost-regex-dev
RUN git clone https://github.com/Submanifold/Aleph.git Aleph
RUN mkdir Aleph/build
WORKDIR Aleph/build
RUN git checkout 8c88506
RUN cmake ..
RUN make aleph
RUN pip3 install bindings/python/aleph

# Install python dependencies
RUN pip3 install sacred matplotlib seaborn pandas

RUN mkdir /Neuropersistence
WORKDIR /Neuropersistence
ADD . /Neuropersistence

From指定镜像源, RUN让docker在镜像内执行命令, WORKDIR指定目录. ADD将主机的文件拷贝到镜像中.

1
docker build -t neuropersistence .

-t指定目标镜像名, .为Dockerfile文件所在目录.

1
2
docker run -v $PWD/results/:/Neuropersistence/results/ neuropersistence python3 -u run_experiments.py
docker run -v $PWD/results/:/Neuropersistence/results/ neuropersistence python3 combine_runs.py results/runs/* --output results/combined_runs.csv

-v使docker容器启动时挂载主机的目录, 用:分隔.

1
docker run -v $PWD/results/:/Neuropersistence/results/ neuropersistence python3 create_plots.py results/combined_runs.csv results/combined_runs.pdf
分享到