什么是 Docker

Docker 最初是 dotCloud 公司创始人  在法国期间发起的一个公司内部项目,它是基于 dotCloud 公司多年云服务技术的一次革新,并于 ,主要项目代码在  上进行维护。Docker 项目后来还加入了 Linux 基金会,并成立推动 。

Docker 自开源后受到广泛的关注和讨论,至今其  已经超过 5 万 7 千个星标和一万多个 fork。甚至由于 Docker 项目的火爆,在 2013 年底,。Docker 最初是在 Ubuntu 12.04 上开发实现的;Red Hat 则从 RHEL 6.5 开始对 Docker 进行支持;Google 也在其 PaaS 产品中广泛应用 Docker。

为什么要用 Docker

作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势。

更高效的利用系统资源

由于容器不需要进行硬件虚拟以及运行完整操作系统等额外开销,Docker 对系统资源的利用率更高。无论是应用执行速度、内存损耗或者文件存储速度,都要比传统虚拟机技术更高效。因此,相比虚拟机技术,一个相同配置的主机,往往可以运行更多数量的应用。

更快速的启动时间

传统的虚拟机技术启动应用服务往往需要数分钟,而 Docker 容器应用,由于直接运行于宿主内核,无需启动完整的操作系统,因此可以做到秒级、甚至毫秒级的启动时间。大大的节约了开发、测试、部署的时间。

一致的运行环境

开发过程中一个常见的问题是环境一致性问题。由于开发环境、测试环境、生产环境不一致,导致有些 bug 并未在开发过程中被发现。而 Docker 的镜像提供了除内核外完整的运行时环境,确保了应用运行环境一致性,从而不会再出现 「这段代码在我机器上没问题啊」 这类问题。

持续交付和部署

对开发和运维()人员来说,最希望的就是一次创建或配置,可以在任意地方正常运行。

使用 Docker 可以通过定制应用镜像来实现持续集成、持续交付、部署。开发人员可以通过  来进行镜像构建,并结合  系统进行集成测试,而运维人员则可以直接在生产环境中快速部署该镜像,甚至结合  系统进行自动部署。

而且使用  使镜像构建透明化,不仅仅开发团队可以理解应用运行环境,也方便运维团队理解应用运行所需条件,帮助更好的生产环境中部署该镜像。

更轻松的迁移

由于 Docker 确保了执行环境的一致性,使得应用的迁移更加容易。Docker 可以在很多平台上运行,无论是物理机、虚拟机、公有云、私有云,甚至是笔记本,其运行结果是一致的。因此用户可以很轻易的将在一个平台上运行的应用,迁移到另一个平台上,而不用担心运行环境的变化导致应用无法正常运行的情况。

更轻松的维护和扩展

Docker 使用的分层存储以及镜像的技术,使得应用重复部分的复用更为容易,也使得应用的维护更新更加简单,基于基础镜像进一步扩展镜像也变得非常简单。此外,Docker 团队同各个开源项目团队一起维护了一大批高质量的 ,既可以直接在生产环境使用,又可以作为基础进一步定制,大大的降低了应用服务的镜像制作成本。

docker一键安装

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

Docker命令实战

常用命令

基础实战

1、镜像

下载最新版镜像
root@hello:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
7d63c13d9b9b: Pull complete
5cb019b641b5: Pull complete
d477de77abf8: Pull complete
c60e7d4c1c30: Pull complete
365a49996569: Pull complete
039c6e901970: Pull complete
Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看本地镜像
root@hello:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx       latest   e9ce56a96f8e   8 hours ago   141MB
root@hello:~
删除镜像
root@hello:~# docker images

REPOSITORY  TAG    IMAGE ID    CREATED    SIZE

nginx     latest   e9ce56a96f8e  8 hours ago  141MB

root@hello:~# 

root@hello:~# docker rmi e9ce56a96f8e

Untagged: nginx:latest

Untagged: nginx@sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288

Deleted: sha256:e9ce56a96f8e0e9f75051f258a595d1257bd6bb91913b79455ea77e67e686c5c

Deleted: sha256:6e5a463ea9608e4712465e1c575b2932dde96f99fa2c2fc31a5bacbe69c725cb

Deleted: sha256:a12cc243b903b34c8137e57160d206d6c1ee76a1ab6011a1cebdceb8b6ff8768

Deleted: sha256:a562e4589c72b0706526e13eed9c4f037ab5d1f50eb4529b38670abe353248f2

