# Run jobs in a Docker

{% hint style="warning" %}
注意：中国大陆地区的互联网可能无法访问 hub.docker.com ，也可能无法使用 `docker pull`等自动下载镜像的功能。有很多解决方法，大家可以自己找找访问这些页面的方法，也可以网上试试搜索“国内如何访问下载Docker镜像”。
{% endhint %}

## 0) Brief introduction

* 如果我们需要在安装着windows或者mac操作系统的机器上获得linux的运行环境，一个常见的做法是安装linux的虚拟机。Docker相当于是一种轻量级的虚拟机。
* 除了可以当作虚拟机来使用，Docker可以对软件依赖等进行很好的封装，可以极大的提高运行环境的可移植性。所以如果您的机器原本就安装着linux的发行版，也可以通过使用我们提供的docker \_**image**\_来节约一些安装软件依赖的时间。
* 镜像(***image***)和容器(***container***)是docker的两个比较重要的概念。***image**可以理解成是一个用于创建虚拟机的模板，而**container**相当于是一个根据**image**创建出的虚拟机。这二者的关系有点类似于面向对象编程中类(class)和对象(object)的关系。*[*Docker Hub*](https://hub.docker.com/)*上维护了很多别人配置好的**image*** (如 [Ubuntu Image](https://hub.docker.com/_/ubuntu))，我们可以直接拿来使用。大家如果在自己的机器上安装好了docker软件，下面是一个快捷的获取和使用Linux（Ubuntu版）虚拟机的方法（注意保持联网）:

```bash
# A Quick Start in Terminal after you installed Docker software in your computer:  
docker pull ubuntu         # download an image of ubuntu, you can also specify a version tag (ubuntu is equivalent to ubuntu:latest)
docker run -i -t ubuntu    # run (create) a container of ubuntu, same as docker run -i -t ubuntu /bin/bash
exit                       # exit your container
```

* 我们也可以手工加载我们自己配置的docker \_**image** ，\_如下图所示，如果我们自己有一个 ***image*** （可以打包成.tar.gz结尾的一个压缩包，如 bioinfo\_tsinghua.docker.tar.gz），大家在自己的机器上安装了docker软件后，就可以加载这个 ***image***，从中创建出docker ***container***，利用docker \_**container** \_ 中的运行环境进行后续的学习。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvH49myTGA7P046AY%2Fdocker_usage.png?generation=1540298190358830\&alt=media)

* 如果大家希望进一步了解docker的优势，以及它和传统虚拟机的区别，可进一步参考[为什么使用Docker](https://yeasy.gitbooks.io/docker_practice/introduction/why.html)。

## 1) Install and Use Docker in Mac

### 1.0) Install Docker

docker程序在Mac上的安装很简单，可以通过Apple Store安装，或者直接从<https://docs.docker.com/desktop/install/mac-install/>下载 Docker安装程序后进行安装。

### 1.1) Load an *image*

我们可以如上面的简介里面所描述的，很快捷地从Docker Hub中获取一些别人配置好的 \_**image。**\_除此之外，我们也可以手工加载。

下面介绍如何手工加载我们为本课程配置的 ***image***。

* 从 [Teaching Dockers ](https://book.ncrnalab.org/teaching/appendix/appendix-iv.-teaching#teaching-docker)里面下载docker \_**image**\_文件 (如 bioinfo\_PartI-PartII-PartIII1-3.tar.gz) 到本地
* 双击运行docker程序，然后在“/Applications⁩/⁨Utilities” 中打开 "Terminal" 软件
* 以下命令默认都是在“Terminal”中运行。“#”以及"#"之后的内容是注释语句，实际操作中不用输入
* 假设我们这里将打包好的docker \_**image**\_下载到了桌面上`~/Desktop/bioinfo_PartI-PartII-PartIII1-3.tar.gz`。如果下载到了其他位置，请换成相应的路径

```bash
#load the image
docker load -i ~/Desktop/bioinfo_PartI-PartII-PartIII1-3.tar.gz  
```

* 经过这一步操作后，输入`docker images`将显示导入的\_**image**\_信息:

```
REPOSITORY                   TAG       IMAGE ID       CREATED         SIZE
xfliu1995/bioinfo_tsinghua   2         cc2dc08c705b   11 months ago   2.84GB
```

### 1.2) Run a *container* from an *image*

`docker run`用于从 image 创建 (run) 一个 ***container***。

```bash
mkdir ~/Desktop/bioinfo_tsinghua_share
docker run --name=bioinfo_tsinghua -dt  -h bioinfo_docker --restart unless-stopped -v ~/Desktop/bioinfo_tsinghua_share:/home/test/share xfliu1995/bioinfo_tsinghua:2
```

这里我们新建了一个名为`bioinfo_tsinghua`的\_**container**\_，重要参数说明如下:

* `-dt --restart unless-stopped`: 设置该\_**container**\_能一直在后台保持运行状态
* `-v ~/Desktop/bioinfo_tsinghua_share:/home/test/share`: 将宿主机的目录`~/Desktop/bioinfo_tsinghua_share`作为数据卷挂载到\_**container***的`/home/test/share`目录下。docker通过数据卷(volume)实现宿主机和***container***之间的文件同步。我们在***container**\_中修改`/home/test/share`目录下的内容，就相当于修改宿主机`~/Desktop/bioinfo_tsinghua_share`目录下的内容。

到此为止，我们已经成功地安装了Docker程序，成功地载入一个\_**image***，创建***container**\_，并使其在后台运行。

### 1.3) Use and exit the *container*

进入一个\_**container**\_ (例如上节中开启的\_**container：**\_bioinfo\_tsinghua)，只需执行如下3个步骤:

* a) 运行\_**container**\_: 在Terminal中输入 `docker exec -it bioinfo_tsinghua bash` 。
  * `docker exec`用于在一个正在运行的\_**container**\_中执行命令
  * `-it`: 交互(interactive, -i)式的运行\_**container**\_中的bash命令，并在terminal中显示输入输出(-t)
* b) 使用\_**container**\_提供的运行环境，如下图

