Docker 1 - 基本使用

2023-05-16

Docker

文章目录

  • Docker
    • 一、关于 Docker
      • 安装 Docker
        • 1、查看版本
        • 2、安装
        • 3、卸载 Docker
      • 常见命令
        • 查看Docker 磁盘使用情况
        • 清理磁盘
        • 停止Docker 服务
    • 二、镜像
      • 查看已安装镜像
      • 拉取镜像
      • 删除镜像
      • 查找镜像
        • 方式一:通过 Docker Hub
        • 方式二:docker search 命令
      • 加速服务(daocloud)
    • 三、容器
      • 查看容器
      • 启动/停止容器
      • 删除容器
      • 检查容器的状态信息
      • 查看容器内运行的进程
      • 启动容器 run
      • 进入容器并命令式交互
        • 方式一:exec
        • 方式二:attach
      • 从容器拷贝文件到主机上
    • 相关资料


一、关于 Docker

Docker 官网:https://www.docker.com

Github Docker 源码:https://github.com/docker/docker-ce

Docker 文档:https://docs.docker.com/reference/


安装 Docker

1、查看版本

安装之前,先看看是否安装,以及版本号

$ docker --version
Docker version 20.10.6, build 370c289

$ docker-compose --version
docker-compose version 1.29.1, build c34c88b2

$ docker version  # 更全的信息
Client:
 Cloud integration: 1.0.14
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.16.3
 Git commit:        370c289
 Built:             Fri Apr  9 22:46:57 2021
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:44:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

2、安装

在这里插入图片描述


在这里插入图片描述


$ docker cp repo:/git/getting-started/ .
$ cd getting-started 
$  docker build -t docker101tutorial . 
[+] Building 9.2s (6/24)                                       [+] Building 332.0s (25/25) FINISHED        

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

$ docker run -d -p 80:80 --name docker-tutorial docker101tutorial
8b17f9ba92e67e2d475db56345c23873129cf15f31688ba0ce2d860fe0d58a98
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:80: bind: address already in use.

在这里插入图片描述


3、卸载 Docker


常见命令

attach    Attach to a running container                 # 当前 shell 下 attach 连接指定运行镜像

build     Build an image from a Dockerfile              # 通过 Dockerfile 定制镜像

commit    Create a new image from a container changes   # 提交当前容器为新的镜像

cp        Copy files/folders from the containers filesystem to the host path   #从容器中拷贝指定文件或者目录到宿主机中

create    Create a new container                        # 创建一个新的容器,同 run,但不启动容器

diff      Inspect changes on a container's filesystem   # 查看 docker 容器变化

events    Get real time events from the server          # 从 docker 服务获取容器实时事件

exec      Run a command in an existing container        # 在已存在的容器上运行命令

export    Stream the contents of a container as a tar archive   # 导出容器的内容流作为一个 tar 归档文件[对应 import ]

history   Show the history of an image                  # 展示一个镜像形成历史

images    List images                                   # 列出系统当前镜像

import    Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]

info      Display system-wide information               # 显示系统相关信息

inspect   Return low-level information on a container   # 查看容器详细信息

kill      Kill a running container                      # kill 指定 docker 容器

load      Load an image from a tar archive              # 从一个 tar 包中加载一个镜像[对应 save]

login     Register or Login to the docker registry server    # 注册或者登陆一个 docker 源服务器

logout    Log out from a Docker registry server          # 从当前 Docker registry 退出

logs      Fetch the logs of a container                 # 输出当前容器日志信息

port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT    # 查看映射端口对应的容器内部源端口

pause     Pause all processes within a container        # 暂停容器

ps        List containers                               # 列出容器列表

pull      Pull an image or a repository from the docker registry server   # 从docker镜像源服务器拉取指定镜像或者库镜像

push      Push an image or a repository to the docker registry server    # 推送指定镜像或者库镜像至docker源服务器

restart   Restart a running container                   # 重启运行的容器

rm        Remove one or more containers                 # 移除一个或者多个容器

rmi       Remove one or more images             # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]

run       Run a command in a new container              # 创建一个新的容器并运行一个命令

save      Save an image to a tar archive                # 保存一个镜像为一个 tar 包[对应 load]

search    Search for an image on the Docker Hub         # 在 docker hub 中搜索镜像

start     Start a stopped containers                    # 启动容器

