yum remove docker-ce \
docker-ce-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
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
yum makecache fast
yum install docker-ce docker-ce-cli containerd.io
启动docker
systemctl start docker
设置docker开机启动
systemctl enable docker
修改配置文件 /etc/docker/daemon.json,没有该文件就新建
vim /etc/docker/daemon.json
添加以下内容:
{
"registry-mirrors": [
"https://dockerhub.azk8s.cn",
"https://hub-mirror.c.163.com"
]
}
之后重新启动服务
systemctl daemon-reload
systemctl restart docker
运行hello-world镜像,验证docker
[root@localhost docker]# docker run hello-world
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镜像是一个特殊的文件系统,提供了容器运行所需要的程序,资源,配置文件.镜像不包含任何动态数据,其内容在构建之后不会被改变.
镜像是由多层文件系统联合构成.构建镜像时要一层层的构建,每一层构建完就不会再改变,后一层的改变只发生在自己这一层,比如删除前一层文件的操作,只是在当前层标记该文件已经删除,然而实际上并没有删除上一层的文件.
最终容器运行的时候,并不会看到这个文件,但是实际上该文件会一直跟随者镜像.
因此在构建镜像时,每一层尽量只包含该层所需要的东西,其他额外的东西应该在该层构建结束前清理掉.
镜像分层存储的特征使得镜像可以被复用或者定制.可以用之前构建好的镜像作为基础层,然后进一步添加新的层,构建自己所需要的镜像.
容器的实质是进程,但与直接在宿主运行的进程不同,容器进程运行于属于自己的独立的命名空间.
因此容器可以拥有自己的root文件系统,网络配置,进程空间.容器内的进程是运行在一个隔离的环境里,使用起来,像是在一个独立于宿主的系统下操作一样.
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的 类 和 实例 一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等.
前面讲过镜像使用的是分层存储,容器也是如此。每一个容器运行时,是以镜像为基础层,在其上创建一个当前容器的存储层,我们可以称这个为容器运行时读写而准备的存储层为容器存储层。
容器存储层的生存周期和容器一样,容器消亡时,容器存储层也随之消亡。因此,任何保存于容器存储层的信息都会随容器删除而丢失。
按照 Docker 最佳实践的要求,容器不应该向其存储层内写入任何数据,容器存储层要保持无状态化。所有的文件写入操作,都应该使用 数据卷(Volume)、或者绑定宿主目录,在这些位置的读写会跳过容器存储层,直接对宿主(或网络存储)发生读写,其性能和稳定性更高。
数据卷的生存周期独立于容器,容器消亡,数据卷不会消亡。因此,使用数据卷后,容器删除或者重新运行之后,数据却不会丢失。
docker pull centos:7
docker images
docker run -dit centos:7 bash
--name:为容器指定名称
docker restart xxxx
docker exec -it xxxx bash
exit
docker stop xxxx
docker rm xxxx
docker container prune
删除镜像之前要移除使用该镜像创建的容器
使用IMAGE ID前4位删除镜像
docker image rm xxxx
指定镜像删除
docker image rm centos
数据卷 是一个可供一个或多个容器使用的特殊目录
docker volume create my-vol
docker volume ls
docker volume inspect my-vol
查询的结果为:
[
{
"CreatedAt": "2020-04-17T21:03:52+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
"Name": "my-vol",
"Options": {},
"Scope": "local"
}
]
docker run -it -v my-vol:/webapp centos:7 bash
删除之前要移除使用该数据卷的容器
docker volume rm my-vol
清理无主数据卷
docker volume prune
docker run -it -v /usr/app:/opt/app centos:7 bash
容器之间要通信则需要指定容器ip,创建网络,如tomcat访问mysql时,mysql必须得有ip才能访问
查看docker所有网络
docker network ls
自动创建网络
docker network create -d bridge my-network
指定IP创建网络
docker network create --subnet=172.20.0.0/24 my-network
docker run -it --name app1 --network my-network centos:7 bash
在另外一个终端执行:
docker run -it --name app2 --network my-network centos:7 bash
在两个终端分别执行:
ping app1
ping app2
显示如下:
[root@b7571d01bcbd /]# ping app1
PING app1 (172.20.0.2) 56(84) bytes of data.
64 bytes from app1.my-network (172.20.0.2): icmp_seq=1 ttl=64 time=0.108 ms
64 bytes from app1.my-network (172.20.0.2): icmp_seq=2 ttl=64 time=0.098 ms
64 bytes from app1.my-network (172.20.0.2): icmp_seq=3 ttl=64 time=0.150 ms
64 bytes from app1.my-network (172.20.0.2): icmp_seq=4 ttl=64 time=0.117 ms
64 bytes from app1.my-network (172.20.0.2): icmp_seq=5 ttl=64 time=0.082 ms
准备
apache-tomcat-7.0.55.tar.gz daemon.json Dockerfile jdk-8u11-linux-x64.tar.gz key.json
#以centos7为基础,安装jdk和tomcat
FROM centos:7
#添加jdk和tomcat压缩包到指定目录,并解压缩
ADD jdk-8u11-linux-x64.tar.gz /opt/
ADD apache-tomcat-7.0.55.tar.gz /usr/
#为了方便,可以把文件夹名字改简单一点
RUN mv /usr/apache-tomcat-7.0.55 /usr/tomcat
#设置环境变量
ENV JAVA_HOME=/opt/jdk1.8.0_11 \
CATALINA_HOME=/usr/tomcat \
PATH=$PATH:/opt/jdk1.8.0_11/bin:/usr/tomcat/bin
#暴露容器8080端口
EXPOSE 8080
#设置容器启动时自动运行tomcat
ENTRYPOINT /usr/tomcat/bin/startup.sh && tail -F /usr/tomcat/logs/catalina.out
执行命令构建镜像:
docker build -t tomcat:7 .
创建成功后会提示:
Successfully tagged tomcat:7
准备存储目录
mkdir -p /opt/webapps/ROOT
mkdir /var/lib/tomcat-logs
#添加tomcat首页
vim /opt/webapps/ROOT/index.html
#添加内容
<h1> hello world~~~~<h1>
docker run -d -p 8080:8080 --name tomcat7 \
-v/opt/webapps:/usr/tomcat/webapps \
-v /var/lib/tomcat-logs:/usr/tomcat/logs tomcat:7
拉取mysql镜像
docker pull mysql:5.7
创建mysql数据卷
docker volume create mysql-volume
启动mysql容器
docker run -d --name mysql --net my-network \
--ip 172.20.0.10 -v mysql-volume:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root -p 3306:3306 \
mysql:5.7 --character-set-server=utf8
参考博文:https://blog.csdn.net/weixin_38305440/article/details/102810532