官方文档:按照步骤要求安装,当中遇到问题在纪录下来
使用FinalShell远程连接linux安装
1先删除旧版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2使用镜像仓库的方式安装
安装需要的软件包
$ sudo yum install -y yum-utils
安装DOCKER引擎(ce 社区版 ee 企业版)
$ sudo yum install docker-ce docker-ce-cli containerd.io
启动docker
$ sudo systemctl start docker
查看是否成功 (docker version 命令查看版本 显示以下相关则表明成功)
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
hello-world
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
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:
For more examples and ideas, visit:
查看一下这个hello-world
(docker images 命令查看拉取的所有镜像)
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
卸载Docker
- 卸载Docker Engine,CLI和Containerd软件包:
yum remove docker-ce docker-ce-cli containerd.io
- 主机上的映像,容器,卷或自定义配置文件不会自动删除。要删除所有图像,容器和卷:
(docker工作路径默认在/var/lib/docker)
rm -rf /var/lib/docker
docke run运行流程
底层原理
Docker的常用的命令
指定版本下载
容器命令
查看容器的元数据: docker inspect 容器id
进入当前正在运行的容器
docker的命令是十分多的,上面是我们最常用的
docker安装tomcat #启动运行 [root@localhost home]# docker run -d -p 8080:8080 --name tomcat01 tomcat #测试访问发现 报404 未找到 #原因 1,linux命令少了 2,没有webapp 阿里云镜像 默认是最小的镜像 没用的都删除了 只保证能运行 #可以把 webapps.dist 下面的赋值到 webapps下即可访问 进入 容器内部 cp -r webapps.dist/* webapps
总结一句话:容器的持久化和同步操作!容器间的数据也是可以共享的!
类似于双向绑定,挂载了之后相当于后面在linux修改就把容器里面的也修改了
这里构建后面记得加一个 .
重新构建后 vim ifconfig命令都可以用了
docker history 查看镜像变更历史记录
dockerfile里面CMD命令和ENTRYPOINT命令的区别
cmd命令是执行一次 后面如果在启动这个镜像的时候追加了参数它会把里面的命令替换掉
ENTRYPOINT则是追加上去
实战:使用Dockerfile制作Tomcat镜像
1,准备镜像文件tomcat压缩包,jdk的压缩包
2,编写dockerfile文件,使用官方命名Dockerfile,这样构建的时候久不用制定名字了,默认寻找这个名字
FROM centos
MAINTAINER lzc1029669636@qq.com
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u144-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.59.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ENV CLASSPATH JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.59
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.59
ENV PATH JAVA_HOME/bin:CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.59/bin/startup.sh && tail -F /url/local/apache-tomcat-8.5.59/bin/logs/catalin
a.out
3,构建镜像
docker build -t diytomcat .
4,启动镜像
docker run -d -p 8080:8080 --name lzctomcat -v /home/lzc/tomcat/test:/usr/local/apache-tomcat-8.5.59/webapps/test -v /home/lzc/tomcat/tomcatlogs/:/usr/local/apache-tomcat-8.5.59/logs diytomcat
5,访问测试成功
6,发布项目(做了挂载 在本地发布就可以了 tomcat 一个项目是WEB-INF 下面是web.xml 同级的是jsp文件)
发布自己的镜像
1,地址hub.docker.com/注册自己的账号
2,确定这个账号可以登录、
3,在我们服务器上提交自己的镜像
4,登录完毕就可以提交
docker push 镜像名 带上版本号
docker网络
理解Docker0
清空所有环境
三个网络
问题:docker是如何处理容器网络访问的?
原理
1、我们没启动一个docker容器,docker都会给docker容器分配一个ip,我们只要安装了docker,就会有一个网卡docker0桥接模式,使用的技术是evth-pair技术!
如何使用容器名来ping通网络
使用--link连接访问 正的访问可以访问,反过来就不能ping
探究inspect
其实就是tomcat02在本地的hosts文件配置了tomcat01的映射,所有就可以ping到tomcat01
反之没有就不可以
本质:--link就是我们在hosts配置映射
现在不推荐--link了,都是自定义网络
自定义网络
测试
自定义创建网络
--driver代表网络模式
--subnet 设置子网 后面的/16 代表可以分配65535个网络 /24代表分配255个
--gateway 配置网关 类似于我们平常的wifi的网关
就可以看到我们的创建的网络
测试使用自己的网络
运行两个容器使用该网络
再次查看
可以看到容器中有两个
这时我们用自己的网络使容器之间都能ping通了,不用使用--link
自定义的网络docker都已经帮我们维护好了对应的关系。
好处:
搭建集群的时候使用不同的网络保证集群是安全和健康的
网络联通
不同的网段之间如何联通网络?直接ping是不可能的,这时需要
测试打通tomcat01 - mynet
联通之后
mynet里面就有了tomcat01
一个容器两个ip地址
实战:部署redis集群
shell脚本
创建网卡
docker network create redis --subnet 172.38.0.0/16
通过脚本创建留个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
clluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
启动redis这些容器
docker run -p 637{port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
进入控制台
docker exec -it redis-1 /bin/sh
创建集群
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
创建完毕
进入集群模式测试
集群信息
节点信息
springboot微服务打包镜像
步骤:
1创建dockerfile文件
编写命令:例如
//基于java8
FROM java:8
//复制当前项目下所有jar结尾的文件 拷贝到 app.jar
COPY *.jar /app.jar
//映射服务地址
CMD ["--server.port=9999"]
//暴露端口
EXPOSE 9999
//执行java-jar命令
ENTRYPOINT ["java","-jar","/app.jar"]
2打包服务生成一个jar包
3传输发布到服务器
4发布构建到docker
5构建成功
6启动测试