stop      Stop a running containers                     # 停止容器

tag       Tag an image into a repository                # 给源中镜像打标签

top       Lookup the running processes of a container   # 查看容器中运行的进程信息

unpause   Unpause a paused container                    # 取消暂停容器

version   Show the docker version information           # 查看 docker 版本号

wait      Block until a container stops, then print its exit code   # 截取容器停止时的退出状态值

查看Docker 磁盘使用情况

$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          2         2         995.7MB   821.7MB (82%)
Containers      2         1         17.86MB   17.77MB (99%)
Local Volumes   1         0         10.32MB   10.32MB (100%)
Build Cache     48        0         402MB     402MB

清理磁盘

$ docker system prune
WARNING! This will remove:
  - all stopped containers 
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N]

删除 关闭的容器、无用的数据卷 和 网络无TAG的镜像
-a 参数 无用的镜像也会删除 (慎用)


停止Docker 服务

$ systemctl stop docker

二、镜像

查看已安装镜像

# 当前安装的镜像
$ docker image ls
REPOSITORY          TAG       IMAGE ID       CREATED        SIZE
docker101tutorial   latest    20c870477397   21 hours ago   28MB
alpine/git          latest    7660bb3edeb5   4 days ago     25.1MB
ubuntu              latest    7e0aa2d69a15   4 weeks ago    72.7MB

Options:

  • -a:列出本地所有镜像
  • -q:只显示镜像id
  • -digests:显示镜像的摘要信息
  • --no-trunc:显示完整的镜像信息

拉取镜像

以 ubuntu 为例

# 第一次拉
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
345e3491a907: Pull complete 
57671312ef6f: Pull complete 
5e9250ddb7d0: Pull complete 
Digest: sha256:cf31af331f38d1d7158470e095b132acd126a7180a54f263d386da88eb681d93
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest


# 第二次拉
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:adf73ca014822ad8237623d388cedf4d5346aa72c270c5acc01431cc93e18e2d
Status: Image is up to date for ubuntu:latest
docker.io/library/ubuntu:latest

拉取指定 tag 的 image


删除镜像

# 删除镜像
$ docker rmi ubuntu
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 5235a49aea61 is using its referenced image 7e0aa2d69a15

# 如果有容器依赖于此镜像(无论是否运行),则镜像无法被删除

# 删除多个
$ docker rmi 镜像名1:TAG 镜像名2:TAG

# 删除全部
$ docker rmi -f $(docker images -qa)

查找镜像

方式一:通过 Docker Hub

从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

在这里插入图片描述


方式二:docker search 命令

$ docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   12302     [OK]       
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   535                  [OK]
websphere-liberty                                         WebSphere Liberty multi-architecture images …   273       [OK]       
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   251                  [OK]

OPTIONS:

  • -s:列出收藏数不小于指定值的镜像
  • --automated:只列出 automated build 类型的镜像

标签说明

  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本。

如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。


加速服务(daocloud)

市面上有很多加速服务的提供商,如:DaoCloud,阿里云等。

直接访问docker hub比较慢。通过daocloud来访问稍微快一些。

在虚拟机浏览器里打开< http://www.daocloud.io/>,然后注册用户或者直接用微信扫二维码登录。登录后点击“镜像仓库”。

点击DockerHub镜像,这里可以打开docker官方的镜像仓库,然后在里面搜索你要的镜像即可。


三、容器

创建容器会有单独的介绍


查看容器

最右侧 NAMES 列,对应的是容器的名字

# 查看所有容器(包含未运行)
$ docker ps -a  
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS                          PORTS     NAMES
dd05dd4f0b3d   docker101tutorial   "/docker-entrypoint.…"   2 minutes ago   Exited (0) About a minute ago             vibrant_franklin
8b17f9ba92e6   docker101tutorial   "/docker-entrypoint.…"   16 hours ago    Created                                   docker-tutorial
f0ecd71a3a90   alpine/git          "git clone https://g…"   21 hours ago    Exited (0) 21 hours ago                   repo

# 查看当前运行的容器
$ docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Options:

  • -a :列出当前所有正在运行的容器+历史上运行过的
  • -l :显示最近创建的容器。
  • -n:显示最近n个创建的容器。
  • -q :静默模式,只显示容器编号。
  • --no-trunc :不截断输出。

启动/停止容器

