标签:win virt cond mes repos lis drive 新建 真实机
1. 制作自定义镜像(base基础镜像,搭建共性环境)
基于centos镜像使用commit创建新的镜像文件
基于centos镜像使用Dockerfile文件创建一个新的镜像文件
1.1 使用镜像启动容器
1)在该容器基础上修改yum源
docker_images]# docker run -it centos
345 /]# rm -rf /etc/yum.repos.d/*
345 /]# vi /etc/yum.repos.d/dvd.repo
[dvd]
name=dvd
enabled=1
gpgcheck=0
345 /]# yum clean all
345 /]# yum repolist
2)安装测试软件
345 /]# yum -y install net-tools iproute psmisc vim-enhanced
3)ifconfig查看
345 /]# ifconfig
... inet 172.17.0.2 ..
345 /]# exit
1.2 另存为另外一个镜像
1)创建新建镜像
images]# docker start 8d07ecd7e345
//可以简写为8d,要保证唯一性
images]# docker commit 8d07ecd7e345 myos:v1
2)查看新建的镜像
images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myos v1 7898cd0262c1 44 seconds ago 374.3 MB
3)验证新建镜像
images]# docker run -it myos:v1
4bf /]# ifconfig(有此命令,表示已安装)
...inet 172.17.0.3
1.3 使用Dockerfile文件创建一个新的镜像文件(根据基础镜像创建个性镜像)
Dockerfile语法格式:
– FROM:基础镜像
– MAINTAINER:镜像创建者信息(说明)
– EXPOSE:开放的端口
– ENV:设置环境变量
– ADD:复制文件到镜像
– RUN:制作镜像时执行的命令,可以有多个
– WORKDIR:定义容器默认工作目录
– CMD:容器启动时执行的命令,仅可以有一条CMD
1)创建一个Apache的镜像文件
docker1 ~]# mkdir oo
docker1 ~]# cd oo
oo]# touch Dockerfile //Dockerfile文件第一个字母要大写
oo]# cp /etc/yum.repos.d/local.repo ./
oo]# vi Dockerfile
FROM myos:v1
RUN yum -y install httpd
ENV EnvironmentFile=/etc/sysconfig/httpd
WORKDIR /var/www/html/ //定义容器默认工作目录
RUN echo "test" > /var/www/html/index.html
EXPOSE 80 //设置开放端口号
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
oo]# docker build -t myos:http .
Successfully built 2cbd819bc792
oo]# docker run -d myos:http
oo]# docker inspect 2cbd819bc792
oo]# curl 172.17.0.3
test
2.创建私有镜像仓库
Docker主机:192.168.1.20
镜像仓库服务器:192.168.1.10
2.1 自定义私有仓库
1)定义一个私有仓库
oo]# vim /etc/docker/daemon.json //不写这个文件会报错
{
"insecure-registries" : ["192.168.1.10:5000"] //使用私有仓库运行容器
}
oo]# systemctl restart docker
oo]# docker run -(it)d -p 5000:5000 registry(:latest)
oo]# curl 192.168.1.10:5000/v2/
{} //出现括号
oo]# docker tag busybox:latest 192.168.1.10:5000/busybox:latest
//打标签
oo]# docker push 192.168.1.10:5000/busybox:latest //上传
oo]# docker tag myos:http 192.168.1.10:5000/myos:http
oo]# docker push 192.168.1.10:5000/myos:http
2)在docker2上面启动
docker2 ~]# scp 192.168.1.10:/etc/docker/daemon.json /etc/docker/
docker2 ~]# systemctl restart docker
docker2 ~]# docker images
docker2 ~]# docker run -it(d) 192.168.1.10:5000/myos:http /bin/bash
//直接启动
2.2 查看私有仓库
1)查看里面有什么镜像
{"repositories":["busybox","myos"]}
2)查看里面的镜像标签
{"name":"busybox","tags":["latest"]}
{"name":"myos","tags":["http"]}
3.NFS共享存储(多个容器共享一个目录)
要求创建NFS共享,能映射到容器里:
服务器创建NFS共享存储,共享目录为/content,权限为rw
客户端挂载共享,并将共享目录映射到容器中
3.1 配置NFS服务器
真机:
]# yum -y install nfs-utils
]# mkdir /content
]# vim /etc/exports
/content/ *(rw,no_root_squash)
]# systemctl restart nfs-server.service
]# systemctl restart nfs-secure.service
]# exportfs -rv
exporting *:/content
]# chmod 777 /content
]# echo 11 > /content/index.html
3.2 配置客户端
oo]# yum -y install nfs-utils
oo]# systemctl restart nfs-server.service
oo]# showmount -e 192.168.1.254
Export list for 192.168.1.254:
/content *
docker1 ~]# mkdir /mnt/qq
docker1 ~]# mount -t nfs 192.168.1.254:/content /mnt/qq
docker1 ~]# ls /mnt/qq
index.html
docker1 ~]# cat /mnt/qq/index.html
11
docker1 ~]# docker run -d -p 80:80 -v /mnt/qq:/var/www/html -it myos:http
-v 宿主机目录:容器目录
docker1 ~]# curl 192.168.1.10
11
docker1 ~]# ll /mnt/qq/index.html
-rw-r--r-- 1 root root 3 2月 26 08:53 /mnt/qq/index.html
docker1 ~]# vim /mnt/qq/index.html
11 dddd
docker2 ~]# yum -y install nfs-utils
docker2 ~]# showmount -e 192.168.1.254
Export list for 192.168.1.254:
/content *
docker2 ~]# mkdir /mnt/qq
docker2 ~]# mount -t nfs 192.168.1.254:/content /mnt/qq
docker2 ~]# docker run -d -p 80:80 -v /mnt/qq:/var/www/html -it 192.168.1.10:5000/myos:http
docker2 ~]# curl 192.168.1.20
11 dddd
docker1 ~]# touch /mnt/qq/a.sh
docker1 ~]# echo 22 > /mnt/qq/index.html
docker2 ~]# ls /mnt/qq/
a.sh index.html
docker2 ~]# cat /mnt/qq/index.html
22
4.创建自定义网桥(虚拟交换机)
创建网桥设备docker01
设定网段为172.30.0.0/16
启动nginx容器,nginx容器桥接docker01设备
映射真实机8080端口与容器的80端口
4.1 新建Docker网络模型
1)新建docker1网络模型
docker1 ~]# yum -y install bridge-utils
--subnet= 指定子网
docker1 ~]# docker network create --subnet=172.30.0.0/16 docker01
4287a...
docker1 ~]# docker network list
NETWORK ID NAME DRIVER SCOPE
03dabb0950de bridge bridge local
4287af1f4da0 docker01 bridge local
1dc55eeaba31 host host local
eef252b83aba none null local
docker1 ~]# ip a s
docker1 ~]# docker network inspect docker01
[
{
"Name": "docker01",
"Id": "4287af1f4da036e1f91885646c33ce29f44dd08fd0b93ac07d5ccc4d9eac4bf1",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.30.0.0/16"
}
]
},
"Internal": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
2)使用自定义网桥启动容器(用同一个虚拟交换机,(docker01)启动的两个容器之间可以互相通信)
docker1 ~]# docker run --network=docker01 -id nginx
3)端口映射
(外网可以访问到容器内容,即容器部署的环境就变成真机(宿主机)环境,不同容器部署的不同环境真机可以随时调用,部署(启动就可以了,不用可以关掉)【应用程序部署和封装的核心技术】,部署各种各样的运行环境。
-p 8080(宿主机端口):80(容器端口)
docker1 ~]# docker run -p 8080:80 -id nginx
e523b386f9...
docker1 ~]# curl 192.168.1.10:8080
......
4.2 扩展实验
1)新建一个网络模型docker02
--driver bridge 交换机
docker1 ~]# docker network create --driver bridge docker02
//新建一个 名为docker02的网络模型(虚拟交换机)
5496835bd...
docker1 ~]# ifconfig
//但是在用ifconfig命令查看的时候,显示的名字并不是docker02,而是br-5496835bd3f5
br-5496835bd3f5: flags=4099 mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:89:6a:a2:72 txqueuelen 0 (Ethernet)
RX packets 8 bytes 496 (496.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 496 (496.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker1 ~]# docker network list //查看显示docker02(查看加粗字样)
NETWORK ID NAME DRIVER SCOPE
03dabb0950de bridge bridge local
4287af1f4da0 docker01 bridge local
dc316667b1df docker02 bridge local
1dc55eeaba31 host host local
eef252b83aba none null local
2)若要解决使用ifconfig命令可以看到docker02的问题,可以执行以下几步命令
docker1 ~]# docker network list //查看docker0的NETWORK ID(加粗字样)
NETWORK ID NAME DRIVER SCOPE
03dabb0950de bridge bridge local
4287af1f4da0 docker01 bridge local
dc316667b1df docker02 bridge local
1dc55eeaba31 host host local
eef252b83aba none null local
3)查看03dabb0950de的信息
[root@docker2 ~]# docker network inspect 03dabb0950de
[
{
"Name": "bridge",
"Id": "03dabb0950de905f65a4e467e1af6b78b268cc8388a08f9098f145b0573dc9e3",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Containers": {
"9601f47408ae7f218a8ded218205e77983326b06a5bb062f27ea311abd51fc6d": {
"Name": "hopeful_wing",
"EndpointID": "be2e930d737a4e0e957c11847b8e2c0ea4b48bbd0a5e2bc882a10566ff385cbe",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"fbf16a1f9477c06fedfb1a9a138f99443e058815f0b26a771b9db4fe41cba10e": {
"Name": "distracted_kowalevski",
"EndpointID": "ef15224759de94d7bcc3f347f0023eb378cc45af214125bc2c9c0ae32a3fd58c",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
4)查看图片的倒数第六行有"com.docker.network.bridge.name": "docker0"字样
5)把刚刚创建的docker02网桥删掉
docker1 ~]# docker network rm docker02 //删除docker02
docker02
docker1 ~]# docker network create \
docker02 -o com.docker.network.bridge.name=docker02
//创建docker02网桥
d721c...
docker1 ~]# ifconfig //ifconfig查看有docker02
docker02: flags=4099 mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
6)若想在创建docker03的时候自定义网段(之前已经创建过docker01和02,这里用docker03),执行以下命令
docker1 ~]# docker network create docker03 --subnet=172.40.0.0/16 -o com.docker.network.bridge.name=docker03
ab22300...
docker1 ~]# ifconfig //ifconfig查看,显示的是自己定义的网段
docker03: flags=4099 mtu 1500
inet 172.40.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
docker镜像编排是docker的核心.
**********************
虚拟化:kvm namespace(内核的三个功能)
---libvirt 单机命令管理 docker 单机容器管理
---openstack 管理集群 k8s 管理容器集群
***********************
五十四.自定义镜像及仓库、持久化存储 、 Docker网络架构
标签:win virt cond mes repos lis drive 新建 真实机
原文地址:https://www.cnblogs.com/luwei0915/p/10496456.html