七、Docker 容器
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源
Docker 可以让开发者打包它们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化
Docker 使用完全使用沙箱机制,两个容器之间不会有任何接口 (这个有点像 iPhone 的 app ) ,更重要的是容器性能开销极低
学习前提
在继续阅读之前,我们希望你对 Linux 有一些基本的了解,包括
1、 会使用Linux常用的命令;
2、 知道大部分的Linux常识,比如终端、service、ip、用户、组等;
3、 熟练使用Ubuntu或者Centos或者MacOS种的一种昂;
如果你对这些知识还是一知半解,可以访问我们的 Linux 基础教程 先进行一些简单的了解
Docker的应用场景
1、 Web应用的自动化打包和发布;
2、 自动化测试和持续集成、发布;
3、 在服务型环境中部署和调整数据库或其他的后台应用;
4、 从头编译或者扩展现有的OpenShift或CloudFoundry平台来搭建自己的PaaS环境;
Docker 的优点
1、 简化程序;
Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,便可以实现虚拟化
Docker 改变了虚拟化的方式,使开发者可以直接将自己的成果放入 Docker 中进行管理
方便快捷已经是 Docker 的最大优势,过去需要用数天乃至数周的 任务,在Docker容器的处理下,只需要数秒就能完成
2、 解决运维配置噩梦;
在没有 Docker 之前,每一台机器,每一个要用到的依赖,几乎都要重新配置一遍
比如新增一台 MySQL 数据库,就要从头开始配置所有环境
有了 Docker 之后,只需要从仓库里把之前的 MySQL 镜像拉出来,直接使用
3、 节省开支;
使用 Docker ,可以在一台电脑上部署多个服务,多个应用,从而充分榨干每一滴性能
同时,又不用担心多个应用之间出现相互访问的情况
相关链接
Docker 官网: http://www.docker.com
Github Docker 源码: https://github.com/docker/docker
在上一章节中我们使用 docker run
命令运行了一个 Hello World
开启了 Docker
容器的使用
每次遇到这种新的技术,我都有一个待解问题,到底有多少命令,我要怎么使用它们
那么,Docker 中到底有多少命令呢?
docker 命令
上一章节中我们所使用的所有命令都以 docker
开头,那如果直接运行 docker
命令会怎么样呢?
答案就是列出 Docker 所支持的所有命令
[root@www.hotmindshare.com ~]# docker
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/Users/luojianguo/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/Users/luojianguo/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/Users/luojianguo/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/Users/luojianguo/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
checkpoint Manage checkpoints
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
deploy Deploy a new stack or update an existing stack
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
很长的命令列表,不过只要关注最后一行就好了
大概意思是说可以通过命令 docker command –help 更深入的了解指定的 Docker 命令使用方法
例如我们要查看 docker stats 指令的具体使用方法
可以输入如下的命令
[root@www.hotmindshare.com ~]# docker stats --help
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output