1.0 了解Docker
基础Docker先要了解基础概念
全程围绕
1.主机
2.镜像
3.容器
技术镜像
主机:本机
镜像:下载组件的仓库
容器:启动组建的地址
进阶知识点
- Docker Compose
- Docker Swarm
- CI\CD Jenkins
服务查询
[root@VM-12-14-centos conf]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
掌握指令:
镜像篇幅
<!-- 查询镜像 -->
docker images
<!-- 查询镜像 查询订阅量超过3000的 -->
docker search mysql --filter==STARS=300
<!-- 下载镜像 不屑tag 默认 latest(最新版本) -->
docker pull {name}:{tag}
<!-- 删除镜像 -->
docker rmi -f $(docker ps -aq)
容器篇幅
<!-- 查询容器 -->
docker ps #running容器列表
<!-- 进入运行的容器 -->
docker exec -it {容器名/id} /bin/bash
【例子】
docker exec -it kafka bash
cd /opt/kafka_*/binl #进入目标路径
<!-- 删除容器 -f(强制删除运行的容器) -->
docker rm -f $(docker ps -aq)
<!-- 容器的启动操作 -->
docker [状态] {容器ID}
[状态]start restart stop kill
<!-- 容器日志 -->
docker logs -f -t --tail {显示的日志条数} {容器名/id}
<!-- 查询容器的进程信息 -->
docker top {容器名/id}
<!-- 查看镜像容器内部信息 -->
docker inspect {容器名/id}
<!-- 进入容器 查看运行情况 -->
docker attach {容器名/id}
<!-- 容器拷贝路径 -->
docker cp {容器id}:/{文件路径} {要保存的本机地址}
【例子】
docker cp 1sf/user/java.jar /data/log
docker pull详解
[root@instance-9uhh6pnu data]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete ## 层级 docker image 的联合文件,增加同一个工具的复用性。相同内容即可不用下载
93619dbc5b36: Pull complete ## UnionFS 联合文件系统
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
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 ## 真实的地址
docker run详解
常用
docker run [参数] image
[参数]
--name="{name}" 容器名称,用于区分容器
-i -t 交互方式运行 进入容器查看内容
-d 后台运行
-P 指定端口 {主机端口}:{容器端口} 可以之开启{容器端口} 添加IP也可以
--rm 再容器启动关闭后,自动删除改容器
-v 文件挂载 {主机路径}/{容器路径}
-e elasticasearch 特殊配置(可以看docker相关文档)
--volumes-from 容器和容器间相互挂载 {父容器id}
docker 具名挂载 匿名
<!--启动并进入容器 基础-->
docker run -d -p -it nginx /bin/bash
<!--匿名挂载-->
docker run -d -P --name nginx01 -v /etc/nginx nginx
等效
docker run -d -p -it nginx
<!--具名挂载-->
docker run -d -P --name nginx02 -v ceshi-nginx:/etc/nginx nginx
<!-- 指定路径挂载 -->
docker run -d -P --name nginx03 -v /data/nginx:/etc/nginx nginx
<!--(容器内路径)-->
/etc/nginx
<!-- 查询数据卷 -->
docker volume ls
local {ID}
docoker目录结构
[root@VM-12-14-centos conf]# cd /var/lib/docker/
[root@VM-12-14-centos docker]# ls
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
buildkit
containers 容器
image 镜像
network 网络
overlay2
plugins 插件
runtimes
swarm
tmp
trust
volumes 挂载文件
docker 镜像 commit
1.0 对原本的镜像进行修改
docker commit
docker commit -m="提交的描述信息" -a="作者" {原容器ID} {新镜像名称}:{tag}
【例子】
[root@instance-9uhh6pnu data]# docker commit -a="仓鼠" -m="add webapps" d52368b6cb26 tomcat-cs:1.0
sha256:d1a4172a026fbe539ac9290f802d986c370dd117c85c249b543e4d058248e394
[root@instance-9uhh6pnu data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-cs 1.0 d1a4172a026f 11 seconds ago 685MB # 自己提交的
nginx latest 605c77e624dd 9 months ago 141MB
tomcat 9.0 b8e65a4d736d 10 months ago 680MB #原本的内容
mysql 5.7 c20987f18b13 10 months ago 448MB
portainer/portainer-ce latest 0df02179156a 10 months ago 273MB
[root@instance-9uhh6pnu data]#
2.0 纯手动打造镜像
<!-- 指定路径 创建文件 -->
vim Dockerfile
指定文件内容
<!-- 基础镜像 一切的起源 三种写法,其中<tag>和<digest> 是可选项,如果没有选择,那么默认值为latest -->
FROM {image}:{digest}
<!-- 指定作者 -->
MAINTAINER {name}
LABEL {name}
<!-- 构建是需要运行的指令 -->
RUN
<!-- 一个复制命令,把文件复制到镜像中。-->
ADD
<!-- 功能是为镜像指定标签-->
LABEL
<!--镜像工作目录-->
WORKDIR
<!--挂载的目录-->
VOLUME
<!--镜像保留的端口配置-->
EXPOST
<!--指定这个容器启动时运行的执行-->
CMD
<!--和CMD相同,可追加-->
ENTRYPOINT
<!-- 文件拷贝 -->
COPY
<!--构建环境变量-->
ENV
==【例子 以spring boot为例】==
FROM java:8
COPY *.jar /app.jar
CMD ["--server.prot=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
<!-- 如何构建 -->
docker build -f {需要构造的镜像路径} -t {敬香名称}:{版本号} .
【例子】
[root@instance-9uhh6pnu dockercommit]# docker build -f /data/dockercommit/dockerfile -t cscocdocker:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
---> 5d0da3dc9764
Step 2/4 : VOLUME ["test","test"]
---> Running in 97ccd3f191ec
Removing intermediate container 97ccd3f191ec
---> 302596806951
Step 3/4 : CMD echo "----结束---"
---> Running in 41754c5f08c0
Removing intermediate container 41754c5f08c0
---> 4c525ef3c73d
Step 4/4 : CMD /bin/bash
---> Running in bda60a274e95
Removing intermediate container bda60a274e95
---> b805ecfa80b6
Successfully built b805ecfa80b6
Successfully tagged cscocdocker:1.0
docker 镜像 pull
docker login -u {账号}
docker push {镜像名}
推荐阿里云镜像
疑问解决
1. 执行安装时 出现以下错误时
Last metadata expiration check: 0:03:31 ago on Sat 16 Apr 2022 11:10:20 AM CEST.
Error:
Problem 1: problem with installed package podman-2.2.1-7.module_el8.3.0+699+d61d9c41.x86_64
- package podman-2.2.1-7.module_el8.3.0+699+d61d9c41.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
- package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- cannot install the best candidate for the job
- package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering
Problem 2: problem with installed package buildah-1.16.7-4.module_el8.3.0+699+d61d9c41.x86_64
- package buildah-1.16.7-4.module_el8.3.0+699+d61d9c41.x86_64 requires runc >= 1.0.0-26, but none of the providers can be installed
- package docker-ce-3:20.10.6-3.el8.x86_64 requires containerd.io >= 1.4.1, but none of the providers can be installed
- package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.3-3.2.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.3-3.2.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.3.0+699+d61d9c41.x86_64
- conflicting requests
- package runc-1.0.0-56.rc5.dev.git2abd837.module_el8.3.0+569+1bada2e4.x86_64 is filtered out by modular filtering
- package runc-1.0.0-64.rc10.module_el8.3.0+479+69e2ae26.x86_64 is filtered out by modular filtering
(try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)
看报错信息是已经有了 podman
和 buildah
看样子操作系统只允许一个容器工具存在。然不兼容,那就卸载掉 podman ,然后在安装 Docker.
yum -y erase podman buildah
2. 运行mysql报错
[root@VM-12-14-centos my.cnf.d]# sudo docker run -d -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
docker: Error response from daemon: Conflict. The container name "/mysql" is already in use by container "8028e0740331c91adfa02fa46f7b7a331858670438303a1c6c8326c56053f01b". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'
(1)移除原来的container ID(就是长长的那串数字)
docker rm 8028e0740331c91adfa02fa46f7b7a331858670438303a1c6c8326c56053f01b
(2)再创建新容器
改变原-v的路径
评论区