1 通过模板创建镜像
- 导入镜像基本操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
https:
//openvz
.org
/Download/template/precreated
在这个网址里面下载对应的模板
[root@chy src]
[root@chy src]
sha256:3e6c83d2f3749ae4cde27673354cab305bfdc360e40d10d072f6a7dfd3edcdca
[root@chy src]
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 3e6c83d2f374 2 minutes ago 435MB
centos_with_net latest 87dde0ff7187 21 hours ago 277MB
centos latest d123f4e55e12 10 days ago 197MB
[root@chy src]
67c68340c58643e4e085dc04fe7552bad4ff6c41c612c26e9ce99e63b4872317
[root@chy src]
[root@67c68340c586 /]
[root@67c68340c586 /]
Linux 67c68340c586 3.10.0-514.el7.x86_64
|
- 导出/恢复镜像基本操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@chy ~]
[root@chy ~]
67c68340c586
[root@chy ~]
Untagged: centos7:latest
Deleted: sha256:3e6c83d2f3749ae4cde27673354cab305bfdc360e40d10d072f6a7dfd3edcdca
Deleted: sha256:788edba9eaa8ade63d8ba9d5747281c5da2b34b12a6c80f4dffd8ad9e05f68c1
[root@chy ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_with_net latest 87dde0ff7187 22 hours ago 277MB
centos latest d123f4e55e12 10 days ago 197MB
[root@chy ~]
788edba9eaa8: Loading layer [==================================================>] 446.1MB
/446
.1MB
Loaded image: centos7:latest
[root@chy ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 latest 3e6c83d2f374 38 minutes ago 435MB
centos_with_net latest 87dde0ff7187 22 hours ago 277MB
centos latest d123f4e55e12 10 days ago 197MB
|
导出镜像还有一个命令是如下
|
1
2
|
docker
export
container_id >
file
.
tar
//
导出容器,可以迁移到其他机器上,需要导入
cat
file
.
tar
|docker
import
- aming_test
//
这样会生成aming_test的镜像(导入镜像)
|
2 容器管理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@chy ~]
[root@chy ~]
438606c
[root@chy src]
[root@chy ~]
46fe5b5e5d2768727e8256e79ba6a3b3255394bda5398ec4011526fec8437dbc
[root@chy ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46fe5b5e5d27 centos_with_net
"bash"
7 seconds ago Up 5 seconds centos6
[root@chy ~]
[root@chy ~]
111
[root@chy ~]
[root@chy ~]
34effba
[root@chy ~]
46fe5b5e5d27
438606c2d465
[root@chy ~]
46fe5b5e5d27
438606c2d465
docker rmi $(docker images |
awk
'{print $3}'
|
tail
-n +2)
//
删除所有的镜像
|
3 仓库管理
- 创建私有仓库
|
1
2
|
[root@chy ~]
[root@chy ~]
|
- 将镜像上传到私有仓库
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@chy ~]
[root@chy ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.212.10:5000
/centos7
latest 3e6c83d2f374 2 hours ago 435MB
centos7 latest 3e6c83d2f374 2 hours ago 435MB
centos_with_net latest 87dde0ff7187 24 hours ago 277MB
registry latest a07e3f32a779 10 days ago 33.3MB
centos latest d123f4e55e12 10 days ago 197MB
[root@chy ~]
The push refers to a repository [192.168.212.10:5000
/centos7
]
Get https:
//192
.168.212.10:5000
/v2/
: http: server gave HTTP response to HTTPS client
解决方法如下:
[root@chy ~]
{
"insecure-registries"
:[
"192.168.212.10:5000"
] }
如上指定一个私有仓库的地址,这个地址是宿主机的地址
[root@chy ~]
[root@chy ~]
ef587be45616
[root@chy ~]
The push refers to a repository [192.168.212.10:5000
/centos7
]
788edba9eaa8: Pushed
latest: digest: sha256:d57bd79f46a10c520a62814d33a13c7c4c2b10ac143650effc9c8e35b2094565 size: 529
[root@chy ~]
{
"repositories"
:[
"centos7"
]}
|
- 怎么下载私有仓库的镜像
如果在其它机器下载私有仓库切记不要忘记指定私有仓库地址的ip。([root@chy ~]# vi /etc/docker/daemon.json //更改配置文件
{ "insecure-registries":["192.168.212.10:5000"] })
4 数据管理
- 容器是由镜像启动的,这时如果将容器关闭或者是删除那容器里面的数据去哪了呢?这时数据也会一并的消除,这样肯定是不符合我们的要求的,所以呢想到了一个方法就是将宿主机的一个目录挂载到容器中,这时容器里产生的数据都会在宿主机的目录中显示。下面就是如何将容器中的数据搞到容器中
- 挂载本地的目录到容器里
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@chy ~]
bc7f95597ceb71a3ebfcb5f4955d171b69f740c9598396eae1234966d68d1f81
[root@chy ~]
总用量 16
drwxr-xr-x 3 root root 4096 9月 2 18:38 backup
drwxr-xr-x 3 root root 4096 10月 27 06:15 gitroot
drwxr-xr-x 9 mysql1 mysql1 4096 11月 14 22:47 mariadb
drwxr-xr-x 5 root root 4096 8月 5 04:40 wwwroot
[root@chy ~]
[root@bc7f95597ceb /]
[root@bc7f95597ceb /]
[root@chy ~]
123/ backup/ gitroot/ .htpasswd mariadb/ wwwroot/
|
- 挂载数据卷
|
1
2
3
4
5
6
|
[root@chy ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc7f95597ceb centos7
"bash"
8 minutes ago Up 8 minutes sharp_kepler
ef587be45616 registry
"/entrypoint.sh /e..."
About an hour ago Up About an hour 0.0.0.0:5000->5000
/tcp
elastic_varahamihira
[root@chy ~]
12204913c07468253f58a52c52e328ff5cdb1d01866ae55e86d7c9f088a0bf2f
|
- 定义数据卷容器
有时候,我们需要多个容器之间相互共享数据,类似于linux里面的NFS,所以就可以搭建一个专门的数据卷容器,然后其他容器直接挂载该数据卷。
|
1
2
3
4
|
[root@chy ~]
这里的-
v
是有两层含义的第一层含义是: 冒号是用来交互宿主机与容器的目录的映射,第二层含义是作为数据卷容器就是不加冒号的写法,第二层的含义是用来共享一个目录,所以-
v
/data
的目录为共享目录
然后让其他容器挂载该数据卷
docker run -itd --volumes-from testvol aming123
bash
|
希望看过的童鞋多多指教,谢谢!![j_0063.gif]()
![j_0063.gif]()
本文转自我不是瘦子51CTO博客,原文链接:http://blog.51cto.com/chy940405/1981804,如需转载请自行联系原作者