![docker](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvH4KSa1h8QXfFpu7%2Fbash-in-container.gif?generation=1540298190271360\&alt=media)

* c) 退出\_**container**\_：`exit`

> 注：每次使用\_**container**\_时要检查docker程序是否运行了，否则请双击docker程序图标运行docker程序

## 2) Install and Use Docker in Windows

### 2.0) Install Docker

### 2.0.a) Update your Windows

docker对windows的系统版本有一定要求，请参考<https://docs.docker.com/desktop/install/windows-install/>。如果版本过低，建议更新操作系统。可从清华info的软件资源获取。

### 2.0.b) Download and install Docker

从 [官网](https://store.docker.com/editions/community/docker-ce-desktop-windows) 下载 Docker安装包，并进行安装。

### 2.0.c) Setup Windows Subsystem for Linux (WSL)

* docker的运行需要依赖WSL2，因此需要启用WSL。在开始菜单搜索 PowerShall, 并启动。 ![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvFWab3QcuwxaRbUp%2Fwin_docker6.png?generation=1540298182408626\&alt=media)
* 运行如下命令并重启电脑:

```bash
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
```

* 如果您的机器上安装的是WSL1，则需要进行升级，否则可能出现报错。可以按照下面的步骤进行升级。

(1) enable the Virtual Machine Platform optional feature

```bash
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```

(2) Download [WSL2 Linux kernel update package](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi) for x64 machines

(3) Set WSL 2 as your default version

```bash
wsl --set-default-version 2
```

更多升级WSL2的资料，可参考<https://docs.microsoft.com/zh-cn/windows/wsl/install-win10#step-4---download-the-linux-kernel-update-package>

### 2.1) Restart

安装完成后，选择`Enable & Restart`以启用Hyper-V

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvFWZJoirSYFo8Nyx%2Fwin_docker5.png?generation=1540298184364783\&alt=media)

### 2.2) Check the installation

在power shell中输入以下命令，检查 Docker 程序是否成功安装。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvFWcNwngohFv_5aJ%2Fwin_docker7.png?generation=1540298182524532\&alt=media)

### 2.3) Load a Docker *Image*

我们可以如上面的简介里面所描述的，很快捷地从Docker Hub中获取一些别人配置好的 \_**image。**\_除此之外，我们也可以手工加载。

下面介绍如何手工加载我们为本课程配置的 ***image***。

* 从 [Teaching Dockers ](https://book.ncrnalab.org/teaching/appendix/appendix-iv.-teaching#teaching-docker)里面下载docker ***image**文件\*\*：\*\** bioinfo\_PartI-PartII-PartIII1-3.tar.gz到本地，并且通过在 Power shell 中输入以下命令导入将\_**image**\_导入到 Docker 中。
* **需要将路径`C:\Users\xxx\Desktop\bioinfo_PartI-PartII-PartIII1-3.tar.gz`换成打包的docker \_image**\_**的实际路径**，建议使用绝对路径。
* <mark style="color:red;">注意windows系统的路径中使用的是反斜杠</mark>。

```bash
# 假设我们把image下载到了桌面上
docker load -i C:\Users\xxx\Desktop\bioinfo_PartI-PartII-PartIII1-3.tar.gz
```

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvFWeUpBZ_96WdNA5%2Fwin_docker8.png?generation=1540298183017694\&alt=media)

### 2.4) Run a *container* from an *image*

* 在宿主机上新建一个文件夹用于和docker ***container*** 实现文件共享。这里假设我们在桌面上新建了一个名为bioinfo\_tsinghua\_share的文件夹
* 然后，用docker run新建一个名为 `bioinfo_tsinghua` 的 ***container***。
* <mark style="color:red;">**请把C:\Users\xxx\Desktop\bioinfo\_tsinghua\_share换成你刚刚新建的共享目录在你的宿主机上的绝对路径**</mark>

