标签:比较 数据持久化 version creat 常见 停止 apt 运行时 cannot
仓库(registry)--repo(Repository)-->镜像(按版本区分)
yum库-->不同repo-->软件(按名字区分)
Docker是一个c/s架构的系统,Docker守护进程运行在主机上,然后通过Socket连接从客户端访问,守护进程从客户端接收命令并管理运行在主机上帝额容器。容器,是一个运行环境,就是我们前面说的集装箱。
使用centos镜像以后台模式启动一个容器
? docker run -d centos
然后执行:
? docker ps -a 进行查看,发现容器已经退出
很重要的说明一点问题:
? Docker容器后台运行,就必须要有一个前台进程
? 容器命令如果不是那些一直挂起的命令(比如top,tail) ,就会自动退出
? 这是docker的一个机制,比如web容器,我们以nginx为例,正常情况下,我们配置服务器服务只要启动响应的serveice 即可,例如 : service nginx start
? 但是这样做,nginx 为后台进程模式,就会导致docker前台没有运行的应用。
? 这样容器后台启动后,会立刻自杀因为他觉得没事可做
? 所以最佳的解决办法就是将你要运行的程序以前台进程的形式运行
docker images
[root@qzk ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b 11 days ago 126MB
mysql 5.6 732765f8c7d2 13 days ago 257MB
mysql <none> 3ed1080b793f 2 months ago 256MB
alpine 3.8 dac705114996 5 months ago 4.41MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
redis 3.2 87856cc39862 10 months ago 76MB
# REPOSITORY:镜像仓库源
# TAG:镜像标签
# IMAGE ID:镜像ID
# CREATED:镜像创建时间
# SIZE:镜像大小
# 同一个仓库源可以由多个Tag,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG来定义不同的镜像
[root@qzk ~]# docker images -qa
5a3221f0137b
732765f8c7d2
3ed1080b793f
dac705114996
fce289e99eb9
87856cc39862
常见参数:
docker search:搜索镜像
查询的网站是:https://hub.docker.com
docker search tomcat
[root@qzk ~]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2503 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 66 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 29 [OK]
kubeguide/tomcat-app Tomcat image for Chapter 1 27
docker pull(拉取镜像):
docker rmi:删除某个镜像
[root@qzk ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 6 days ago 202MB
nginx latest 5a3221f0137b 11 days ago 126MB
mysql 5.6 732765f8c7d2 13 days ago 257MB
mysql <none> 3ed1080b793f 2 months ago 256MB
alpine 3.8 dac705114996 5 months ago 4.41MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
redis 3.2 87856cc39862 10 months ago 76MB
[root@qzk ~]# docker run -it 67fa590cfc1c
[root@de1acb46c61f /]# ls # 此时已经进入 服务器的docker里面由centos镜像生成一个centos中跑命令
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@de1acb46c61f /]# pwd # 查看当前位置 在docker中的centos的根目录下
/
# docker ps 查看当前正在运行的容器实例
[root@qzk ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 6 days ago 202MB
nginx latest 5a3221f0137b 11 days ago 126MB
mysql 5.6 732765f8c7d2 13 days ago 257MB
mysql <none> 3ed1080b793f 2 months ago 256MB
alpine 3.8 dac705114996 5 months ago 4.41MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
redis 3.2 87856cc39862 10 months ago 76MB
[root@qzk ~]# docker run -d 5a3221f0137b
85b03f2f534fb9481ff94550b87a1914a7252d702377afb374941737b6ac5740
[root@qzk ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 8 seconds ago Up 8 seconds 80/tcp suspicious_darwin
[root@qzk ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 6 days ago 202MB
nginx latest 5a3221f0137b 11 days ago 126MB
mysql 5.6 732765f8c7d2 13 days ago 257MB
mysql <none> 3ed1080b793f 2 months ago 256MB
alpine 3.8 dac705114996 5 months ago 4.41MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
redis 3.2 87856cc39862 10 months ago 76MB
[root@qzk ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 80/tcp suspicious_darwin
[root@qzk ~]# dcoker ps -n3
-bash: dcoker: command not found
[root@qzk ~]# docker ps -n3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp suspicious_darwin
de1acb46c61f 67fa590cfc1c "/bin/bash" 15 minutes ago Exited (127) 12 minutes ago vigilant_banzai
47bfa51bd6ab alpine:3.8 "/bin/sh" 4 hours ago Exited (130) 4 hours ago exciting_nash
[root@qzk ~]# docker ps -lq
85b03f2f534f
[root@qzk ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp suspicious_darwin
de1acb46c61f 67fa590cfc1c "/bin/bash" 15 minutes ago Exited (127) 12 minutes ago vigilant_banzai
47bfa51bd6ab alpine:3.8 "/bin/sh" 4 hours ago Exited (130) 4 hours ago exciting_nash
f4525af85b89 mysql:5.6 "docker-entrypoint.s…" 24 hours ago Exited (0) 3
......
退出容器:
docker start 容器ID:启动容器
docker stop 容器ID:停止容器
docker restart 容器ID :重启容器
docker kill 容器ID:强制停止容器
docker rm 容器ID:删除容器
一次删除多个容器:
[root@qzk ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp suspicious_darwin
[root@qzk ~]# docker restart 85b03f2f534f # 重启容器
85b03f2f534f
[root@qzk ~]# docker ps # 查看当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 18 minutes ago Up 4 seconds 80/tcp suspicious_darwin
[root@qzk ~]# docker kill 85b03f2f534f # 强制停止该容器
85b03f2f534f
[root@qzk ~]# docker ps # 查看当前值横在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@qzk ~]# docker ps -l # 查看上一次运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 22 minutes ago Exited (137) About a minute ago suspicious_darwin
[root@qzk ~]# docker ps -n2 # 查看上两次运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b03f2f534f 5a3221f0137b "nginx -g 'daemon of…" 22 minutes ago Exited (137) About a minute ago suspicious_darwin
de1acb46c61f 67fa590cfc1c "/bin/bash" 26 minutes ago Exited (127) 23 minutes ago vigilant_banzai
[root@qzk ~]# docker ps # 查看当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
查看容器日志:docker logs -f -t --tail 容器ID
查看容器内的进程:docker top 容器ID
查看容器内部的细节:docker inspect 容器ID -->返回json字符串形式的容器细节
进入正在运行的容器并以命令行交互:docker attach 容器ID
这个命令是指进入还在运行的docker容器中的
[root@qzk ~]# docker exec -t f555a0797343 ls -l /tmp # -> exec 在容器中执行,并不影响容器进程运行
total 4
-rwx------ 1 root root 836 Aug 1 01:10 ks-script-rnBCJB
-rw------- 1 root root 0 Aug 1 01:09 yum.log
[root@qzk ~]# docker attach f555a0797343
[root@f555a0797343 /]# ls -l /tmp
total 4
-rwx------ 1 root root 836 Aug 1 01:10 ks-script-rnBCJB
-rw------- 1 root root 0 Aug 1 01:09 yum.log
从容器内拷贝文件的到宿主机上:docker cp 容器ID:容器内路径 目的主机路径
[root@f555a0797343 /]# cd /tmp/
[root@f555a0797343 tmp]# ls
ks-script-rnBCJB yum.log
[root@f555a0797343 tmp]# read escape sequence
[root@qzk ~]# docker cp f555a0797343:/tmp/yum.log /root
[root@qzk ~]# ll
total 40
drwxr-xr-x 2 root root 4096 Jul 17 00:06 conf
drwxr-xr-x 4 polkitd input 4096 Jul 17 00:06 data
-rw-r--r-- 1 root root 13185 Jul 16 23:53 get-docker.sh
drwxr-xr-x 2 root root 4096 Jul 17 00:06 logs
drwxr-xr-x 5 root root 4096 Aug 26 20:03 mysql
drwxr-xr-x 5 root root 4096 Aug 20 17:27 nginx
drwxr-xr-x 3 root root 4096 Jul 17 08:42 redis
-rw------- 1 root root 0 Aug 1 09:09 yum.log
是什么:
镜像是一种轻量级的、可独立执行的软件包,用来打包软件的额运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容包括代码,运行时的库,环境变量和配置文件
联合文件系统(UnionFS):镜像是联合文件系统的缩写
docker镜像加载原理
分层的镜像
问题:为什么Docker镜像要采用分层结构呢?
问题:镜像有哪些特点:
Docker的容器数据卷类似于Redis里面的rdb和aof文件(redis是分布式内存数据库,其数据持久化依赖于rdb和aof文件)
[root@qzk ~]# mkdir mydocker
[root@qzk ~]# cd mydocker/
[root@qzk mydocker]# vim Dockerfile
[root@qzk mydocker]# pwd
/root/mydocker
[root@qzk mydocker]# ll
total 4
-rw-r--r-- 1 root root 137 Aug 27 23:41 Dockerfile
[root@qzk mydocker]# docker build -f /root/mydocker/Dockerfile -t qzk/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : From centos
---> 67fa590cfc1c
Step 2/4 : VOLUME ["/dataVolumeContainer1","/dataValumeContainer2"]
---> Running in 18b409b1e5ad
Removing intermediate container 18b409b1e5ad
---> d097de0ae30d
Step 3/4 : CMD echo "finished,-----------success1"
---> Running in 458efc00f43a
Removing intermediate container 458efc00f43a
---> 809d0617b45f
Step 4/4 : CMD /bin/bash
---> Running in 5d39aeb36569
Removing intermediate container 5d39aeb36569
---> 533e43312b70
Successfully built 533e43312b70
Successfully tagged qzk/centos:latest
[root@qzk mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
qzk/centos latest 533e43312b70 14 seconds ago 202MB
centos latest 67fa590cfc1c 6 days ago 202MB
nginx latest 5a3221f0137b 11 days ago 126MB
mysql 5.6 732765f8c7d2 13 days ago 257MB
mysql <none> 3ed1080b793f 2 months ago 256MB
alpine 3.8 dac705114996 5 months ago 4.41MB
hello-world latest fce289e99eb9 7 months ago 1.84kB
redis 3.2 87856cc39862 10 months ago 76MB
[root@qzk mydocker]# docker run -it qzk/centos
[root@e1aa4179b889 /]# ll
total 64
-rw-r--r-- 1 root root 12090 Aug 1 01:10 anaconda-post.log
lrwxrwxrwx 1 root root 7 Aug 1 01:09 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Aug 27 15:51 dataValumeContainer2
drwxr-xr-x 2 root root 4096 Aug 27 15:51 dataVolumeContainer1
# Dockerfiles.txt
# volume test
From centos
VOLUME ["/dataVolumeContainer1","/dataValumeContainer2"] # 挂载两个数据卷
CMD echo "finished,-----------success1"
CMD /bin/bash
此时即完成数据卷的添加且绑定到宿主机对应的文件夹
},
"Mounts": [
{
"Type": "volume",
"Name": "2093233e62fd1861cd936c2f24b2aeed7938660679fbb4ef7410b4a55c1a8315",
"Source": "/var/lib/docker/volumes/2093233e62fd1861cd936c2f24b2aeed7938660679fbb4ef7410b4a55c1a8315/_data",
"Destination": "/dataValumeContainer2",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "2f2eae1d07d01dc5a1e26ff0388321dd0a51e19a1ba7ba04d89a4470caeed38c",
"Source": "/var/lib/docker/volumes/2f2eae1d07d01dc5a1e26ff0388321dd0a51e19a1ba7ba04d89a4470caeed38c/_data", # 对应宿主机的文件夹
"Destination": "/dataVolumeContainer1",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
命名的容器挂载数据卷,其他容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器
root@a85b1adc42b2 dataVolumeContainer1]# [root@qzk _data]#
[root@qzk _data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
147e8ecfb961 qzk/centos "/bin/sh -c /bin/bash" About a minute ago Up About a minute dc02
159a20092c5b qzk/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes dc01
f4525af85b89 mysql:5.6 "docker-entrypoint.s…" 28 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp qzkmysql
d2c751fb7ea8 nginx "nginx -g 'daemon of…" 7 days ago Up 4 hours 0.0.0.0:8081->80/tcp docker_nginx
[root@qzk _data]# ls
test.txt
[root@qzk _data]# ls
test.txt
[root@qzk _data]# cd
[root@qzk ~]# docker attach dc01
[root@159a20092c5b /]# cd /dataVolumeContainer1/
[root@159a20092c5b dataVolumeContainer1]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 27 16:19 1.txt
[root@159a20092c5b dataVolumeContainer1]# read escape sequence
[root@qzk ~]# docker attach dc02
[root@147e8ecfb961 dataVolumeContainer1]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 27 16:19 1.txt
[root@qzk _data]# ls
test.txt
[root@qzk _data]# cd ..
[root@qzk 2f2eae1d07d01dc5a1e26ff0388321dd0a51e19a1ba7ba04d89a4470caeed38c]# cd ..
[root@qzk volumes]# ls
107e09635eb15fe000de2e3cdb291b05d44156f9bd5bfb39c84b926a75b37197
1cd5cb6d336ea6c0915f92d5b99ed9522f1878526e91990636c308c284c61a79
2093233e62fd1861cd936c2f24b2aeed7938660679fbb4ef7410b4a55c1a8315
2f2eae1d07d01dc5a1e26ff0388321dd0a51e19a1ba7ba04d89a4470caeed38c
43267e6f36e88653976d1547f9aff74b2a293c182f2a20069b511b9f738d5ecf
adf312b8b092ca8a2493579a3ea850f9da7c778f7ffdbd67678b17c313701a35
c0979a3fc3137ff4612337fea71f8d604f03e88e5c874227deed99fcc53e5d95
metadata.db
标签:比较 数据持久化 version creat 常见 停止 apt 运行时 cannot
原文地址:https://www.cnblogs.com/qianzhengkai/p/11421602.html