Docker的概念及基本用法
Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源。Docker提供了一种在安全、可重复的环境中自动部署软件的方式。
关于Docker的架构,查看官方介绍。
Docker致力于提供:
1. 简单、轻量的建模方式
2. 开发运维职责的逻辑分离,降低开发与生产环境的不一致性。
3. 快速高效的开发生命周期
4. 面向服务的架构,推荐单个容器只运行一个应用程序或进程
Docker组件
Docker核心组件:
Docker客户端和服务器
Docker镜像
Registry
Docker容器
Docker技术组件:
1) libcontainer,一个原生的linux 容器格式
2) Linux 内核的命名空间(namespace),用户隔离文件系统、进程和网络
Docker安装和启动
1 安装
如下安装适用于Redhat操作系统。
安装方式有两种:使用yum在线安装,或者使用安装包安装。
使用yum方式安装
1 //1 更新yum 安装包index 2 $ sudo yum makecache fast 3 4 //2. 安装docker最新版本 5 $ sudo yum -y install docker-engine 6 7 // 或者安装特定版本 8 $ yum list docker-engine.x86_64 --showduplicates |sort -r 9 $ sudo yum -y install docker-engine-<VERSION_STRING>
使用安装包安装
1 下载对应操作系统版本的包,比如对应RHCE的rpm包 2 安装 $ sudo yum install package.rpm
2 Docker启动
$ sudo systemctl start docker
验证Docker是否安装成功
$ sudo docker run hello-world
使用docker version查看版本
更多参考官方文档。
基本命令
1 查看Docker所有的命令
docker
Usage: docker COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/Users/liuchun/.docker") -D, --debug Enable debug mode --help Print usage -H, --host list Daemon socket(s) to connect to (default []) -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/liuchun/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/Users/liuchun/.docker/cert.pem") --tlskey string Path to TLS key file (default "/Users/liuchun/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: checkpoint Manage checkpoints container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker volume Manage volumes Commands: attach Attach 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 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.
2 查看指定命令的帮助
docker command --help
,例如启动命令docker run --help
Docker配置服务
RHCE7 参考https://docs.docker.com/engine/admin/
运行一条命令的example
# docker run busybox echo "Hello, this is new msg" # docker ps //查看容器 # docker ps -a // 查看到已经停止的容器
3 容器管理
docker run: 命令用来创建运行容器
docker ps:
用来查看正在运行的容器列表
几个最常用的参数:
-a
:查看所有容器,含停止运行的-l
:查看刚启动的容器-q
:只显示容器ID
docker start: 启动容器
我们可以通过 docker attach
连接到某个正在运行的container。
docker stop: 停止容器
docker inspect: 查看Docker容器或镜像的一些内部信息
docker rm: 删除容器操作,默认不可以删除运行的容器,但提供了强制删除的参数-f
docker top: 查看容器中运行的进程信息,显示容器中进程的PID,UID,PPID,时间,tty等信息。
4 镜像管理
docker images: 查看镜像列表
其中:
- REPOSITORY:仓库名称
- TAG:标签名,一个仓库可以有若干个标签对应不同的镜像,默认都是
latest
- IMAGE ID:镜像ID
- CREATED:创建时间,注意不是本地的pull时间
- SIZE:镜像大小
docker pull: 获取镜像, 从Docker Hub上pull某一个镜像
创建镜像: 最常用的是写一个Dockerfile,从Dockerfile里创建新的镜像。
清理镜像:使用docker rmi来删除镜像,删除镜像前要用docker rm删除对应的container