Deleted: sha256:fd67efaafabe1a0b146e9f7d958de79ec8fcec9aa6ee13ca3052b4acd8a3b81a

Deleted: sha256:c3967df88e47f739c3048492985aafaafecd5806de6c6870cbd76997fc0c68b0

Deleted: sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576e

root@hello:~# 

root@hello:~# docker images

REPOSITORY  TAG    IMAGE ID  CREATED  SIZE

root@hello:~#
下载指定版本镜像
root@hello:~# docker pull nginx:1.20.1
1.20.1: Pulling from library/nginx
b380bbd43752: Pull complete
83acae5e2daa: Pull complete
33715b419f9b: Pull complete
eb08b4d557d8: Pull complete
74d5bdecd955: Pull complete
0820d7f25141: Pull complete
Digest: sha256:a98c2360dcfe44e9987ed09d59421bb654cb6c4abe50a92ec9c912f252461483
Status: Downloaded newer image for nginx:1.20.1
docker.io/library/nginx:1.20.1
root@hello:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.20.1   c8d03f6b8b91   5 weeks ago   133MB
root@hello:~#

2、容器

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

【docker run  设置项  镜像名  】 镜像启动运行的命令(镜像里面默认有的,一般不会写)

# -d:后台运行

# --restart=always: 开机自启

# -p 主机端口:容器端口

root@hello:~# docker run --name=myningx -d --restart=always -p 88:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
7d63c13d9b9b: Pull complete
5cb019b641b5: Pull complete
d477de77abf8: Pull complete
c60e7d4c1c30: Pull complete
365a49996569: Pull complete
039c6e901970: Pull complete
Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288
Status: Downloaded newer image for nginx:latest
15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60
root@hello:~#
root@hello:~# curl -I 127.0.0.1:88
HTTP/1.1 200 OK
Server: nginx/1.21.4
Date: Wed, 17 Nov 2021 02:03:13 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 02 Nov 2021 14:49:22 GMT
Connection: keep-alive
ETag: "61814ff2-267"
Accept-Ranges: bytes root@hello:~#
查看当前运行的容器
root@hello:~# docker ps
CONTAINER ID   IMAGE     COMMAND                 CREATED             STATUS             PORTS                               NAMES
15db0ba492cf   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:88->80/tcp, :::88->80/tcp   myningx
root@hello:~#
停止容器
root@hello:~# docker stop 15db0ba492cf
15db0ba492cf
root@hello:~#
root@hello:~# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS   PORTS     NAMES
root@hello:~#
查看所有容器
root@hello:~# docker  ps -a
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS                     PORTS     NAMES
15db0ba492cf   nginx     "/docker-entrypoint.…"   2 minutes ago   Exited (0) 12 seconds ago             myningx
root@hello:~#
启动容器
root@hello:~# docker start 15db0ba492cf
15db0ba492cf
root@hello:~# docker ps
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS                               NAMES
15db0ba492cf   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 3 seconds   0.0.0.0:88->80/tcp, :::88->80/tcp   myningx
root@hello:~#
删除容器,在运行中无法删除
root@hello:~# docker rm 15db0ba492cf
Error response from daemon: You cannot remove a running container 15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60. Stop the container before attempting removal or force remove
强制删除容器
root@hello:~# docker rm -f 15db0ba492cf
15db0ba492cf
root@hello:~#

3、进入容器操作容器

root@hello:~# docker exec -it b1d72657b /bin/bash
root@b1d72657b272:/#
root@b1d72657b272:/#

4、修改容器内容

root@hello:~# docker exec -it b1d72657b /bin/bash
root@b1d72657b272:/# echo "123" > /usr/share/nginx/html/index.html
root@b1d72657b272:/#
root@b1d72657b272:/# echo "cby" > /usr/share/nginx/html/index.html root@hello:~# curl 127.0.0.1:88
123
root@hello:~# root@hello:~# docker exec -it b1d72657b /bin/bash
root@b1d72657b272:/# echo "cby" > /usr/share/nginx/html/index.html root@hello:~# curl 127.0.0.1:88
cby
root@hello:~#

5、挂载外部数据

root@hello:~# docker run --name=myningx -d --restart=always -p 88:80 -v /data/html:/usr/share/nginx/html/ nginx  

e3788cdd7be695fe9a1bebd7306c131d6380da215a416d19c162c609b8f108ae

