docker 入门到精通(一)

本贴最后更新于 1217 天前,其中的信息可能已经天翻地覆

1、docker是什么

没有docker之前,需要把一套环境用到的所有工具都部署一遍,花费的时间非常久。

有了docker之后,你只需要把环境打包成一个镜像,复制到另外一台服务器上安装镜像即可。

1.gif

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

docker能干什么?

虚拟化软件运行环境,以最小的代价换取最大的资源。比如以前一台服务器只能同时跑3个redis,用了docker之后能跑30个。并且每个redis是相互隔离的,这也是docker图标上集装箱所想表达的思想。

2、docker中的名词概念

2.jpg

仓库(repository):Docker hub,存放在镜像的地方,国内可配置镜像加速下载。

镜像(image):是一个模块,可基于此模板运行处多个容器。

容器(container):通过镜像创建的独立运行的一个或一组应用。

3、安装docker

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

3.png

Linux系统要求

CentOS 7 或者更高版本。

卸载老版本
# 卸载老版本
$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 删除资源目录
rm -rf /var/lib/docker
配置国内镜像加速
mkdir -p /etc/docker
vi daemon.json 
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}

systemctl daemon-reload
systemctl restart docker

4、Hello World

[root@localhost ~]# docker run hello-world
#############################从docker hub下载hello-world镜像###############################
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latest
############################运行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 hello-world 运行流程

docker先在本地寻找hello-world镜像没有找到就会去docker hub中下载,下载完成之后docker运行容器hello-world,后台会有一个守护进程,所谓的守护进程可以理解为一个24小时不中断运行的程序。我们通过docker命令调用守护进程,执行对应的方法完成操作。

docker底层原理图

4.jpg

docker为什么快,因为他不需要Guest OS, Guest OS就是你在虚拟机中安装的CentOS、Ubuntu,Guest OS启动时需要进行很多引导操作,非常消耗性能。而docker直接利用宿主机的内容加上docker engine就能运行容器,极大的节省了性能开销。所以说docker是秒级,虚拟机是分钟级。

回帖
请输入回帖内容 ...