Docker基础

一、Docker概述

Docker基于Go语言开发的。

1、虚拟机技术的缺点:

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

2、Docker与传统的虚拟化的对比

容器是在操作系统层面上实现虚拟化,直接复用本地主机的操作系统,而传统方式则是在硬件层面实现。
在这里插入图片描述

3、Docker和虚拟机技术的不同

  • 传统的虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
  • 容器内的应用直接运行在宿主机的内容,容器是没有自己的内核的,也没有虚拟宿主机的硬件,所以十分轻便了
  • 每个容器间是互相隔离,每个容器内都有一个属于自己的文件系统,互不影响

二、Docker安装

Docker的基本组成

镜像(image)

docker镜像 —>模板
通过镜像可以创建多个容器

容器(container)

独立运行一个或一组多个应用,通过镜像来创建。
启动、停止、删除。
类似于一个简易的linux系统

仓库(repository)

镜像存放的地方

分为:

  • 共有仓库
    Docker Hub
  • 私有仓库
    阿里等

安装Docker

系统环境
centos 7

[root@s1lu ~]# uname -r
3.10.0-1160.53.1.el7.x86_64
[root@s1lu ~]# 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 \
    https://download.docker.com/linux/centos/docker-ce.repo

阿里镜像

yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、更新yum软件包索引

yum makecache faster

5、安装docker

yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

6、启动docker

systemctl start docker

7、通过docker version验证是否安装成功

docker version

8、hello-world

docker run hello-world

在这里插入图片描述

9、查看镜像

docker images

10、卸载docker

卸载依赖

yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin

删除资源

rm -rf /var/lib/docker
rm -rf /var/lib/containerd

run命令的工作流程

在这里插入图片描述

工作原理

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

在这里插入图片描述

Docker为什么比vm快

  • Docker有着比虚拟机更少的抽象层
  • Docker利用的是宿主机的内核,vm需要Guest OS
    在这里插入图片描述

三、Docker常用命令

1、帮助命令

docker version	#显示docker的版本信息
docker info	# 显示docker的系统信息,包括系统镜像和容器数量
docker 命令 --help	#帮助命令

官方帮助文档:https://docs.docker.com/reference/
在这里插入图片描述

2、镜像命令

docker images #查看本机所有的镜像

docker images 查看本机上的镜像

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

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

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

docker search 搜索镜像

[root@s1lu ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13205     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5047      [OK]       
phpmyadmin                      phpMyAdmin - A web interface for MySQL and M…   628       [OK]       
percona                         Percona Server is a fork of the MySQL relati…   586       [OK]       
         
#可选项
-f, --filter	#过滤

docker pull 下载镜像

[root@s1lu ~]# docker pull mysql
Using default tag: latest	#不写tag ,默认就是最latest
latest: Pulling from library/mysql
051f419db9dd: Pull complete	#分层下载 ,docker image的核心 联合文件系统
7627573fa82a: Pull complete 
a44b358d7796: Pull complete 
95753aff4b95: Pull complete 
a1fa3bee53f4: Pull complete 
f5227e0d612c: Pull complete 
b4b4368b1983: Pull complete 
f26212810c32: Pull complete 
d803d4215f95: Pull complete 
d5358a7f7d07: Pull complete 
435e8908cd69: Pull complete 
Digest: sha256:b9532b1edea72b6cee12d9f5a78547bd3812ea5db842566e17f8b33291ed2921	#签名
Status: Downloaded newer image for mysql:latest	#真实地址
docker.io/library/mysql:latest

#指定版本下载
[root@s1lu ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
9815334b7810: Pull complete 
f85cb6fccbfd: Pull complete 
b63612353671: Pull complete 
447901201612: Pull complete 
9b6bc806cc29: Pull complete 
24ec1f4b3b0d: Pull complete 
207ed1eb2fd4: Pull complete 
27cbde3edd97: Pull complete 
0a5aa35cc154: Pull complete 
e6c92bf6471b: Pull complete 
07b80de0d1af: Pull complete 
Digest: sha256:c1bda6ecdbc63d3b0d3a3a3ce195de3dd755c4a0658ed782a16a0682216b9a48
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 删除镜像

docker rmi -f 容器ID  #删除指定镜像
docker rmi -f 容器ID 容器ID 容器ID	#删除多个镜像
docker rmi -f $(docker images -aq)	#删除全部镜像

3、容器命令

有了镜像才可以创建容器

拉取一个镜像

docker pull centos

新建容器并启动

docker run [可选参数] image

#参数
--name="Name"	#容器名字,用来区分容器
-d	#后台运行
-it #使用交互方式运行,进入容器查看内容
-p #指定容器端口 -p 8080:8080  小写p
-P	#随机指定端口  大写P

# 测试
##启动容器
[root@s1lu ~]# docker run -it centos /bin/bash
[root@be959538b168 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

##退出容器
[root@be959538b168 /]# exit
exit
[root@s1lu ~]# ls
anaconda-ks.cfg  hadoop-2.7.7.tar.gz  jdk-8u221-linux-x64.tar.gz  original-ks.cfg
[root@s1lu ~]# 

列出所有容器

docker ps	#列出当前正在运行的容器

-a	#列出当前正在运行的容器和历史运行过的容器
-n=?	#列出最近创建的n个容器
-p #只显示容器ID

退出容器

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

删除容器

docker rm 容器ID	#删除指定容器,不能删除正在运行的容器,可以通过 -rf 强制删除
docker rm -g $(docker -ps -aq)	#删除所有容器
docker ps -a -q|xargs docker rm	#删除所有容器

启动容器和停止容器

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

后台启动容器

docker run -d 容器ID

# bug
使用docker ps,发现这个容器停止了
是因为:
docker容器使用后台运行,就必须要有一个前台进程,docker发先没有应用,就会自动停止

查看日志命令

docker logs  容器ID
-tf	#显示日志
-tail n	#输出n条

查看容器中的进程信息

docker top 容器ID

查看镜像的元数据

docker inspect 容器ID

进入当前正在运行的容器

docker exec -it 容器ID bashshell 
docker attach 容器ID

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

docker cp 容器ID:容器内路径 目的的主机路径 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

S1Lu

多谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值