$ docker start c001  # 启动容器
c001

$ docker restart <容器 ID>  # 重启容器
c001
 
$ docker ps  # 查看运行中的容器
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS          PORTS     NAMES
ab16bcb55850   ubuntu    "bash"    8 minutes ago   Up 17 seconds             c001

$ docker stop  c001  # 关闭容器
c001

$ docker start -i  c001   # -i 启动后进入 bash 
root@ab16bcb55850:/# exit
exit

# 强制停止容器
$ docker kill <容器 ID>/<容器 NAME>

使用 版本为15.10的ubuntu系统镜像 来运行容器时,命令如下:

$ docker run -t -i ubuntu:15.10 /bin/bash 
root@d77ccb2e5cca:/#

删除容器

# 删除(已停止)容器
$ docker rm  b83c32b59c67

docker rm -f tomcat1/containerid   #删除container

# 删除镜像
$ docker rmi  7e0aa2d69a15

# 列举出所有的再进行删除
$ docker rm -f $(docker ps -a)    

检查容器的状态信息

查看容器内部细节

$ docker inspect 容器名

查看容器内运行的进程

$ docker top  容器名

如:

$ docker top con4
Error response from daemon: Container bd9177d4ec977dcc5cb1920fd23a3e0c4 is not running

启动容器 run

# 启动守护式容器
$ docker run -d con4

# 查看容器日志
$ docker logs -f -t --tail con4

进入容器并命令式交互

方式一:exec

# $ docker exec -it c001 bashShell
$ docker exec -it c001 /bin/bash

在容器中打开终端,并且可以启动新的进程。


方式二:attach

重新进入

$ docker attach c001

直接进入容器,启动命令的终端,不会启动新的进程


从容器拷贝文件到主机上

$ docker cp 容器ID:容器内路径  目的主机路径

相关资料

  • 知乎 慕课网:可能是把Docker的概念讲的最清楚的一篇文章
    https://zhuanlan.zhihu.com/p/38552635
  • 刀客传奇:Docker应用详细解析
    https://www.jianshu.com/p/ddcea0e29a31
  • 知乎十里:macOS 下使用 Docker 搭建 ubuntu 环境
    https://zhuanlan.zhihu.com/p/59548929
  • 博客园三只松鼠:Docker实践:python应用容器化
    https://www.cnblogs.com/shenh/p/9518343.html
  • Compose 命令说明
    https://wiki.jikexueyuan.com/project/docker-technology-and-combat/commands.html
  • 梁桂钊:30 分钟快速入门 Docker 教程
    https://mp.weixin.qq.com/s/DY3pLat1ehudP624Io73Hg
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Docker 1 - 基本使用 的相关文章