```bash
docker run --name=bioinfo_tsinghua -dt -h bioinfo_docker --restart unless-stopped -v C:\Users\xxx\Desktop\bioinfo_tsinghua_share:/home/test/share xfliu1995/bioinfo_tsinghua:2
```

* 这里的参数含义和在mac上基本一致:
  * `-dt --restart unless-stopped`: 设置该\_**container**\_能一直在后台保持运行状态
  * `-v C:\Users\xxx\Desktop\bioinfo_tsinghua_share:/home/test/share`: 将宿主机的目录`C:\Users\xxx\Desktop\bioinfo_tsinghua_share`作为数据卷挂载到\_**container***的`/home/test/share`目录。我们在***container**\_中修改`/home/test/share`目录下的内容，就相当于修改宿主机`~/Desktop/bioinfo_tsinghua_share`目录下的内容。
* 在 Windows 10 Pro 上，Docker中的`/home/test/share`目录归root所有，所以要将其改为归test用户所有:

```bash
#用root的身份(-u root)运行已在运行的container: bioinfo_tsinghua中的chown命令,把/home/test/share的所有权给test用户
docker exec -u root bioinfo_tsinghua chown -R test:test /home/test/share
```

执行这一步时，系统会弹出以下提示，我们选择 `Share it`

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LPVv7obRlTivTDgBNhr%2F-LPVvFWiwkWBF70Zvx2D%2Fwin_docker10.png?generation=1540298182497732\&alt=media)

### 2.6) Run and exit the *container*

如果\_**container***创建成功，之后每次只需要启动Docker程序，然后在 Power shell 中输入以下命令即可进入***container**\_：

```bash
docker exec -it bioinfo_tsinghua bash
```

* `docker exec`用于在一个正在运行的\_**container**\_中执行命令
* `-it`: 交互(interactive, -i)式的运行\_**container**\_中的bash命令，并在terminal中显示输入输出(-t)

> 注：如果power shell在这一步出现了卡死的情况，可以尝试使用windows的其他终端，如cmd等。

之后即可运行\_**container**\_中提供的各种Linux命令；

输入 `exit` 即可回到 Power shell。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LR4wNoSElny2w0peW3s%2F-LPVvFWkHuQzuCnrw73k%2Fwin_docker11.png?generation=1541992981660551\&alt=media)

> 注：每次使用\_**container**\_时要检查docker程序是否运行了，否则请双击docker程序图标运行docker程序

## 3) How to maintain a *container*

### 3.1) Check if Docker is installed

在不同操作系统中，均可在相应的终端(terminal或power shell)中运行以下命令，检查 Docker 是否正常安装：

```bash
docker info
```

### 3.2) Check how many *containers* are running

```bash
docker ps #查看当前正在运行的容器container
docker ps -a #查看所有容器container
docker images #查看所有镜像image
```

### 3.3) Remove a *container*

如果你误删的某些软件依赖，以至需要的程序无法正常使用，可以删除该\_**container**\_:

```bash
docker rm -f bioinfo_tsinghua
```

> **Tips**：之前提到过 `bioinfo_tsinghua` 一直保持运行状态，所以我们这里用 `-f` 来强制（force）删除。

再用`docker run`新建一个干净的\_**container**\_。

### 3.4) Clean files

* 打包的\_**image**\_加载后就不再需要，可以直接删除
* 如果需要删除docker已经加载的\_**image**\_，可以使用以下命令:

```bash
docker rmi bioinfo_tsinghua # 删除image，慎重使用
```

## 4) Advanced Settings

打开docker的控制菜单，通过 Preferences 选择卡来进行参数修改。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LspENp3amniTg9coCcc%2F-LspEP7MvIqKkzxzAfQd%2Fa0-perfermance.png?generation=1572851273301738\&alt=media)

建议 CPUs 设置为4， 内存为7G， Swap为2G。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LspENp3amniTg9coCcc%2F-LspEP7OUrN_zYaEvN5C%2Fa2-cpu.png?generation=1572851265981981\&alt=media)

建议存储空间为90G。

![](https://4115668567-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LPVsf5VZbQ7h14X29qW%2F-LspENp3amniTg9coCcc%2F-LspEP7QH2AXCVwf853L%2Fa1-disk.png?generation=1572851265452234\&alt=media)

## **5) References**

> 这里推荐阅读两篇Docker安装教程，分别是[《macOS 安装 Docker》](https://yeasy.gitbooks.io/docker_practice/install/mac.html)和[《Windows 10 PC 安装 Docker CE》](https://yeasy.gitbooks.io/docker_practice/install/windows.html)，来自于[《Docker——从入门到实践》](https://yeasy.gitbook.io/docker_practice/)书，这本书对Docker进行了深入浅出的讲解，对于初学者理解和学习docker技术有所帮助。另外，网上也有很多docker教程，有深入学习兴趣的同学可以自行了解。
