One - One Code All

Blog Content

Docker初步

Linux-Mac 容器化 运维   2014-03-15 23:03:57

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。


一个完整的Docker有以下几个部分组成:

dockerClient客户端

Docker Daemon守护进程

Docker Image镜像

DockerContainer容器


Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。 


Docker采用 C/S架构 Docker daemon 作为服务端接受来自客户的请求,并处理这些请求(创建、运行、分发容器)。 客户端和服务端既可以运行在一个机器上,也可通过 socket 或者RESTful API 来进行通信。

Docker daemon 一般在宿主主机后台运行,等待接收来自客户端的消息。 Docker 客户端则为用户提供一系列可执行命令,用户用这些命令实现跟 Docker daemon 交互。


在docker的网站上提到了docker的典型场景:

Automating the packaging and deployment of applications(使应用的打包与部署自动化)

Creation of lightweight, private PAAS environments(创建轻量、私密的PAAS环境)

Automated testing and continuous integration/deployment(实现自动化测试和持续的集成/部署)

Deploying and scaling web apps, databases and backend services(部署与扩展webapp、数据库和后台服务)


安装:yum -y install docker 或者 curl -fsSL https://get.docker.com/ | sh 

启动:service docker start

测试:docker run hello-world

查看容器启动情况:docker ps

安装镜像的两种方式:通过 Dockerfile 构建 ; docker pull php



docker上安装应用:

安装nginx:

创建目录:mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

安装nginx: docker pull nginx

使用nginx镜像,运行容器:docker run -p 80:80 --name mynginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx  



安装PHP:

创建目录:mkdir -p ~/php-fpm/logs ~/php-fpm/conf

安装php: docker  pull php:5.3-fpm

使用nginx镜像,运行容器: docker run -p 9000:9000 --name  myphp-fpm -v ~/nginx/www:/www -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs   -d php:5.3-fpm




下一篇:虚拟机与docker容器区别

The minute you think of giving up, think of the reason why you held on so long.