随机推荐

  • [Android] 通过Room操作SQLite数据库

    谷歌推荐使用Room操作数据库 xff0c Room在 SQLite 上提供了一个抽象层 xff0c 在充分利用 SQLite强大功能的同时 xff0c 能够流畅地访问数据库 Room的三个主要组件 xff1a 数据库类 xff0c 用于保
  • [Android] Dialog篇

    一 AlertDialog 简单Dialog xff1a Dialog dialog span class token operator 61 span new AlertDialog Builder span class token pu
  • 谷歌pixel刷机_如何在Google Pixel上禁用(或启用)应用建议

    谷歌pixel刷机 Android 11 introduced a feature for Google Pixel phones called App Suggestions The launcher will suggest diffe
  • [Android] 菜单加水平分割线

    选项菜单加水平分隔线 menu的item加group分组设置setGroupDividerEnabled为true span class token operator lt span menu xmlns android span clas
  • 快速安装Pytorch和Torchvision

    文章目录 1 Linux下激活自己的虚拟环境并查看Python版本2 查看需要安装的Pytorch和Torchvision版本3 直接命令行安装3 1 如果不报错的话3 2 ERROR Could not install packages
  • 【Darknet-53】YOLOv3 backbone Darknet-53 详解

    文章目录 1 模型计算量与参数量2 Darknet 53网络3 感谢链接 1 模型计算量与参数量 模型计算量与参数量的计算方式主要有两种 xff0c 一种是使用thop库 xff0c 一种是使用torchsummaryX 使用pip ins
  • 【DeeplabV3+】DeeplabV3+网络结构详解

    文章目录 1 常规卷积与空洞卷积的对比1 1 空洞卷积简介1 2 空洞卷积的优点 2 DeeplabV3 43 模型简介3 DeeplabV3 43 网络代码4 mobilenetv2网络代码5 感谢链接 聊DeeplabV3 43 网络前
  • 【YOLOv3 decode】YOLOv3中解码理解decode_box

    文章目录 1 解码是什么意思2 代码解读3 生成网格中心 代码详解4 按照网格格式生成先验框的宽高 代码详解5 感谢链接 1 解码是什么意思 在利用YOLOv3网络结构提取到out0 out1 out2之后 xff0c 不同尺度下每个网格点
  • 【DeeplabV3+ MIoU】DeeplabV3+计算评价指标

    文章目录 1 分割常用评价指标1 1 什么是MIoU1 2 什么是PA1 2 什么是MPA 2 根据混淆矩阵计算评价指标2 1 计算MIoU2 2 计算PA2 3 计算MPA 3 计算 MIoU 总体代码4 compute mIoU 函数
  • 【MobileNetV3】MobileNetV3网络结构详解

    文章目录 1 MobileNetV3创新点2 block变成了什么样2 1 总体介绍2 2 SE模块理解2 3 ReLu6和hardswish激活函数理解 3 网络总体结构4 代码解读5 感谢链接 在看本文前 xff0c 强烈建议先看一下之
  • 【Smooth L1 Loss】Smooth L1损失函数理解

    文章目录 1 引言2 L1 Loss3 L2 Loss4 Smooth L1 Loss5 曲线对比分析6 参考链接 1 引言 目标检测任务的损失函数由Classificition Loss和Bounding Box Regeression
  • Command errored out with exit status 1类似问题解决方案

    在使用pip install onnx时遇到如下错误 xff1a Building wheels for collected packages onnx Building wheel for onnx setup py error ERRO
  • 【tensorflow onnx】TensorFlow2导出ONNX及模型可视化教程

    文章目录 1 背景介绍2 实验环境3 tf2onnx工具介绍4 代码实操4 1 TensorFlow2与ONNX模型导出4 2 ONNX正确性验证4 3 TensorFlow2与ONNX的一致性检查4 4 多输入的情况4 5 设定输入 输出
  • 如何在Microsoft Edge中更改主页

    By default Microsoft Edge opens with a custom New Tab page full of content Luckily it s easy to open the browser with a
  • 【PaddlePaddle onnx】PaddlePaddle导出ONNX及模型可视化教程

    文章目录 1 背景介绍2 实验环境3 paddle onnx export函数简介4 代码实操4 1 PaddlePaddle与ONNX模型导出4 2 ONNX正确性验证4 3 PaddlePaddle与ONNX的一致性检查4 4 多输入的
  • 【linux命令】如何查看文件/文件夹所占空间大小

    文章目录 1 查看文件大小1 1 方法1 xff1a ls lh1 2 方法2 xff1a du sh1 3 方法3 xff1a stat 2 查看文件夹所占大小2 1 方法1 xff1a du2 2 方法2 xff1a ncdu 1 查看
  • Github+Hexo搭建个人博客(图文详解)

    文章目录 使用Github 43 hexo搭建个人博客 不会让小伙伴们走弯路 1 准备工作 xff1a 安装两个我们本次所需要使用的软件 xff1a 2 注册Github账号以及建立仓库 xff1a https github com htt
  • 2019-12-14-FTP服务器搭建

    title FTP服务器搭建 date 2019 12 14 15 34 19 updated 2019 12 14 15 34 19 categories 服务器 搭建 网络 tags FTP服务器 目录 什么是FTP服务器本地FTP服务
  • 小程序开发需要多少钱?

    小程序开发的费用 xff1a 一般几千到几万不等 看具体要求 其实开发小程序的价格主要取决于你要做多少页面和要做的页面和功能的复杂程度 如果是行业内比较成熟的标准化系统就会相对便宜点 至于开发多少钱 xff0c 这样看你采用以下哪种模式 x
  • Docker 1 - 基本使用

    Docker 文章目录 Docker一 关于 Docker安装 Docker1 查看版本2 安装3 卸载 Docker 常见命令查看Docker 磁盘使用情况清理磁盘停止Docker 服务 二 镜像查看已安装镜像拉取镜像删除镜像查找镜像方式