Docker

安装Docker

Docker的基本组成

镜像(image):
docker镜像好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像 => run => tomcat01容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)
容器(container):
Docker利用容器技术,独立运行一个或者一个组应用,通过镜像来创建的。
启动,停止,删除,基本命令!
目前就可以把这个容器理解为一个简易的Linux系统
仓库(repository):
仓库就是放镜像的地方!
仓库分为公有仓库和私用仓库!
Docker Hub(默认是国外的)
阿里云…都有容器服务器(配置镜像加速!)

环境准备

1、CentOS 7
2、使用XShell连接远程服务器操作

环境查看

# 系统内核是3.10以上的
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# uname -r
3.10.0-1062.18.1.el7.x86_64
# 系统版本
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

安装

帮助文档

#  1、卸载旧的版本
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 2、需要的安装包
yum install -y yum-utils

# 3、设置镜像的仓库
# 阿里云镜像
yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 更新yum软件包索引
yum makecache fast

# 4、安装docker相关的  docker-ce 社区版  docker-ee 企业版
yum install docker-ce docker-ce-cli containerd.io		#docker社区版,客户端,容器

# 5、启动 Docker
systemctl start docker

# 6、查看是否启动成功
docker version

# 7、通过运行 hello-world 镜像来验证是否正确安装了 Docker Engine-Community
docker run hello-world

# 8、查看一下下载的这个 hello-world 镜像
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB

阿里云镜像加速

1、登录阿里云,找到容器镜像服务
2、找到镜像加速地址
3、配置使用

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://birr9xcq.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

Docker运行流程

docker开始运行之后,会在本机寻找镜像

  • 如果本机有这个镜像,就使用这个镜像运行
  • 如果本机没有这个镜像,就去Docker Hub上下载
    • 如果Docker Hub找不到,返回错误,找不到镜像
    • 如果Docker Hub能找到,下载这个镜像到本地,使用这个镜像运行

底层原理

Docker是怎么工作的?
Docker是一个Clinet-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问!
DockerServer接受到Docker-Client的指令,就会执行这个命令!

Docker为什么比 VM 快?
1、Docker有着比虚拟机更少的抽象层。
2、Docker利用的是宿主机的内核,vm需要的是Guest OS

所以说,新建一个容器的时候,Docker不需要向虚拟机一样重新加载一个操作系统内核,避免引导性的操作。虚拟机是加载Gueat OS,分钟级别的,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级!

Docker的常用命令

帮助命令

docker version	# 显示docker的版本信息
docker info			# 显示docker的系统信息
docker --help		# 帮助命令

帮助文档的地址:https://docs.docker.com/engine/reference/commandline/cli/

镜像命令

docker images 查看所有本地的主机上镜像

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB

# 解释
REPOSITORY	镜像的仓库源
TAG			镜像的标签
IMAGE ID	镜像的id
CREATED		镜像的创建时间
SIZE		镜像的大小

# 可选项
--all , -a		# 列出所有的镜像
--quiet , -q	# 只显示镜像的id

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images -q
feb5d9fea6a5
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images -aq
feb5d9fea6a5

docker search 搜索镜像

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13849     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5281      [OK] 

# 可选项,通过收藏来过滤
--filter=STARS=3000	# 搜索出来的镜像就是STARS大于3000的

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13849     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5281      [OK]    

docker pull 下载镜像

# 下载镜像docker pull 镜像名[:tag]
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker pull mysql
Using default tag: latest	# 如果不写tag,默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 	# 分层下载,docker image的核心  联合文件系统
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709	# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	# 真实地址

docker pull mysql
等价于
docker pull docker.io/library/mysql:latest

# 指定版本下载
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         5.7       c20987f18b13   14 months ago   448MB
mysql         latest    3218b38490ce   14 months ago   516MB
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB

docker rmi 删除镜像

[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker rmi -f 容器id	# 删除指定的容器
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker rmi -f 容器id 容器id 容器id 容器id	# 删除指定的容器
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker rmi -f $(docker images -aq)	# 删除全部的容器

容器命令

有了镜像才可以创建容器,Linux,下载一个centos镜像测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image

# 参数说明
--name="Name"	容器名字	tomcat01 tomcat02,用来区分容器
-d		后台方式运行	
-it	使用交互方式进行,进入容器查看内容
-p	指定容器的端口
	-p	ip:主机端口:容器端口
	-p	主机端口:容器端口(常用)
	-p	容器端口
-p	随机指定端口

# 测试,启动并进入容器
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker run -it centos /bin/bash
[root@aadcc8edee36 /]# ls	# 查看容器内的centos,基础版本,很多命令都是不完善的
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

# 从容器中退回主机
[root@8d3505e56e36 /]# exit
exit
[root@iZ2zehsabv9z6c2cjqgqa3Z /]# ls
bin   dev  home  lib64       media  opt    proc  run   srv  tmp  var
boot  etc  lib   lost+found  mnt    patch  root  sbin  sys  usr  www

列出所有的运行的容器

# docker ps	列出当前正在运行的容器
-a	# 列出当前正在运行的容器+带出历史运行过的容器
-n=?	# 显示最近创建的容器
-q	# 只显示容器的编号

[root@iZ2zehsabv9z6c2cjqgqa3Z /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZ2zehsabv9z6c2cjqgqa3Z /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
8d3505e56e36   centos         "/bin/bash"   8 minutes ago    Exited (0) 8 minutes ago              intelligent_swartz
aadcc8edee36   centos         "/bin/bash"   16 minutes ago   Exited (0) 10 minutes ago             vigilant_lewin
0288e20bcfcd   feb5d9fea6a5   "/hello"      24 hours ago     Exited (0) 24 hours ago               epic_feistel
798afe968659   feb5d9fea6a5   "/hello"      3 days ago       Exited (0) 3 days ago                 youthful_turing
279b29468921   feb5d9fea6a5   "/hello"      3 days ago       Exited (0) 3 days ago                 gallant_rubin
126f2dd9c455   feb5d9fea6a5   "/hello"      4 months ago     Exited (0) 4 months ago               mystifying_perlman
[root@iZ2zehsabv9z6c2cjqgqa3Z /]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
8d3505e56e36   centos    "/bin/bash"   14 minutes ago   Exited (0) 14 minutes ago             intelligent_swartz
[root@iZ2zehsabv9z6c2cjqgqa3Z /]# docker ps -aq
8d3505e56e36
aadcc8edee36
0288e20bcfcd
798afe968659
279b29468921
126f2dd9c455

退出容器

exit	# 直接容器停止并退出
Ctrl + P + Q	# 容器不停止退出

删除容器

docker rm 容器id	# 删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)	# 删除所有容器
docker ps -a -q|xargs docker rm	# 删除所有的容器

启动和停止容器的操作

dcoker start 容器id	# 启动容器
docker restart 容器id	# 重启容器
docker stop 容器id	# 停止当前正在运行的容器
docker kill 容器id	# 强制停止当前容器

常用其他命令

后台启动容器

# 命令 docker run -d 镜像名
[root@iZ2zehsabv9z6c2cjqgqa3Z ~]# docker run -d centos

# 问题docker ps,发现 centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就没有程序了

查看日志

docker logs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值