一、Docker概述
1.Docker为什么会出现?
一款产品: 开发–上线 两套环境!应用环境,应用配置! 开发 — 运维。 问题:我在我的电脑上可以允许!版本更新,导致服务不可用!对于运维来说考验十分 大? 环境配置是十分的麻烦,每一个机器都要部署环境(集群Redis、ES、Hadoop…) !费事费力。 发布一个项目( jar + (Redis MySQL JDK ES) ),项目能不能带上环境安装打包! 之前在服务器配置一个应用的环境 Redis MySQL JDK ES Hadoop 配置超麻烦了,不能够跨平台。 开发环境Windows,最后发布到Linux! 传统:开发jar,运维来做! 现在:开发打包部署上线,一套流程做完! 安卓流程:java — apk —发布(应用商店)一 张三使用apk一安装即可用! docker流程: java-jar(环境) — 打包项目帯上环境(镜像) — ( Docker仓库:商店)----- Docker给以上的问题,提出了解决方案!
二、Docker安装
1.Docker的基本组成
2.安装Docker
环境准备
1.需要会一点点Linux的基础
2.CentOS 7
3.我们使用Xshell连接远程服务器进行操作
环境查看
# 系统内核是 3.10 以上的
[root@VM-0-16-centos ~]# uname -r
3.10.0-1160.11.1.el7.x86_64
# 系统版本
[root@VM-0-16-centos ~]# 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 \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新yum软件包索引
yum makecache fast
#4.安装docker相关的 docker-ce 社区版 而ee是企业版
yum install docker-ce docker-ce-cli containerd.io
# 5、启动docker
systemctl start docker
#6、使用docker version查看是否按照成功
docker version
#7、测试
docker run hello-world
[root@VM-0-16-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest
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/
# 8、查看一下下载的镜像
[root@VM-0-16-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
了解:卸载docker
#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!
3.阿里云镜像加速
1.登录阿里云找到容器服务
2.镜像加速器
2.配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://lamskj8g.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
4.docker运行流程图
5.底层原理
Docker是怎么工作的?
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访 问!
Docker-Server接收到Docker-Client的指令,就会执行这个命令!
Docker为什么比VM快
1.Docker有着比虚拟机更少的抽象层。
2.docker利用的是宿主机的内核,VM需要是Guest OS。
所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导 ,虚拟机是加载Guest OS,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级!
三、Docker的常用命令
帮助命令
docker version #显示docker的版本信息。
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
#帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/build/
镜像命令
docker images 查看所有本地的主机上的镜像
[root@VM-0-16-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
# 解释
REPOSITORY # 镜像的仓库源
TAG # 镜像的标签
IMAGE ID # 镜像的id
CREATED # 镜像的创建时间
SIZE # 镜像的大小
# 可选项
-a, --all Show all images (default hides intermediate images) #列出所有镜像
-q, --quiet Only show numeric IDs # 只显示镜像的id
# 执行结果
[root@VM-0-16-centos ~]# docker images -aq #显示所有镜像的id
d1165f221234
docker search 搜索镜像
[root@VM-0-16-centos ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11332 [OK]
mariadb MariaDB Server is a high performing open sou… 4307 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 841 [OK]
# 可选项,通过收藏(stara)来过滤
[root@VM-0-16-centos ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
# 例子:搜索收藏大于3000的
[root@VM-0-16-centos ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11332 [OK]
mariadb MariaDB Server is a high performing open sou… 4307 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@VM-0-16-centos ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
e1acddbe380c: Pull complete #分层下载: docker image 的核心 联合文件系统
bed879327370: Pull complete
03285f80bafd: Pull complete
ccc17412a00a: Pull complete
1f556ecc09d1: Pull complete
adc5528e468d: Pull complete
1afc286d5d53: Pull complete
6c724a59adff: Pull complete
0f2345f8b0a3: Pull complete
c8461a25b23b: Pull complete
3adb49279bed: Pull complete
77f22cd6c363: Pull complete
Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926 # 签名 防伪
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@VM-0-16-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e1acddbe380c: Already exists
bed879327370: Already exists
03285f80bafd: Already exists
ccc17412a00a: Already exists
1f556ecc09d1: Already exists
adc5528e468d: Already exists
1afc286d5d53: Already exists
4d2d9261e3ad: Pull complete
ac609d7b31f8: Pull complete
53ee1339bc3a: Pull complete
b0c0a831a707: Pull complete
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
[root@VM-0-16-centos ~]# docker rmi -f 镜像id #删除指定的镜像
[root@VM-0-16-centos ~]# docker rmi -f 镜像id 镜像id 镜像id 镜像id#删除指定的镜像
[root@VM-0-16-centos ~]# docker rmi -f $(docker images -aq) #删除全部的镜像2
容器命令
docker run # 镜像id 新建容器并启动
docker ps # 列出所有运行的容器 docker container list
docker rm 容器id # 删除指定容器
docker start 容器id #启动容器
docker restart容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
说明:我们有了镜像才能创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
docker run [可选参数] image
# 参数说明
--name="name" 第二个name给容器起个名字 tomcat01 tomcat02 ,用来区分容器
-d 后台方式运行
-it 交互方式运行,进入容器内查看内容
-p (小写)指定容器的端口 -p 8080:8080
-p ip:容器端口(常用)
-p 主机端口:容器端口(常用)
-p 容器端口
-P (大写)随机指定端口
# 测试
# 进入容器
[root@VM-0-16-centos ~]# docker run -it centos /bin/bash
[root@a34f38728486 /]# ls # 查看容器内的centos 基础版本 很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器退回主机
[root@a34f38728486 /]# exit
exit
[root@VM-0-16-centos ~]#
列出所有运行中的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出所有正在运行的容器 + 带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-0-16-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a34f38728486 centos "/bin/bash" 5 minutes ago Exited (0) About a minute ago infallible_davinci
5f9e8f36db41 d1165f221234 "/hello" 5 hours ago Exited (0) 5 hours ago magical_wu
退出容器
exit # 直接容器停止并退出
ctrl + P+Q # 容器不停止退出
删除容器
docker rm 容器id #删除指定容器(不能删除正在运行的容器)。如果要强制删除 rm -f
docker rm -f $(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 镜像名
[root@VM-0-16-centos ~]# docker run -d centos
6ad949d5bb619381d2e139608529d9a31f856134d7d22c9f96f5feb82bdbeede
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题docker ps ,发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs --help
# 执行结果
[root@VM-0-16-centos ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
# 模拟日志
docker run -d centos /bin/sh -c "while true;do echo hzl-log-a;sleep 1;done"
# 执行结果
[root@VM-0-16-centos ~]# docker run -d centos /bin/sh -c "while true;do echo hzl-log-a;sleep 1;done"
f9cb42705132e7b6db4e9bd099f3fb56f306e10334565c11025ed54de61e8f25
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9cb42705132 centos "/bin/sh -c 'while t…" 7 seconds ago Up 7 seconds serene_pascal
#显示日志
-tf #显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -tf 容器id #跟着日志
[root@VM-0-16-centos ~]# docker logs -tf --tail 10 f9cb42705132
查看容器中的进程信息 ps
# 命令docker top 容器id
root@VM-0-16-centos ~]# docker top f9cb42705132
UID PID PPID C STIME TTY TIME CMD
root 28999 28979 0 17:24 ? 00:00:00 /bin/sh -c while true;do echo hzl-log-a;sleep 1;done
root 30912 28999 0 17:30 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
docker inspect 容器id
进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 方式一
# 命令
docker exec -it 容器id bashshell
# 测试
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9cb42705132 centos "/bin/sh -c 'while t…" 14 minutes ago Up 14 minutes serene_pascal
f7c2c7f3b2a8 centos "/bin/bash" 18 minutes ago Up 18 minutes jovial_meninsky
[root@VM-0-16-centos ~]# docker exec -it f9cb42705132 /bin/bash
[root@f9cb42705132 /]#
# 方式二
docker attach 容器id
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝到主机上
docker cp 容器id:容器内路径 主机目的路径
# 测试
# 进入容器内部
[root@VM-0-16-centos home]# docker attach a5e3ff1352f3
[root@a5e3ff1352f3 /]# cd home/
[root@a5e3ff1352f3 home]# ls
[root@a5e3ff1352f3 home]# touch test-hzl.java
[root@a5e3ff1352f3 home]# ls
test-hzl.java
# 退出容器
[root@a5e3ff1352f3 home]# exit
exit
[root@VM-0-16-centos home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-0-16-centos home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a5e3ff1352f3 centos "/bin/bash" 3 minutes ago Exited (0) 7 seconds ago blissful_williamson
# 将文件拷贝出来到主机上(容器停止,数据可以拷贝)
[root@VM-0-16-centos home]# docker cp a5e3ff1352f3:/home/test-hzl.java /home/
[root@VM-0-16-centos home]# ls
kuangshen.java memcached redis test-hzl.java www
# 拷贝是一个手动的过程,未来使用 -v 卷的技术,可以实现
小结
Docker命令帮助文档(重要)
attach Attach local standard input, output, and error streams to a
running container
#当前shell下 attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container's changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem #
拷贝文件
create Create a new container #创建一个新的容器
diff Inspect changes to files or directories on a container's
filesystem #查看docker容器的变化
events Get real time events from the server # 从服务获取容器实时时间
exec Run a command in a running container # 在运行中的容器上运行命令
export Export a container's filesystem as a tar archive #导出容器文件系统作
为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images #列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image #
从tar包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载
一个镜像[对应save]
login Log in to a Docker registry #
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by
default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit
该文作为笔记记录使用,如有侵权请;联系立即删除!