Docker使用

安装

# 方法参见: https://docs.docker.com/engine/install/ubuntu/

1. 删除旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc

2. 安装依赖
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

3. 加入官方的GPG密匙
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4. 加入apt的仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

4.1 如果提示 apt_pkg 没有
sudo apt-get install python3-apt -f
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so
sudo chmod 777 apt_pkg.so
注意,上面改名这招是有效的

4.2 如果提示 add-apt-repository 没有
sudo apt-get install software-properties-common python-software-properties

5. 安装docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

6. 验证
systemctl status docker

换源

1. https://promotion.aliyun.com/ntms/act/kubernetes.html#industry
2. 进入上述网页登入后点镜像搜索,用支付宝扫码注册
3. 点击左边最下面镜像加速器,按要求来

加入GPU支持

# From: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker

# 安装工具链
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) 
   && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - 
   && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker

# 测试
sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi

基本使用

# From: https://github.com/docker/labs/blob/master/beginner/chapters/alpine.md

docker pull alpine # 拉一个精简的Linux系统
docker images # 查看系统中所有的容器

docker run alpine ls -l
# 这个操作经历了这么几个过程
1. docker客户端连接daemon
2. daemon检查当前是否有该镜像,没有就会安装
3. daemon创建容器,运行后面的命令,运行结束则关闭容器
4. daemon把结果输出返回docker客户端
注意: Docker daemon是docker的守护进程,后台运行

docker ps # 查看当前运行中的容器
docker ps -a # 查看当前运行过的容器

docker run -d dockersamples/static-site 
# -d后台运行直到结束,由于这里有服务一直运行因此不会结束

docker stop da0c582936f2
docker rm da0c582936f2
# 使用docker ps查看ID为da0c582936f2后结束并删除

docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site
# -name 设置运行容器进程的名字
# -e 设置环境变量到容器中,这里AUTHOR="Your Name"
# -d 后台运行
# -P 随机设置容器外到容器内的端口映射

docker port static-site
# 查看当前名字的容器的端口映射表,显示为:
80/tcp -> 0.0.0.0:32769   这个是外部访问的地址
443/tcp -> 0.0.0.0:32768

# 使用名字停止容器
docker stop static-site
docker rm static-site
# 或者 docker rm -f static-site

# 创建容器并在后台交互式运行
docker run -itd --name myct -p 3306:3306 -v /xxx/:/home/ mysql
# -itd设置这个容器后台交互式运行, --name设置名字
# -p左边是计算机端口右边是容器内端口 -v设置共享目录 最后mysql是基容器
# 可用 docker ps 查看后台运行的所有容器, docker ps -a 可以看所有容器(包括停止)

# 进入在后台运行的容器
docker exec -it myct /bin/bash

# 创建Dockerfile
FROM mysql # 基于平台镜像基础上来封装
ADD ["容器外文件","容器内目录"] 
# 或者 ADD 容器外文件 容器内目录
# 注意容器外文件需要在当前Dockerfile目录下的相对路径

# 根据Dockerfile创建自己的容器
docker build -t xxx . # .表示当前目录下, -t指定名字
# 运行上面后 docker images 就能看到名字为xxx的镜像

# 输出docker镜像文件以及载入
docker save -o ubuntu.tar ubuntu
docker load --input ubuntu.tar

# 指定版本拉容器
docker pull ubuntu:12.04
docker pull ubuntu # 这个是默认把那本,即latest

# 查看网络上有哪些容器
docker search pytorch

Dockerfile 引例

1. 当前目录创建 app.py

from flask import Flask, render_template
import random

app = Flask(__name__)

# list of cat images
images = [
   "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif"
    ]

@app.route('/')
def index():
    url = random.choice(images)
    return render_template('index.html', url=url)

if __name__ == "__main__":
    app.run(host="0.0.0.0")

2. 当前目录创建 templates/index.html

<html>
  <head>
    <style type="text/css">
      body {
        background: black;
        color: white;
      }
      div.container {
        max- 500px;
        margin: 100px auto;
        border: 20px solid white;
        padding: 10px;
        text-align: center;
      }
      h4 {
        text-transform: uppercase;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h4>Cat Gif of the day</h4>
      <img src="{{url}}" />
      <p><small>Courtesy: <a href="http://www.buzzfeed.com/copyranter/the-best-cat-gif-post-in-the-history-of-cat-gifs">Buzzfeed</a></small></p>
    </div>
  </body>
</html>

3. 当前目录创建 Dockerfile

# 基础镜像
FROM alpine:3.5

# RUN后面跟在容器中运行的命令, 安装pip环境
RUN apk add --update py2-pip

# 把当前目录的 requirements.txt 拷贝到镜像中的/usr/src/app/
# 然后镜像中执行命令
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# 关键文件拷贝到镜像中
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/

# 指定镜像中暴露的端口,因为Flask默认5000端口
EXPOSE 5000

# CMD指定运行命令
CMD ["python", "/usr/src/app/app.py"]

4. 其他引例

ARG PYTORCH="1.5"
ARG CUDA="10.1"
ARG CUDNN="7"

FROM pytorch/pytorch:${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel

ENV TORCH_CUDA_ARCH_LIST="6.0 6.1 7.0+PTX"
ENV TORCH_NVCC_FLAGS="-Xfatbin -compress-all"
ENV CMAKE_PREFIX_PATH="$(dirname $(which conda))/../"

RUN apt-get update && apt-get install -y git ninja-build libglib2.0-0 libsm6 libxrender-dev libxext6 
    && apt-get clean 
    && rm -rf /var/lib/apt/lists/*

# Install MMCV
RUN pip install mmcv-full==latest+torch1.5.0+cu101 -f https://openmmlab.oss-accelerate.aliyuncs.com/mmcv/dist/index.html

# Install MMDetection
RUN conda clean --all
RUN git clone https://github.com/open-mmlab/mmdetection.git /mmdetection
WORKDIR /mmdetection
ENV FORCE_CUDA="1"
RUN pip install -r requirements/build.txt
RUN pip install --no-cache-dir -e .
原文地址:https://www.cnblogs.com/xytpai/p/13477108.html