Docker_入门

目录

toc

参考文献

docker中文网

尚硅谷_Docker核心技术(基础篇)

DockerHub

Docker官方文档(英文)

基础理论

技术意义

解决了运行环境和配置问题的软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术

比起传统虚拟机,更轻便,省去系统的硬件资源

image-20200601212459768

Docker三大元素

镜像

  • 一个只读的模板,镜像可以用来创造镜像容器,一个镜像创作多个容器

容器

  • 独立运行的一个或一组应用.容器使用镜像创建的运行实例
  • 它可以被启动、开始、停止删除
  • 每个容器都是相互隔离、保证安全的平台

image-20200601220216243

仓库

image-20200601220313128

标签类似版本号

三者配合

image-20200601220537249

Docker架构

architecture

The Docker daemon

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

The Docker client

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

Docker registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry. If you use Docker Datacenter (DDC), it includes Docker Trusted Registry (DTR).

When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

虚拟机与容器的对比

虚拟机

image-20200601211637821

缺点

  • 资源占用多
  • 冗余步骤多
  • 启动慢

LXC

image-20200601211841691

两者区别

image-20200601212002713

为什么docker比虚拟机快

image-20200603202750409

image-20200603202802953

如下图所示,docker没有Hypervisor 并且docker不需要重新加载操作系统,全部由Docker Engine完成

image-20200603202816278

image-20200603203228901

开发/运维(DevOps)

docker所支持的理念,即自己开发自己运维,其意义

image-20200601212235321

DockerHub

类似与github,提供Docker镜像仓库

Linux安装Docker

前置

image-20200601213435677

查看内核命令

[atguigu@hadoop101 ~]$ uname -r
2.6.32-642.el6.x86_64
[atguigu@hadoop101 ~]$ cat /etc/redhat-release
CentOS release 6.8 (Final)

centos6安装

安装命令

yum install -y epel-release
yum install -y docker-io
#如果第二个命令失败,可尝试
yum install https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm

安装后配置文件: /etc/sysconfig/docker

启动

#启动
service docker start
#查看版本
docker version

查看配置是否成功

ps -ef|grep docker

image-20200603200723559

入门案例_Hello-world

配置阿里云镜像

默认的docker-hub下载太慢了

阿里云开发者网址

注册,进入控制台

image-20200602224004289

进入加速器,查看加速地址

image-20200603195520535

修改配置

image-20200603195826121

将加速地址复制到other_args

image-20200603200332991

重启

service docker restart

运行hello-world

docker run hello-world

image-20200603201234971

底层运行原理

docker run的流程

image-20200603202225981

image-20200603202508748

常用镜像命令

启动服务

#启动
service docker start
#查看版本
docker version

docker images

查看当前主机镜像

Options

image-20200603204358832

实例

image-20200603203947748

参数说明:

image-20200603204118045

docker search

Options

image-20200603205036399

实例

image-20200603204632102

也可直接在docker hub搜索

image-20200603204736789

可以看出排列都相同

docker pull

拉取镜像

不加tag,默认latest,即最新版本

image-20200603205839260

docker rmi

删除镜像,一般需要加-f强制删除

Options

image-20200603210223866

实例

image-20200603205937996

常用容器命令

运行时报错,解决参考:

因kernel too old 而 centos6.8 升级内核

以centos镜像为例

docker pull centos

image-20200603220125530

docker run

新建并启动容器

Options

image-20200604103846482

经常将i,t一起使用

实例

以交互模式运行一个centos容器,并分配一个伪终端

image-20200604155609002

以守护式方式运行,ps查询发现后台没有进程的原因

image-20200604163803563

为了让守护式容器不退出,即要安排任务给他

docker run -d centos /bin/sh -c "while true;do echo hello zxyy;sleep 4;done"

docker ps

查看当前docker所有镜像运行的容器

Options

image-20200604142412341

实例

image-20200604155844267

退出容器

image-20200604142504340

docker start

后跟容器名或容器ID,用以打开以前运行过的容器

docker restart

重启容器

实例

image-20200604160044857

docker stop

停止容器

docker rm

删除已停止容器,注意rmi表示删除镜像,没有i表示删除容器

一次性删除多个容器

#方法一
docker rm -f ${docker ps -a -q}
#方法二
docker ps -a -q | xargs docker rm

docker logs

docker logs -f -t --tail 3 容器ID

Options

image-20200604163951642

实例

image-20200604164718997

docker top

docker top 容器ID

查看容器内部进程

docker inspect

docker inspect 容器ID

查看容器内部细节

docker attach / exex

重新进入正在运行的容器

用于退出容器的交互界面后,重新进入

image-20200604174438887

exec可以不仅如此容器,直接运行shell命令,如下图

image-20200604203351412

docker cp

将容器内的文件拷贝到主机

该命令用于当你想关掉容器,确定保留容器里的部分重要数据

image-20200604202538630

实例

image-20200604202454139

原文地址:https://www.cnblogs.com/yyjjtt/p/13045927.html