首页 > 系统 > Linux >

# docker 初探02 数据共享和删除docker

2017-04-05

docker 初探02 数据共享和删除docker

# docker 初探02 数据共享和删除docker

删除容器和容器中数据

删除镜像

#查看进程

[root@#localhost ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

81a879a36bd3 wordpress "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp wordpress

a57a3cc492b7 mysql "docker-entrypoint..." About an hour ago Up About an hour 3306/tcp mysqlwp

fe9a249e1bd2 busybox "echo hello world" 2 hours ago Exited (0) 2 hours ago adoring_mclean

[root@#localhost ~]# docker stop fe9a249e1bd2

fe9a249e1bd2

[root@#localhost ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

mysql latest 9546ca122d3a 4 days ago 407 MB

wordpress latest 4ad41adc2794 12 days ago 401 MB

busybox latest 00f017a8c2a6 3 weeks ago 1.11 MB

daocloud.io/library/nginx 1.7.1 e3e043d3ed2f 2 years ago 499 MB

[root@#localhost ~]# docker rmi 00f017a8c2a6

Error response from daemon: conflict: unable to delete 00f017a8c2a6 (must be forced) - image is being used by stopped container fe9a249e1bd2

[root@#localhost ~]# docker rmi --help

Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:

-f, --force Force removal of the image

--help Print usage

--no-prune Do not delete untagged parents

[root@#localhost ~]# docker rmi -f 00f017a8c2a6

Untagged: busybox:latest

Untagged: busybox@sha256:32f093055929dbc23dec4d03e09dfe971f5973a9ca5cf059cbfb644c206aa83f

Deleted: sha256:00f017a8c2a6e1fe2ffd05c281f27d069d2a99323a8cd514dd35f228ba26d2ff

[root@#localhost ~]#

删除容器和数据

docker stop $(docker ps -a)

docker rm -v $(docker pa -aq )

在宿主机和容器之间共享数据

[root@#localhost ~]# docker run -ti -v "$PWD":/data_share ubuntu:14.04 /bin/bash

Unable to find image 'ubuntu:14.04' locally

14.04: Pulling from library/ubuntu

30d541b48fc0: Pull complete

8ecd7f80d390: Pull complete

46ec9927bb81: Pull complete

2e67a4d67b44: Pull complete

7d9dd9155488: Pull complete

Digest: sha256:62a5dce5ceccd7f1cb2672a571ebee52cad1f08eec9b57fe4965fb0968a9602e

Status: Downloaded newer image for ubuntu:14.04

root@75ee0b5c4a04:/# ls

bin boot data_share dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@75ee0b5c4a04:/# ll

total 24

drwxr-xr-x. 1 root root 60 Apr 4 08:10 ./

drwxr-xr-x. 1 root root 60 Apr 4 08:10 ../

-rwxr-xr-x. 1 root root 0 Apr 4 08:10 .dockerenv*

drwxr-xr-x. 2 root root 4096 Apr 4 08:10 bin/

drwxr-xr-x. 2 root root 6 Apr 10 2014 boot/

dr-xr-x---. 4 root root 4096 Apr 4 08:07 data_share/

drwxr-xr-x. 5 root root 360 Apr 4 08:10 dev/

drwxr-xr-x. 1 root root 62 Apr 4 08:10 etc/

drwxr-xr-x. 2 root root 6 Apr 10 2014 home/

drwxr-xr-x. 12 root root 4096 Apr 4 08:10 lib/

drwxr-xr-x. 2 root root 33 Apr 4 08:10 lib64/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 media/

drwxr-xr-x. 2 root root 6 Apr 10 2014 mnt/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 opt/

dr-xr-xr-x. 153 root root 0 Apr 4 08:10 proc/

drwx------. 2 root root 35 Apr 4 08:10 root/

drwxr-xr-x. 8 root root 4096 Feb 27 19:41 run/

drwxr-xr-x. 2 root root 4096 Apr 4 08:10 sbin/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 srv/

dr-xr-xr-x. 13 root root 0 Apr 4 05:27 sys/

drwxrwxrwt. 2 root root 6 Feb 14 23:29 tmp/

drwxr-xr-x. 10 root root 97 Apr 4 08:10 usr/

drwxr-xr-x. 11 root root 4096 Apr 4 08:10 var/

root@75ee0b5c4a04:/# cd data_share/

root@75ee0b5c4a04:/data_share/data_share# echo hi>hi

root@75ee0b5c4a04:/data_share/data_share# ll

total 8

drwxr-xr-x. 2 root root 15 Apr 4 08:11 ./

dr-xr-x---. 4 root root 4096 Apr 4 08:07 ../

-rw-r--r--. 1 root root 3 Apr 4 08:11 hi

root@75ee0b5c4a04:/data_share/data_share# more hi

hi

root@75ee0b5c4a04:/data_share/data_share# exit

exit

[root@#localhost ~]# ls

anaconda-ks.cfg data_share wordpress.backup

[root@#localhost ~]# cd data_share/

[root@#localhost data_share]# ls

hi

[root@#localhost data_share]# more hi

hi

[root@#localhost data_share]#

#看看映射情况

[root@#localhost data_share]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

75ee0b5c4a04 ubuntu:14.04 "/bin/bash" 3 minutes ago Exited (0) 2 minutes ago ecstatic_shockley

81a879a36bd3 wordpress "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp wordpress

a57a3cc492b7 mysql "docker-entrypoint..." About an hour ago Up About an hour 3306/tcp mysqlwp

fe9a249e1bd2 00f017a8c2a6 "echo hello world" 2 hours ago Exited (0) 2 hours ago adoring_mclean

[root@#localhost data_share]# docker inspect -f {{.Mounts}} 75ee0b5c4a04

[{bind /root /data_share true }]

[root@#localhost data_share]#

容器之间共享数据

方法一 :创建数据容器

#如果没有本地路径那么就是创建一个数据容器容器

[root@#localhost ~]# docker run -ti -v /data_share ubuntu:14.04 /bin/bash

[root@#localhost ~]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

7cd78727c2be ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) 13 seconds ago admiring_dijkstra

75ee0b5c4a04 ubuntu:14.04 "/bin/bash" 16 minutes ago Exited (0) 14 minutes ago ecstatic_shockley

81a879a36bd3 wordpress "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp wordpress

a57a3cc492b7 mysql "docker-entrypoint..." About an hour ago Up About an hour 3306/tcp mysqlwp

fe9a249e1bd2 00f017a8c2a6 "echo hello world" 2 hours ago Exited (0) 2 hours ago adoring_mclean

[root@#localhost ~]# docker inspect -f {{.Mounts}} 7cd78727c2be

[{volume 2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069 /var/lib/docker/volumes/2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069/_data /data_share local true }]

[root@#localhost ~]# cd /var/lib/docker/volumes/

[root@#localhost volumes]# ll

total 24

drwxr-xr-x. 3 root root 18 Apr 4 16:24 2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069

drwxr-xr-x. 3 root root 18 Apr 4 14:37 5a7d51b66fc04f38c2e54a1172716f10105cb58fe9738e406a2c1c820aed2e32

drwxr-xr-x. 3 root root 18 Apr 4 14:40 decae6a9061941110ee53691f3a89c5b9c8b67b822ff8d4d3ed94a076990ce4d

-rw-------. 1 root root 32768 Apr 4 16:24 metadata.db

[root@#localhost volumes]# cd 2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069/

[root@#localhost 2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069]# ll

total 0

drwxr-xr-x. 2 root root 16 Apr 4 16:25 _data

[root@#localhost 2293be214b9b681555139bfe63d22529f9e88105c83b2f166682b02b74173069]# cd _data/

[root@#localhost _data]# ll

total 4

-rw-r--r--. 1 root root 4 Apr 4 16:25 hi2

[root@#localhost _data]#

#在主机上修改这个文件也会同步到容器中。

#思路:先创建一个数据容器,然后再创建数据容器的时候将这个容器通过 --volumes-from 挂载其中的卷

[root@#localhost _data]# docker run -v /data_share --name data ubuntu:14.04

[root@#localhost _data]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

b7e6b0c36a81 ubuntu:14.04 "/bin/bash" 18 seconds ago Exited (0) 17 seconds ago data

7cd78727c2be ubuntu:14.04 "/bin/bash" 12 minutes ago Exited (0) 11 minutes ago admiring_dijkstra

75ee0b5c4a04 ubuntu:14.04 "/bin/bash" 27 minutes ago Exited (0) 25 minutes ago ecstatic_shockley

81a879a36bd3 wordpress "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp wordpress

a57a3cc492b7 mysql "docker-entrypoint..." About an hour ago Up About an hour 3306/tcp mysqlwp

fe9a249e1bd2 00f017a8c2a6 "echo hello world" 2 hours ago Exited (0) 2 hours ago adoring_mclean

[root@#localhost _data]# docker inspect -f {{.Mounts}} data

[{volume 23319deb05dd455dfdedcd73078e5d341a7637f0dc3fbb6e5d3b5bcf5c5fc776 /var/lib/docker/volumes/23319deb05dd455dfdedcd73078e5d341a7637f0dc3fbb6e5d3b5bcf5c5fc776/_data /data_share local true }]

[root@#localhost _data]# docker run -ti --volumes-from data ubuntu:14.04 /bin/bash

root@8e0fa83c1235:/# ls

bin boot data_share dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@8e0fa83c1235:/# cd data_share/

root@8e0fa83c1235:/data_share# ls

root@8e0fa83c1235:/data_share# ls

root@8e0fa83c1235:/data_share# echo h3>hi3

root@8e0fa83c1235:/data_share# exit

exit

[root@#localhost _data]# cd /var/lib/docker/volumes/23319deb05dd455dfdedcd73078e5d341a7637f0dc3fbb6e5d3b5bcf5c5fc776/_data /data_share

[root@#localhost _data]# ll

total 4

-rw-r--r--. 1 root root 3 Apr 4 16:39 hi3

[root@#localhost _data]# more hi3

h3

[root@#localhost _data]#

方法二: 通过宿主机中转进行复制

#启动一个容器

[root@#localhost _data]# docker run -d --name testcopy ubuntu:14.04 sleep 360

dc8a6754400fda6423014b7791bb97060fd3c889cdd60e506d41983d6a189164

[root@#localhost _data]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

dc8a6754400f ubuntu:14.04 "sleep 360" 5 seconds ago Up 3 seconds testcopy

81a879a36bd3 wordpress "docker-entrypoint..." 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp wordpress

a57a3cc492b7 mysql "docker-entrypoint..." 2 hours ago Up 2 hours 3306/tcp mysqlwp

#进入这个容器,创建一个文件

[root@#localhost _data]# docker exec -ti testcopy /bin/bash

root@dc8a6754400f:/#

root@dc8a6754400f:/#

root@dc8a6754400f:/# pwd

/

root@dc8a6754400f:/# ls

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@dc8a6754400f:/# echo copy_test > copy_test.txt

root@dc8a6754400f:/# exit

exit

#将这个文件拷贝到宿主机上

[root@#localhost _data]# docker cp testcopy:/copy_test.txt .

[root@#localhost _data]# ll

total 8

-rw-r--r--. 1 root root 10 Apr 4 16:46 copy_test.txt

-rw-r--r--. 1 root root 3 Apr 4 16:39 hi3

[root@#localhost _data]# more copy_test.txt

copy_test

[root@#localhost _data]#

将文件从宿舍机拷贝到容器中

[root@#localhost _data]# docker run -d --name testcopy ubuntu:14.04 sleep 360

88df78f77c4eb164f978aa38ce7513513c6db02d7e59ee787b839f964b6b651d

[root@#localhost _data]# docker exec -ti testcopy /bin/bash

root@88df78f77c4e:/# ls

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@88df78f77c4e:/# pwd

/

root@88df78f77c4e:/# ls

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

root@88df78f77c4e:/# exit

exit

[root@#localhost _data]# docker cp host.txt testcopy:/

[root@#localhost _data]# docker exec -ti testcopy /bin/bash

root@88df78f77c4e:/# ll

total 24

drwxr-xr-x. 1 root root 69 Apr 4 08:53 ./

drwxr-xr-x. 1 root root 69 Apr 4 08:53 ../

-rwxr-xr-x. 1 root root 0 Apr 4 08:52 .dockerenv*

drwxr-xr-x. 2 root root 4096 Apr 4 08:10 bin/

drwxr-xr-x. 2 root root 6 Apr 10 2014 boot/

drwxr-xr-x. 5 root root 340 Apr 4 08:52 dev/

drwxr-xr-x. 1 root root 62 Apr 4 08:52 etc/

drwxr-xr-x. 2 root root 6 Apr 10 2014 home/

-rw-r--r--. 1 root root 23 Apr 4 08:50 host.txt

drwxr-xr-x. 12 root root 4096 Apr 4 08:10 lib/

drwxr-xr-x. 2 root root 33 Apr 4 08:10 lib64/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 media/

drwxr-xr-x. 2 root root 6 Apr 10 2014 mnt/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 opt/

dr-xr-xr-x. 150 root root 0 Apr 4 08:52 proc/

drwx------. 1 root root 26 Apr 4 08:53 root/

drwxr-xr-x. 8 root root 4096 Feb 27 19:41 run/

drwxr-xr-x. 2 root root 4096 Apr 4 08:10 sbin/

drwxr-xr-x. 2 root root 6 Feb 14 23:28 srv/

dr-xr-xr-x. 13 root root 0 Apr 4 05:27 sys/

drwxrwxrwt. 2 root root 6 Feb 14 23:29 tmp/

drwxr-xr-x. 10 root root 97 Apr 4 08:10 usr/

drwxr-xr-x. 11 root root 4096 Apr 4 08:10 var/

root@88df78f77c4e:/#

相关文章
最新文章
热点推荐