什么是 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 nginxUsing default tag: latestlatest: Pulling from library/nginx7d63c13d9b9b: Pull complete 5cb019b641b5: Pull complete d477de77abf8: Pull complete c60e7d4c1c30: Pull complete 365a49996569: Pull complete 039c6e901970: Pull complete Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latest
查看本地镜像
root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest e9ce56a96f8e 8 hours ago 141MBroot@hello:~
删除镜像
root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest e9ce56a96f8e 8 hours ago 141MBroot@hello:~# root@hello:~# docker rmi e9ce56a96f8eUntagged: nginx:latestUntagged: nginx@sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288Deleted: sha256:e9ce56a96f8e0e9f75051f258a595d1257bd6bb91913b79455ea77e67e686c5cDeleted: sha256:6e5a463ea9608e4712465e1c575b2932dde96f99fa2c2fc31a5bacbe69c725cbDeleted: sha256:a12cc243b903b34c8137e57160d206d6c1ee76a1ab6011a1cebdceb8b6ff8768Deleted: sha256:a562e4589c72b0706526e13eed9c4f037ab5d1f50eb4529b38670abe353248f2Deleted: sha256:fd67efaafabe1a0b146e9f7d958de79ec8fcec9aa6ee13ca3052b4acd8a3b81aDeleted: sha256:c3967df88e47f739c3048492985aafaafecd5806de6c6870cbd76997fc0c68b0Deleted: sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576eroot@hello:~# root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEroot@hello:~#
下载指定版本镜像
root@hello:~# docker pull nginx:1.20.11.20.1: Pulling from library/nginxb380bbd43752: Pull complete 83acae5e2daa: Pull complete 33715b419f9b: Pull complete eb08b4d557d8: Pull complete 74d5bdecd955: Pull complete 0820d7f25141: Pull complete Digest: sha256:a98c2360dcfe44e9987ed09d59421bb654cb6c4abe50a92ec9c912f252461483Status: Downloaded newer image for nginx:1.20.1docker.io/library/nginx:1.20.1root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx 1.20.1 c8d03f6b8b91 5 weeks ago 133MBroot@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' locallylatest: Pulling from library/nginx7d63c13d9b9b: Pull complete 5cb019b641b5: Pull complete d477de77abf8: Pull complete c60e7d4c1c30: Pull complete 365a49996569: Pull complete 039c6e901970: Pull complete Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288Status: Downloaded newer image for nginx:latest15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60root@hello:~# root@hello:~# curl -I 127.0.0.1:88HTTP/1.1 200 OKServer: nginx/1.21.4Date: Wed, 17 Nov 2021 02:03:13 GMTContent-Type: text/htmlContent-Length: 615Last-Modified: Tue, 02 Nov 2021 14:49:22 GMTConnection: keep-aliveETag: "61814ff2-267"Accept-Ranges: bytesroot@hello:~#
查看以后运行的容器
root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15db0ba492cf nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:88->80/tcp, :::88->80/tcp myningxroot@hello:~#
进行容器
root@hello:~# docker stop 15db0ba492cf15db0ba492cfroot@hello:~# root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@hello:~#
查看所有容器
root@hello:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15db0ba492cf nginx "/docker-entrypoint.…" 2 minutes ago Exited (0) 12 seconds ago myningxroot@hello:~#
启动容器
root@hello:~# docker start 15db0ba492cf15db0ba492cfroot@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15db0ba492cf nginx "/docker-entrypoint.…" 2 minutes ago Up 3 seconds 0.0.0.0:88->80/tcp, :::88->80/tcp myningxroot@hello:~#
删除容器,在运行中无奈删除
root@hello:~# docker rm 15db0ba492cfError response from daemon: You cannot remove a running container 15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60. Stop the container before attempting removal or force remove
强制删除容器
root@hello:~# docker rm -f 15db0ba492cf15db0ba492cfroot@hello:~#
3、进入容器操作容器
root@hello:~# docker exec -it b1d72657b /bin/bashroot@b1d72657b272:/# root@b1d72657b272:/#
4、批改容器内容
root@hello:~# docker exec -it b1d72657b /bin/bashroot@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:88123root@hello:~# root@hello:~# docker exec -it b1d72657b /bin/bashroot@b1d72657b272:/# echo "cby" > /usr/share/nginx/html/index.html root@hello:~# curl 127.0.0.1:88cbyroot@hello:~#
5、挂载内部数据
root@hello:~# docker run --name=myningx -d --restart=always -p 88:80 -v /data/html:/usr/share/nginx/html/ nginx e3788cdd7be695fe9a1bebd7306c131d6380da215a416d19c162c609b8f108aeroot@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.htmlroot@hello:~# root@hello:~# root@hello:~# curl 127.0.0.1:88cbyroot@hello:~#
6、将运行中的容器构建为镜像
root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe3788cdd7be6 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:88->80/tcp, :::88->80/tcp myningxroot@hello:~# root@hello:~# docker commit -a "cby" -m "my app" e3788cdd7be6 myapp:v1.0sha256:07a7b54c914c79dfbd402029a3d144201235eca72a4f26c92e2ec7780c485226root@hello:~# root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyapp v1.0 07a7b54c914c 4 seconds ago 141MBnginx latest e9ce56a96f8e 8 hours ago 141MBroot@hello:~#
7、镜像保留与导入
root@hello:~# docker save -o cby.tar myapp:v1.0root@hello:~# ll cby.tar -rw------- 1 root root 145910784 Nov 17 02:21 cby.tarroot@hello:~# root@hello:~# docker load -i cby.tar Loaded image: myapp:v1.0root@hello:~# root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyapp v1.0 07a7b54c914c 3 minutes ago 141MBnginx latest e9ce56a96f8e 8 hours ago 141MBnginx 1.20.1 c8d03f6b8b91 5 weeks ago 133MBroot@hello:~#
8、推送到DockerHub,并在其余主机上可拉去该镜像
root@hello:~# docker tag myapp:v1.0 chenbuyun/myapp:v1.0root@hello:~# root@hello:~# docker loginLogin 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: chenbuyunPassword: WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeededroot@hello:~# docker push chenbuyun/myapp:v1.0The 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: 1777root@hello:~# root@hello:~# docker logoutRemoving login credentials for https://index.docker.io/v1/root@hello:~# root@hello:~# docker pull chenbuyun/myapp:v1.0 v1.0: Pulling from chenbuyun/myappDigest: sha256:f085a533e36cccd27a21fe4de7c87f652fe9346e1ed86e3d82856d5d4434c0a0Status: Downloaded newer image for chenbuyun/myapp:v1.0docker.io/chenbuyun/myapp:v1.0root@hello:~# root@hello:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEchenbuyun/myapp v1.0 07a7b54c914c 9 minutes ago 141MBmyapp v1.0 07a7b54c914c 9 minutes ago 141MBnginx latest e9ce56a96f8e 8 hours ago 141MBnginx 1.20.1 c8d03f6b8b91 5 weeks ago 133MBroot@hello:~#
以上仅为常用命令,更多docker相干常识可在:https://www.runoob.com/docker...
Linux运维交换社区
Linux运维交换社区,互联网新闻以及技术交换。
55篇原创内容
公众号
https://blog.csdn.net/qq\_33921750
https://my.oschina.net/u/3981543
https://www.zhihu.com/people/...
https://segmentfault.com/u/hp...
https://juejin.cn/user/331578...
https://space.bilibili.com/35...
https://cloud.tencent.com/dev...
知乎、CSDN、开源中国、思否、掘金、哔哩哔哩、腾讯云
本文应用 文章同步助手 同步