docker引擎的安装
安装步骤
根据自己的系统执行官网上写的相应步骤:docs.docker.com/engine/install。
还有一种官网提供的通用下载脚本:
# 1、执行下载docker引擎的脚本文件:
curl -fsSL get.docker.com -o get-docker.sh
# 2、执行这个脚本,设置阿里云镜像下载加速
sh get-docker.sh --mirror Aliyun
# 3、启动docker服务
systemctl enable docker # 将docker加入开机自启动的列表
systemctl start docker # 启动(restart重启 status状态 stop停止)
# 4、推荐将当前用户加入Docker组
a、创建docker组 sudo groupadd docker
b、将当前用户加入docker组 usermod -aG docker $USER (这个命令也可以自动创建docker组)
查看docker信息:
docker version
docker info
docker中的核心概念
镜像 Image
定义:一个镜像就代表一个软件
mysql镜像、redis镜像、mq镜像
容器 Container
定义:一个镜像运行一次 就会生成一个容器,容器就是一个运行的软件服务
仓库 Repository
定义:仓库用来存储所有软件的镜像位置
- 远程仓库
docker仓库网站地址:https://registry.hub.docker.com - 本地仓库 用来存储在使用docker过程中的相关镜像
位置一般在 /var/lib/docker
docker中核心架构图
docker官方远程仓库在国外,可以配置国内仓库源。
配置阿里云镜像方法:这里
docker 第一个程序
hello-world 官方提供的练手的:
docker run hello-world
执行后发现输出如下:
[root@192 containers]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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/
由此我们可以知道docker执行流程。
docker执行流程
docker中镜像相关操作
辅助命令
docker version
:用来查看docker客户端引擎和server端引擎版本信息docker info
:用来查看docker引擎详细信息docker --help
:用来查看帮助信息
镜像相关命令
docker image ls
:查看当前本地仓库中存在哪些镜像docker images
:(同上)docker images redis
:之查看redis相关的镜像
执行结果如下:
结果解释:
REPOSITORY:镜像名称
TAG:版本
IMAGE ID:部分镜像id
CREATED:镜像创建时间
SIZE:镜像大小docker pull
:下载镜像docker pull redis
docker pull redis:latest
:下载镜像的最新版本docker pull tomcat:9.0
:下载指定版本
docker search
:搜索镜像docker search redis
:这个只能看当前镜像是否存在,没有列出版本号
docker image rm hello-world
:删除镜像(只能删除没有run过的镜像)docker image rm -f hello-world
:删除镜像的同时也把容器给删了docker image rm -f $(docker images redis -q)
:把所有redis的镜像都给删掉
docker中容器相关操作
操作容器命令格式:docker 命令 [选项]
1、如何查看docker引擎中运行容器有哪些?
docker ps
:查看当前docker引擎中正在运行的容器
解释:
CONTAINER ID:容器ID
IMAGE:基于哪个镜像
COMMAND:容器内启动服务命令
CREATED:容器创建时间
STATUS:容器状态
PORTS:容器服务坚挺的端口
NAMES:容器名称
2、如何运行一个容器?docker run 镜像名|镜像id
- 简单运行tomcat镜像(这种方式直接运行容器,外部无法访问)
docker run tomcat
- 运行tomcat容器,同时设置容器与宿主机端口的映射关系
-p
docker run -p 8080(宿主机):8080(docker内) tomcat
注意:可以映射多个端口:-p 8080:8080 -p 8081:8081
- 运行tomcat,开放端口映射,后台启动该服务
-d
docker run -p 8080:8080 -d tomcat
- 在上面的基础上,指定容器名字,方便后续操作。
--name
docker run -p 8080:8080 -d tomcat --name tomcat01 tomcat
3、停止、重启、暂停、恢复 容器
docker stop 容器id/容器名
docker start xxx
、docker restart xxx
docker pause xxx
、docker unpause xxx
如果要查看暂停的容器,那需要增加-a
参数:docker ps -a
docker中容器相关操作(二)
1、杀死容器
docker kill id/name
2、删除容器
删除已经停止的容器:docker rm id/name
强制删除:docker rm -f
删除所有:docker rm -f $(docker ps -aq)
3、查看docker容器内部日志
实时查看容器内服务日志:docker logs -f id/name
4、进入容器内部
以交互模式进入容器的bash:docker exec -it id|name bash
退出容器:exit
5、容器与宿主机操作系统之间文件拷贝
- 容器内 -> 宿主机:
docker cp id:/usr/local/tomcat/webapps /root
- 宿主机 -> 容器内:
docker cp ./a.txt id:/usr/local/tomcat
6、如何部署一个web项目?
- 将test.war上传到Linux服务器
- 通过
docker cp
命令将test.war复制到容器tomcat的webapps目录中即可 - 使用ip端口测试访问
7、查看容器内运行进程
docker top id|name
8、查看容器内细节
docker inspect id|name
容器数据卷机制
Data Volume
作用:用来实现 容器中数据 和 宿主机中数据 进行映射的
注意:数据卷使用必须在容器首次启动时设置
docker run -v 宿主机目录:容器内目录 镜像
- 使用绝对路径设置数据卷(这种方式会将容器路径的原始内容全部清空,始终以宿主机路径为主):
docker run -v 宿主机绝对路径:容器内路径 ...
- 使用相对路径设置数据卷
docker run -v aa:/usr/local/tomcat/webapps ......
aa是什么? - aa代表docker数据卷中别名。注意:这个别名如果存在则直接使用,不存在则自动创建
- 使用别名方式会保留容器路径原始内容,前提是别名对应路径不能存在内容
查看容器内运行进程
docker top id|name