root@hello:~# 

root@hello:~# 

root@hello:~# curl 127.0.0.1:88

<html>

<head><title>403 Forbidden</title></head>

<body>

<center><h1>403 Forbidden</h1></center>

<hr><center>nginx/1.21.4</center>

</body>

</html>

root@hello:~# 

root@hello:~# echo "cby" > /data/html/index.html

root@hello:~# 

root@hello:~# 

root@hello:~# curl 127.0.0.1:88

cby

root@hello:~#

6、将运行中的容器构建为镜像

root@hello:~# docker ps
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS                               NAMES
e3788cdd7be6   nginx     "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:88->80/tcp, :::88->80/tcp   myningx
root@hello:~#
root@hello:~# docker commit -a "cby" -m "my app" e3788cdd7be6 myapp:v1.0
sha256:07a7b54c914c79dfbd402029a3d144201235eca72a4f26c92e2ec7780c485226
root@hello:~#
root@hello:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
myapp       v1.0     07a7b54c914c   4 seconds ago   141MB
nginx       latest   e9ce56a96f8e   8 hours ago     141MB
root@hello:~#

7、镜像保存与导入

root@hello:~# docker save -o cby.tar myapp:v1.0
root@hello:~# ll cby.tar
-rw------- 1 root root 145910784 Nov 17 02:21 cby.tar
root@hello:~#
root@hello:~# docker load -i cby.tar
Loaded image: myapp:v1.0
root@hello:~#
root@hello:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
myapp       v1.0     07a7b54c914c   3 minutes ago   141MB
nginx       latest   e9ce56a96f8e   8 hours ago     141MB
nginx        1.20.1   c8d03f6b8b91   5 weeks ago     133MB
root@hello:~#

8、推送到DockerHub,并在其他主机上可拉该镜像

root@hello:~# docker tag myapp:v1.0 chenbuyun/myapp:v1.0
root@hello:~#
root@hello:~# 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: chenbuyun
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
root@hello:~# docker push chenbuyun/myapp:v1.0
The push refers to repository [docker.io/chenbuyun/myapp]
799aefeaf6b1: Pushed
fd688ba2259e: Mounted from library/nginx
c731fe3d8126: Mounted from library/nginx
3b1690d8cd86: Mounted from library/nginx
03f105433dc8: Mounted from library/nginx
bd7b2912e0ab: Mounted from library/nginx
e8b689711f21: Mounted from library/nginx
v1.0: digest: sha256:f085a533e36cccd27a21fe4de7c87f652fe9346e1ed86e3d82856d5d4434c0a0 size: 1777
root@hello:~#
root@hello:~# docker logout
Removing login credentials for https://index.docker.io/v1/
root@hello:~#
root@hello:~# docker pull chenbuyun/myapp:v1.0
v1.0: Pulling from chenbuyun/myapp
Digest: sha256:f085a533e36cccd27a21fe4de7c87f652fe9346e1ed86e3d82856d5d4434c0a0
Status: Downloaded newer image for chenbuyun/myapp:v1.0
docker.io/chenbuyun/myapp:v1.0
root@hello:~#
root@hello:~# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
chenbuyun/myapp   v1.0     07a7b54c914c   9 minutes ago   141MB
myapp             v1.0     07a7b54c914c   9 minutes ago   141MB
nginx             latest   e9ce56a96f8e   8 hours ago     141MB
nginx             1.20.1   c8d03f6b8b91   5 weeks ago     133MB
root@hello:~#

以上仅为常用命令,更多docker相关知识可在:https://www.runoob.com/docker/docker-tutorial.html

https://blog.csdn.net/qq_33921750

https://my.oschina.net/u/3981543

https://www.zhihu.com/people/chen-bu-yun-2

https://segmentfault.com/u/hppyvyv6/articles

https://juejin.cn/user/3315782802482007

https://space.bilibili.com/352476552/article

https://cloud.tencent.com/developer/column/93230

知乎、CSDN、开源中国、思否、掘金、哔哩哔哩、腾讯云

