JyLie

vuePress-theme-reco JyLie    2017 - 2023
JyLie

Choose mode

  • dark
  • auto
  • light
主页
分类
  • API
  • HTML
  • css
  • vue
  • Linux
  • Docker
  • Webpack
  • WebGL
  • PixiJS
  • Github
  • BOM
  • XML
  • bug
  • ie
  • uniapp
  • IE
  • mysql
  • font
  • bom
  • canvas
  • video
  • html
  • JavaScript
  • js
  • 运算符
  • RegExp
  • 编码
  • MiniApp
  • nginx
  • Tool
  • node.js
  • cat
  • nodejs
  • protocol
  • URL
  • FLOW
  • DNS
  • Protocol
  • python
  • 安全
  • linux
  • shell
  • IDE
  • Packer
  • ViteJS
  • git
  • vendor
  • WebApp
  • WebView
  • Window API
  • webview
  • 规范
标签
时光轴
GitHub
author-avatar

JyLie

74

Article

79

Tag

主页
分类
  • API
  • HTML
  • css
  • vue
  • Linux
  • Docker
  • Webpack
  • WebGL
  • PixiJS
  • Github
  • BOM
  • XML
  • bug
  • ie
  • uniapp
  • IE
  • mysql
  • font
  • bom
  • canvas
  • video
  • html
  • JavaScript
  • js
  • 运算符
  • RegExp
  • 编码
  • MiniApp
  • nginx
  • Tool
  • node.js
  • cat
  • nodejs
  • protocol
  • URL
  • FLOW
  • DNS
  • Protocol
  • python
  • 安全
  • linux
  • shell
  • IDE
  • Packer
  • ViteJS
  • git
  • vendor
  • WebApp
  • WebView
  • Window API
  • webview
  • 规范
标签
时光轴
GitHub
  • Docker 使用实践

    • Window Terminal
      • 国内镜像源
        • 设置 Window 子系统 镜像源
          • 设置 Window docker 软件 镜像源
            • docker run
              • 参数说明

          Docker 使用实践

          vuePress-theme-reco JyLie    2017 - 2023

          Docker 使用实践


          JyLie 2020-03-26 docker

          # Install

          # Window Terminal

          在安装 Docker 前先确认下 Windows 需符合一下条件:

          • 支持 Hyper-V 的 Windows 版本,Hyper-V 目前仅在 Windows 10 之后的版本支持
          • BIOS 里需要启用 Virtualization(虚拟化)

          在 Docker 官网 安装 Docker for Windows

          # 镜像源

          # 国内镜像源

          • 默认镜像:https://hub.docker.com/

          由于国内网络问题,使用默认镜像存在加载缓慢的问题。为了更丝滑的使用 docker,因此需要在下载好 docker 后将其镜像源设置为国内镜像源。

          常用国内镜像源有:

          • Docker 中国区官方镜像 https://registry.docker-cn.com
          • 中国科技大学 https://docker.mirrors.ustc.edu.cn
          • 网易 http://hub-mirror.c.163.com
          • ustc https://docker.mirrors.ustc.edu.cn

          # 设置 Window 子系统 镜像源

          添加 https://registry.docker-cn.com 到 registry-mirrors 数组

          • vim 编辑文件
          [root@localhost ~]# vim /etc/docker/daemon.json
          {
            "registry-mirrors": ["https://registry.docker-cn.com"]
          }
          [root@localhost ~]# systemctl restart docker.service
          
          1
          2
          3
          4
          5

          # 设置 Window docker 软件 镜像源

          使用 vim 进入镜像源配置路径:/etc/apt/sources.list

          本案例使用 163 的镜像源:

          deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
          deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
          deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
          deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
          deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
          deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
          deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
          deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
          deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
          deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
          
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10

          接使用 vim 打开 sources.list ,使用编辑模式 E 进去 dd 又 p,然后:wq 保存退出。

          # 编辑镜像源配置文件
          vim /etc/apt/sources.list
          
          # 更新完镜像源后需更新软件
          apt update
          
          1
          2
          3
          4
          5

          # Docker 常用命令

          当前 Docker 运行基于版本 v20.x

          # 查看docker版本,Bash输出:Docker version 20.10.17, build 100c701
          docker --version
          
          # 查看docker本地镜像,Bash输出如下:
          # REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
          # ubuntu       18.04     71eaf13299f4   8 days ago   63.1MB
          docker images
          
          # 下载docker镜像
          # 如:docker pull ubuntu:18.04
          docker pull [image_name]:[image_version]
          
          # 运行docker镜像,如运行ubuntu的bash窗口
          docker run -it ubuntu:18.04 bash
          
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14

          # 模拟创建-打包-提交新镜像

          
          # 接着下载常用软件
          apt install iputils-ping wget net-tools vim
          
          # 安装python
          apt install python3.6
          ln -s /usr/bin/python3.6 /usr/bin/python
          apt install python3-pip
          
          # # 假若至此是某项目需要的配置,先查看刚配置的容器信息,Bash输出如下:
          # CONTAINER ID   IMAGE          COMMAND   CREATED       STATUS       PORTS     NAMES
          # bac6226f51a3   ubuntu:18.04   "bash"    4 hours ago   Up 4 hours             xenodochial_lalande
          docker ps -a
          
          # 接着打包镜像,Bash输出打包镜像的sha256如下:
          # sha256:06811dd1053f30caefab5f64515bd0b3ff48334f4d6c0697818e4e594c2e6bcb
          docker commit bac6226f51a3 liejy/test:1.0
          
          # 然后查看镜像,Bash输出如下:
          # REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
          # liejy/test   1.0       06811dd1053f   6 seconds ago   569MB
          # ubuntu       18.04     71eaf13299f4   8 days ago      63.1MB
          docker images
          
          # 接着本地测试新镜像
          docker run -it 06811dd1053f bash
          
          # 打包好镜像后本地就可以查看到liejy/test了,当然为了共享方便,即可将新镜像推送到docker hub
          docker pull liejy/test:1.0
          
          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

          # 基础知识

          # docker run

          # 参数说明

          • -a stdin:指定标准输入/输出内容的类型,可以选择的类型包括"STDIN","STDOUT","STDERR";
          • -d:在后台运行容器,并返回容器 ID;
          • -i:以交互模式运行容器,通常与参数-t 同时使用;
          • -P:随机端口映射,容器内部端口随机映射到主机端口;
          • -p:指定端口映射,格式为主机(宿主机)端口:容器端口;
          • -t:为容器重新分配一个伪输入终端,通常与参数-i 同时使用;
          • -name:为容器指定一个名称;
          • -dns8.8.8.8:指定容器使用的 DNS 服务器,默认和宿主机一致;
          • -dns-search example.com:用于指定容器内 DNS 搜索域名,默认和宿主机一致;
          • h:指定容器的 hostname;
          • -e:设置环境变量,如-e username""xxx;
          • -env-file=[]:从指定文件中读取环境变量;
          • -cpuset:绑定容器到指定 CPU 运行,如--cpuset="0-2"或--cpuset="0,1,2";
          • -m:设置容器使用的内存空间最大值;
          • -net="bridge":指定容器的网络连接类型,支持"bridge","host","none","container:<name|id>"四种类型;
          • -link=[]:向另一个容器添加链接;
          • -expose=[]:开放一个端口或一组端口;
          • volume, -v:绑定一个卷;

          # 进阶