Docker

Leon
2021-07-30 / 0 评论 / 250 阅读 / 正在检测是否收录...

Docker

  1. 简介

Docker 采用Go语言编写,是一个开放源代码软件(源代码库:https://github.com/moby/moby),是一个用于开发应用、交付(shipping)应用、运行应用的开放平台。 Docker允许用户将基础设施(Infrastructure)中的应用单独分割出来,形成更小的颗粒(容器),从而提高交付软件的速度。

Docker容器与虚拟机类似,但二者在原理上不同。容器是将操作系统层虚拟化,虚拟机则是虚拟化硬件,因此容器更具有便携性、高效地利用服务器。 容器更多的用于表示 软件的一个标准化单元。由于容器的标准化,因此它可以无视基础设施(Infrastructure)的差异,部署到任何一个地方。另外,Docker也为容器提供更强的业界的隔离兼容。[1]

| | Docker容器 | 虚拟机(VM) |
| ---------- | ----------------------- | --------------------------- |
| 操作系统 | 与宿主机共享OS | 宿主机OS上运行宿主机OS |
| 存储大小 | 镜像小,便于存储与传输 | 镜像庞大(vmdk等) |
| 运行性能 | 几乎无额外性能损失 | 操作系统额外的cpu、内存消耗 |
| 移植性 | 轻便、灵活、适用于Linux | 笨重、与虚拟化技术耦合度高 |
| 硬件亲和性 | 面向软件开发者 | 面向硬件运维者 |

logoHorizontal.png

可使用Play with Docker https://labs.play-with-docker.com/来练习docker指令

  1. Hello World

使用docker运行hello world[2]

docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

docker.png

  1. 简单使用

    1. 目标:建立一个“Hi ThunderSoft”Nginx网页服务器镜像。
    2. 拉取nginx基本镜像

      docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
33847f680f63: Pull complete 
dbb907d5159d: Pull complete 
8a268f30c42a: Pull complete 
b10cf527a02d: Pull complete 
c90b090c213b: Pull complete 
1f41b2f2bf94: Pull complete 
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
  1. 查看本地镜像

    docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    08b152afcfae   10 days ago   133MB
  1. 运行镜像

    docker run -dp 80:80 nginx
  运行后系统返回了一串sha256的容器id

  查看容器
docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
fc8162606fcf   nginx     "/docker-entrypoint.…"   34 seconds ago   Up 33 seconds   0.0.0.0:80->80/tcp   nifty_leavitt
  同时暴露出80端口

  ![image-20210801193628279.png][3]

  点击(访问这个网址)即可看到nginx的默认页面。

  # Welcome to nginx!

  If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

  For online documentation and support please refer to [nginx.org](http://nginx.org/).
  Commercial support is available at [nginx.com](http://nginx.com/).

  *Thank you for using nginx.*
  1. 修改默认页面

    1. 进入容器

      docker exec -it 容器名称或id(id前几位就行) /bin/bash
         root@fc8162606fcf:/# 
  2. 修改nginx默认文件内容
echo "<h1>Hi Thundersoft</h1>" > /usr/share/nginx/html/index.html
  3. 刷新页面即可看到修改后的内容。

     # Hi Thundersoft
  1. 从容器创建镜像

    commit -a "作者名" -m "说明" 容器名称或者id 镜像名称:镜像版本
  成功则返回了一串sha256的镜像id

  查看本地镜像
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
ts           latest    298133099b1e   37 seconds ago   133MB
nginx        latest    08b152afcfae   10 days ago      133MB
  1. 保存镜像为tar归档文件

    docker save -o 压缩文件名 镜像名:标签
  查看
$ ls -l
total 134240
-rw-------    1 root     root     137459712 Aug  1 11:49 ts.tar
  1. 删除刚刚创建的镜像。

    $ docker rmi 
    nginx:latest  ts:latest     
    [node1] (local) root@192.168.0.13 ~
    $ docker rmi ts:latest 
    Untagged: ts:latest
    Deleted: sha256:298133099b1ef66a0bd4ad0c04676118dcaef22e635928680692bcbf5cc56af5
    Deleted: sha256:d111dc2677957b6e00fbb140fae7ac110d6d258a055d2bef33a88249ad9897f9
    [node1] (local) root@192.168.0.13 ~
    $ docker images
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    nginx        latest    08b152afcfae   10 days ago   133MB
  2. tar归档镜像的使用

    docker load < ts.tar
  ts已成功导入并且可以成功运行
bcf8600f8844: Loading layer [==================================================>]  14.85kB/14.85kB
Loaded image: ts:latest
[node1] (local) root@192.168.0.13 ~
$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
ts           latest    298133099b1e   7 minutes ago   133MB
nginx        latest    08b152afcfae   10 days ago     133MB
[node1] (local) root@192.168.0.13 ~
$ docker run -dp 81:80 ts
a4818859dc11a41f0a6d6f36cc130c7e0c8bfff317572114a88b6be037fc6915
  ![image-20210801195619457.png][4]

  # Hi Thundersoft
  1. 远程镜像仓库的使用

    1. 在docker hub注册
      https://hub.docker.com/signup
    2. 终端登录

      docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: fuhailong1998              
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
  3. 将自己的镜像改名
 docker tag 镜像名或id docker hub用户名/镜像名:版本
     For example:
docker tag ts:latest fuhailong1998/ts:latest
  4. 将镜像push到自己的远程库
docker push docker hub用户名/镜像名:版本
     For example:
docker push fuhailong1998/ts:latest
The push refers to repository [docker.io/fuhailong1998/ts]
bcf8600f8844: Pushed 
e3135447ca3e: Mounted from library/nginx 
b85734705991: Mounted from library/nginx 
988d9a3509bb: Mounted from library/nginx 
59b01b87c9e7: Mounted from library/nginx 
7c0b223167b9: Mounted from library/nginx 
814bff734324: Mounted from library/nginx 
latest: digest: sha256:e538a178407d3cd1fe84ba48c84be1e4a59e7f16e19db2538f2e294fb0e3244b size: 1778
     此时刷新自己的docker hub远程库即可看到自己的镜像。

     ![image-20210801200636389.png][5]

  5. 在另一台主机拉取镜像。

     1. 在Play with Docker上新建节点(相当于新建一台主机)。

     ![image-20210801200852986.png][6]

     2. 此时这台新节点是没有任何镜像的
$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
     3. 拉取远程镜像
$ docker pull fuhailong1998/ts
Using default tag: latest
latest: Pulling from fuhailong1998/ts
33847f680f63: Pull complete 
dbb907d5159d: Pull complete 
8a268f30c42a: Pull complete 
b10cf527a02d: Pull complete 
c90b090c213b: Pull complete 
1f41b2f2bf94: Pull complete 
1db4419f88f4: Pull complete 
Digest: sha256:e538a178407d3cd1fe84ba48c84be1e4a59e7f16e19db2538f2e294fb0e3244b
Status: Downloaded newer image for fuhailong1998/ts:latest
docker.io/fuhailong1998/ts:latest
$ docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
fuhailong1998/ts   latest    298133099b1e   26 minutes ago   133MB
     4. 将刚刚拉取的镜像运行为容器
$ docker run -dp 80:80 2981
703d229923940401f26a35ebf9b562305abe421460f14d6ddeeb06c2a3fe4c87
        运行后此节点显示“80”,代表暴露了80端口。

        ![image-20210801201436234.png][7]

        点击访问就是刚刚在第一个节点创建的镜像。

        # Hi Thundersoft

     

     
  1. 基本命令[3]

##### 容器生命周期管理

| 命令 | 说明 | |
| :------------: | :------------------------------: | --------------------------------------- |
| docker run | 创建一个新的容器并运行一个命令。 | |
| docker start | 启动一个或多个已经被停止的容器。 | |
| docker stop | 停止一个运行中的容器。 | |
| docker restart | 重启容器docker。 | |
| docker kill | 终止一个运行中的容器。 | |
| docker rm | 删除容器。 | |
| docker pause | 暂停容器中所有的进程。 | |
| docker unpause | 恢复容器中所有的进程。 | |
| docker create | 创建一个新的容器但不启动它。 | |
| docker exec | 在运行的容器中执行命令。 | docker exec -i -t dockername /bin/bash |

##### 容器操作

| 命令 | 说明 | |
| :------------: | :----------------------------------------------------------: | ------------------------------------------ |
| docker ps | 查看容器。 | |
| docker inspect | 获取容器/镜像的元数据。 | |
| docker top | 查看容器中运行的进程信息,支持 ps 命令参数。 | |
| docker attach | 连接到正在运行中的容器。 | docker attach --sig-proxy=false dockername |
| docker events | 从服务器获取实时事件。 | |
| docker logs | 获取容器的日志。 | |
| docker wait | 阻塞运行直到容器停止,然后打印出它的退出代码。 | |
| docker export | 将文件系统作为一个tar归档文件导出到输出流。 | |
| docker port | 列出指定的容器的端口映射,或者查找将PRIVATE_PORT NAT到面向公众的端口。 | |

##### 容器rootfs命令

| 命令 | 说明 |
| :------------: | :----------------------------: |
| ddocker commit | 从容器创建一个新的镜像。 |
| docker cp | 用于容器与主机之间的数据拷贝。 |
| docker diff | 检查容器里文件结构的更改。 |

##### 镜像仓库

| 命令 | 说明 |
| :-----------: | :----------------------------------------------------------: |
| docker login | 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub |
| docker logout | 登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub |
| docker pull | 从镜像仓库中拉取或者更新指定镜像。 |
| docker push | 将本地的镜像上传到镜像仓库,要先登陆到镜像仓库。 |
| docker search | 从Docker Hub查找镜像 |

##### 本地镜像管理

| 命令 | 说明 |
| :------------: | :---------------------------------: |
| docker images | 列出本地镜像。 |
| docker rmi | 删除本地镜像。 |
| docker tag | 标记本地镜像,将其归入某一仓库。 |
| docker build | 使用 Dockerfile 创建镜像。 |
| docker history | 查看镜像的创建历史。 |
| docker save | 将指定镜像保存成 tar 归档文件。 |
| docker load | 导入使用docker save命令导出的镜像。 |
| docker import | 从归档文件中创建镜像。 |

##### info|version

命令说明
docker info显示 Docker 系统信息,包括镜像和容器数。
docker version显示 Docker 版本信息。

参考文献

0

评论 (0)

取消