学习docker看此文足以的更多相关文章

  1. 从零开始学习 Docker

      这篇文章是我学习 Docker 的记录,大部分内容摘抄自 <<Docker - 从入门到实践>> 一书,并非本人原创.学习过程中整理成适合我自己的笔记,其中也包含了我自己的 ...

  2. android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

    android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升

  3. 转 从红帽、GitHub和Docker看开源商业模式的进阶

    从红帽.GitHub和Docker看开源商业模式的进阶 发表于2014-12-16 10:26| 7594次阅读| 来源http://stratechery.com/| 0 条评论| 作者Ben Th ...

  4. 如何学习Docker

    如何学习Docker 学习Docker,如果没有云计算的基本知识,以及内核的基本知识,那么学习并理解起来会稍吃力.作为容器,Docker容器的优势在哪,不足在哪,最好了解容器的实现是怎样的(简单了解) ...

  5. 系统学习Docker 践行DevOps理念

    Docker代表的容器技术是近两年的大热技术,和人工智能.区块链等热点不同,容器技术的门槛并不高,每一个开发.测试.运维人员都能在日常工作中掌握和使用,是当今IT从业人员的必备技能之一.本课程会带大家 ...

  6. [转帖]nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件

    nginx学习,看这一篇就够了:下载.安装.使用:正向代理.反向代理.负载均衡.常用命令和配置文件 2019-10-09 15:53:47 冯insist 阅读数 7285 文章标签: nginx学习 ...

  7. 从零开始学习docker之在docker中搭建redis(集群)

    docker搭建redis集群 docker-compose是以多容器的方式启动,非常适合用来启动集群 一.环境准备 云环境:CentOS 7.6 64位 二.安装docker-compose #需要 ...

  8. Docket学习--Docker入门

    什么是Docker? Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...

  9. 学习docker

    虚拟机下Ubuntu环境 1.sudo apt-get update 2.sudo apt-get install docker.io 3.在daocloud(http://www.daocloud. ...

  10. 学习 Docker - 入门

    Docker简介 一种虚拟容器技术. 一种虚拟化分方案: 操作系统级别的虚拟化: 只能运行相同或相似内核的操作系统: 依赖与linux内核特性:Namespace和Cgroups(Control Gr ...

随机推荐

  1. AT212 P-CASカードと高橋君

    题目描述 高桥君为了准备即将到来的7月27日土用丑日,打算邮购一些高级鳗鱼食材,通过网上银行来支付. 高桥君使用的银行卡背面有下图所示的9×9密码表.支付的时候从表中某一位置开始根据指定的方向连续读4 ...

  2. ddddd

    项目二阶段总结 账户微服务 短信发送 1.压测发现问题 首先对短信smscomponent的send方法在test单元测试类中测试,不是真的发短信测试,可以建立请求开始和结束的时间戳来确定请求的耗时. ...

  3. Tomcat后端无法返回数据

    以前使用的是Tomcat8和9,这次使用的10怎么就无法返回数据呢? 一开始以为是因为这个错误:Using CATALINA_OPTS:   "" 但是后来发现怎么尝试都无法取出这 ...

  4. CF1408

    CF1408 那个博客搭好遥遥无期. A: 直接做就行了,我没智力还写 \(dp\) . #include<bits/stdc++.h> using namespace std; #def ...

  5. shell命令查找文件

    1.find命令的参数下面是find命令一些常用参数的例子,有用到的时候查查就行了,像上面前几个贴子,都用到了其中的的一些参数,也可以用man或查看论坛里其它贴子有find命令手册使用name选项文件 ...

  6. Manage your references to .Net assemblies Dynamics 365 for Operations VS projects

    (Dynamics 365 for Operations was previously known as the New Dynamics AX) Dynamics 365 for Operation ...

  7. 前端复习之JavaScript(ECMAScript5)

    啦啦啦啦啦啦啦啦绿绿绿绿绿绿 1 1.JavaScript: 2 前段三大语言:HTML CSS js 3 HTML:专门编写网页内容的语言 4 CSS:专门编写网页样式的语言 5 js:专门编写网页 ...

  8. 制作mnist格式数据集

    import os from PIL import Image from array import * from random import shuffle # # 文件组织架构: # ├──trai ...

  9. Windows下Zookeeper安装使用

    Windows下Zookeeper安装使用 ZooKeeper是一种分布式协调服务,用于管理大型主机. 在分布式环境中协调和管理服务是一个复杂的过程. ZooKeeper通过其简单的架构和API解决了 ...

  10. SQL 2019 卸载方法

    一.关闭服务 1.  win+r      services.msc   打开服务,关闭与SQL有关的服务内容. 二.卸载应用 1.win+r  control   打开控制面板---卸载程序---卸 ...