├── 03-使用Docker构建服务.md ├── README.md ├── 00-Docker入门之基本命令.md ├── 01-使用Docker镜像和仓库.md ├── 04-使用Docker API.md └── 02-在测试中使用Docker.md /03-使用Docker构建服务.md: -------------------------------------------------------------------------------- 1 | # 构建 Jekyll 站点 2 | Jekyll 是基于 ruby 的博客生成工具,基于 Ubuntu 镜像进行构建。 3 | 4 | 安装 jekell 的 Dockerfile 5 | ``` 6 | FROM ubuntu 7 | MAINTAINER titushuang "ituzhi@163.com" 8 | ENV REFRESHED_AT 2015-10-12 9 | 10 | RUN apt-get -yqq update 11 | RUN apt-get -yqq install ruby ruby-dev make nodejs 12 | RUN gem install --no-rdoc --no-ri jekyll 13 | 14 | VOLUME /data 15 | VOLUME /var/www/html 16 | WORKDIR /data 17 | 18 | ENTRYPOINT [ "jekyll", "build", "--destination=/var/www/html" ] 19 | 20 | ``` 21 | 22 | 安装 apache 的 Dockerfile 23 | ``` 24 | FROM ubuntu 25 | MAINTAINER titushuang "ituzhi@163.com" 26 | ENV REFRESHED_AT 2015-10-12 27 | 28 | RUN apt-get -yqq update 29 | RUN apt-get -yqq install apache2 30 | 31 | VOLUME [ "/var/www/html" ] 32 | WORKDIR /var/www/html 33 | 34 | ENV APACHE_RUN_USER www-data 35 | ENV APACHE_RUN_GROUP www-data 36 | ENV APACHE_LOG_DIR /var/log/apache2 37 | ENV APACHE_PID_FILE /var/run/apache2.pid 38 | ENV APACHE_RUN_DIR /var/run/apache2 39 | ENV APACHE_LOCK_DIR /var/lock/apache2 40 | 41 | RUN mkdir -p $APAHCE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR 42 | 43 | EXPOSE 80 44 | 45 | ENTRYPOINT [ "/usr/sbin/apache2" ] 46 | CMD ["-D", "FOREGROUND"] 47 | ``` 48 | 49 | 构建 Jeyll 容器 50 | 51 | `sudo docker run -v /home/huangyi/Practice/Docker/apache/james_blog:/data/ --name titusblog titushuang/jekyll` 52 | 53 | 构建 Apache 容器,共享使用 Jeyll 的卷 54 | 55 | `sudo docker run -d -P --volumes-from titusblog titushuang/apache` 56 | 57 | 更新博客内容后使用下列命令重新编译 Jekyll 58 | 59 | `sudo docker start titusblog` 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker 之路 2 | 3 | ## 入门教程 4 | 5 | [00-Docker 入门之基本命令](./00-Docker入门之基本命令.md) 6 | 7 | [01-使用 Docker 镜像和仓库](./01-使用Docker镜像和仓库.md) 8 | 9 | [02-在测试中使用 Docker](./02-在测试中使用Docker.md) 10 | 11 | [03-使用 Docker 构建服务](./03-使用Docker构建服务.md) 12 | 13 | [04-使用 Docker API](https://github.com/Huangtuzhi/DockerTutorial/blob/master/04-%E4%BD%BF%E7%94%A8Docker%20API.md) 14 | 15 | ## 书籍 16 | 17 | [00-第一本 Docker 书 ](http://www.amazon.cn/%E7%AC%AC%E4%B8%80%E6%9C%ACDocker%E4%B9%A6/dp/B0151ETBYU/ref=sr_1_2?s=books&ie=UTF8&qid=1445679137&sr=1-2&keywords=%E7%AC%AC%E4%B8%80%E6%9C%ACDocker%E4%B9%A6) 18 | 19 | [01-Docker 入门实战](http://dockone.io/article/233) 20 | 21 | [02-Docker 源码分析](http://www.amazon.cn/Docker%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-%E5%AD%99%E5%AE%8F%E4%BA%AE/dp/B012ROMRUM/ref=sr_1_1?ie=UTF8&qid=1445679291&sr=8-1&keywords=Docker%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90) 22 | 23 | ## 文档 24 | 25 | [00-Docker 官方文档](https://docs.docker.com/) 26 | 27 | [01-Docker 中文社区](http://www.docker.org.cn/) 28 | 29 | [02-Docker 中文文档](https://github.com/widuu/chinese_docker) 30 | 31 | ## 工具 32 | 33 | [00-Docker 源码](https://github.com/docker/docker) 34 | 35 | [01-Docker Hub](https://hub.docker.com/) (托管服务) 36 | 37 | [02-Docker ](https://github.com/docker/compose/) (在集群中部署分布式应用) 38 | 39 | [03-Docker Machine](https://github.com/docker/machine) (简化 Docker 安装命令行工具) 40 | 41 | [04-Docker Registry](https://github.com/docker/distribution) (存储仓库) 42 | 43 | [05-Docker Swarm](https://github.com/docker/swarm) (搭建 Docker 集群) 44 | 45 | ## 镜像 46 | 47 | [00-Docker Hub 官方镜像](https://github.com/docker-library/official-images) 48 | 49 | [01-Base Image](https://github.com/phusion/baseimage-docker) 50 | 51 | [02-Busybox](https://github.com/jpetazzo/docker-busybox) 52 | 53 | [03-OpenWRT](http://www.zoobab.com/docker-openwrt-image) 54 | 55 | [04-Phusion Docker Hub Account](https://hub.docker.com/u/phusion/) 56 | 57 | [05-passenger-docker](https://github.com/phusion/passenger-docker) ( Ruby, Python, Node.js and Meteor web apps Docker 基础镜像) 58 | 59 | [06-docker-alpine](https://github.com/gliderlabs/docker-alpine) (Alpine Linux 基础镜像) 60 | 61 | [07-docker-fluentd](https://github.com/kiyoto/docker-fluentd) 62 | -------------------------------------------------------------------------------- /00-Docker入门之基本命令.md: -------------------------------------------------------------------------------- 1 | ## 安装 Docker 2 | 3 | Ubuntu/Mint 下安装 Docker 4 | 5 | `sudo apt-get install lxc-docker` 6 | 7 | 如果 Docker 启动报错: 8 | > Error loading docker apparmor profile: exec: "/sbin/apparmor_parser" 9 | 10 | 则需要先安装依赖包 11 | 12 | `sudo apt-get install apparmor` 13 | 14 | 运行Docker镜像 15 | 16 | `sudo docker run -i -t ubuntu /bin/bash` 17 | 18 | `-i: 保证容器中STDIN开启` 19 | 20 | `-t: 为容器分配一个伪tty终端` 21 | 22 | ## 基本命令 23 | 24 | 查看当前系统中容器的列表 25 | 26 | `docker ps -a` 27 | 28 | 重新打开容器会话shell 29 | 30 | `sudo docker attach titus_docker_container` 31 | 32 | exit退出容器shell后,容器会随之停止运行。若需重新运行容器,需要执行 33 | 34 | `sudo docker start titus_docker_container` 35 | `sudo docker attach titus_docker_container` 36 | 37 | 创建守护式容器,不打开shell会话,让进程打印hello world 38 | 39 | `huangyi@HP ~ % sudo docker run --name daemon_dave -d ubuntu /bin/sh -c "while true;do echo hello world; sleep 1; done"` 40 | 41 | 查看daemon容器内部日志 42 | 43 | `sudo docker logs daemon_dave` 44 | 45 | 查看daemon容器内部进程 46 | 47 | `sudo docker top daemon_dave` 48 | 49 | 查看daemon容器更多信息 50 | 51 | `sudo docker inspect daemon_dave` 52 | 53 | 查看daemon容器固定字段信息 54 | 55 | `sudo docker inspect --format='{{ .NetworkSettings.IPAddress }}' daemon_dave` 56 | 57 | docker工作目录位于/var/lib/docker/containers,目录树如下 58 | 59 | ``` 60 | ├── 0f2daad3a2347c06cea22cebd704d6d147380b4d7a14ac459a1a94c617aaf09d 61 | │ ├── 0f2daad3a2347c06cea22cebd704d6d147380b4d7a14ac459a1a94c617aaf09d-json.log 62 | │ ├── config.json 63 | │ ├── hostconfig.json 64 | │ ├── hostname 65 | │ ├── hosts 66 | │ └── resolv.conf 67 | ├── 6c2f88d0899b8041213eee0216689a4f0cd64ed31b41ca2b7d948af209e7613a 68 | │ ├── 6c2f88d0899b8041213eee0216689a4f0cd64ed31b41ca2b7d948af209e7613a-json.log 69 | │ ├── config.json 70 | │ ├── hostconfig.json 71 | │ ├── hostname 72 | │ ├── hosts 73 | │ └── resolv.conf 74 | ``` 75 | 76 | 在a6b*-json.log中可以查看daemon容器的日志 77 | 78 | ``` 79 | {"log":"hello world\n","stream":"stdout","time":"2015-10-11T11:39:26.038047014Z"} 80 | {"log":"hello world\n","stream":"stdout","time":"2015-10-11T11:39:27.039658441Z"} 81 | {"log":"hello world\n","stream":"stdout","time":"2015-10-11T11:39:28.041660087Z"} 82 | ``` 83 | 84 | ##参考 85 | 86 | [http://www.open-open.com/lib/view/open1423703640748.html](http://www.open-open.com/lib/view/open1423703640748.html) 87 | 88 | [第一本Docker书](http://www.amazon.cn/%E7%AC%AC%E4%B8%80%E6%9C%ACDocker%E4%B9%A6-%E8%A9%B9%E5%A7%86%E6%96%AF%C2%B7%E7%89%B9%E6%81%A9%E5%B8%83%E5%B0%94/dp/B00RBEIFMI/ref=sr_1_1_twi_pap_2?s=books&ie=UTF8&qid=1444566016&sr=1-1&keywords=%E7%AC%AC%E4%B8%80%E6%9C%ACdocker%E4%B9%A6) -------------------------------------------------------------------------------- /01-使用Docker镜像和仓库.md: -------------------------------------------------------------------------------- 1 | ## Docker镜像 2 | * 由文件系统叠加而成 3 | * 最底端第一层是引导文件系统bootfs,类似grub 4 | * 镜像第二层是root文件系统rootfs 5 | 6 | 列出镜像 7 | 8 | ``` 9 | huangyi@HP ~ % sudo docker images 10 | REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 11 | ubuntu latest cdd474520b8c 2 days ago 188 MB 12 | ``` 13 | 14 | 删除镜像 15 | 16 | `sudo docker rmi ubuntu` 17 | 18 | ## 镜像与容器关系 19 | 镜像与容器是一对多映射,类似于 20 | 21 | 镜像 ——> 程序 22 | 23 | 容器 ——> 进程 24 | 25 | ## 仓库 26 | 查看ubuntu仓库中其他镜像 27 | 28 | ``` 29 | HP docker # sudo docker pull ubuntu 30 | Pulling repository ubuntu 31 | c6a3582257ff: Pulling image (vivid-20150528) from ubuntu, endpoint: https://registry-1.docker.io/v1/ 32 | 5ba9dab47459: Pulling image (14.04.1) from ubuntu, endpoint: https://registry-1.docker.io/v1/ 33 | ``` 34 | 35 | ## commit 构建新镜像 36 | 37 | 创建一个要进行修改的定制容器 38 | 39 | `sudo docker run -i -t ubuntu /bin/bash` 40 | 41 | 在容器中安装vim 42 | 43 | ``` 44 | root@93a46591d393:/# sudo apt-get install vim 45 | Reading package lists... Done 46 | ``` 47 | 48 | 提交 49 | ``` 50 | HP huangyi # sudo docker commit 93a46591d393 ubuntu/myvim 51 | 3806f1faa5f007ccc756c96490d23c75fb8ede77775c3cd2b310617038157876 52 | ``` 53 | 54 | 查看本机现在的Repo,可以看见多了一个`ubuntu/myvim` 55 | 56 | ``` 57 | HP huangyi # sudo docker images 58 | REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 59 | ubuntu/myvim latest 3806f1faa5f0 2 minutes ago 231.4 MB 60 | ubuntu latest cdd474520b8c 3 days ago 188 MB 61 | ``` 62 | 63 | ##基于 Dockerfile 构建新镜像 64 | 65 | ``` 66 | HP Docker # tree 67 | . 68 | └── static_web 69 | └── Dockerfile 70 | ``` 71 | 72 | Dockerfile文件 73 | ``` 74 | # Version: 0.01 75 | FROM ubuntu 76 | MAINTAINER titushuang "ituzhi@163.com" 77 | RUN apt-get update 78 | RUN apt-get install -y nginx 79 | RUN echo 'Hi, I am in your container' \ 80 | > /usr/share/nginx/html/index.html 81 | EXPOSE 80 82 | ``` 83 | 84 | 构建镜像 85 | 86 | `sudo docker build -t="titushuang/static_web" .` 87 | 88 | 参看Repo,可以看见多了一个`titushuang/static_web` 89 | ``` 90 | huangyi@HP ~ $ sudo docker images 91 | REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 92 | titushuang/static_web latest 1f8ee6fd2bd6 5 minutes ago 227.7 MB 93 | ubuntu/myvim latest 3806f1faa5f0 29 minutes ago 231.4 MB 94 | ubuntu latest cdd474520b8c 3 days ago 188 MB 95 | ``` 96 | 97 | 上述每一条RUN语句都会生成新镜像,更新镜像ID,删除旧的镜像。 98 | 99 | 查看最终镜像 100 | 101 | ``` 102 | huangyi@HP ~ $ sudo docker run -t -i 1f8ee6fd2bd6 /bin/bash 103 | root@778aa645f23f:/# cat /usr/share/nginx/html/index.html 104 | Hi, I am in your containe 105 | ``` 106 | 107 | 可见镜像构建成功。 108 | 109 | 也可以启动容器中的Nginx。 110 | 111 | ``` 112 | sudo docker run -i -t -p 80 titus/static_web 113 | nginx -g "daemon off"; 114 | ``` 115 | 116 | -p选项将宿主机的49153端口绑定到容器的80端口,在宿主机中 117 | 118 | ``` 119 | HP static_web # curl 192.168.1.154:49153 120 | Hi, I am in your container 121 | ``` 122 | 123 | ##镜像推送到 Docker Hub 124 | 登陆 Docker Hub 125 | 126 | `sudo docker login` 127 | 128 | 推送 129 | 130 | `docker push titushuang/web` 131 | 132 | 需要特别注意的是构建时的username一定是Docker Hub中的用户名, 133 | 134 | `sudo docker build -t="titushuang/web" .` 135 | 136 | 在[Docker Hub](https://hub.docker.com/r/titushuang/web/)中可以看见推过去的repo。 137 | 138 | ##参考 139 | 140 | [http://stackoverflow.com/questions/25388684/pushing-docker-image-to-dockerhub](http://stackoverflow.com/questions/25388684/pushing-docker-image-to-dockerhub) 141 | 142 | -------------------------------------------------------------------------------- /04-使用Docker API.md: -------------------------------------------------------------------------------- 1 | Docker 生态系统中有三种 API 2 | 3 | * Registry API:提供与存储 Docker 镜像的 Docker Registry 集成的功能 4 | * Docker Hub API:提供与 Docker Hub 集成的功能 5 | * Docer Remote API:提供与 Docker 守护进程进行集成的功能 6 | 7 | 这三种 API 都是 [RESTful](http://www.ruanyifeng.com/blog/2011/09/restful) 风格的,需要着重了解的是 Remote API,主要是用来和 Docker 守护进行进行通信,进而可以远程控制镜像或者容器,就像在本机执行命令一样。 8 | 9 | ## 配置 Remote API 功能 10 | 11 | 默认情况下,Docker 进程会绑定到所在宿主机的套接字,位于 `unix:///var/run/docker.sock`。如果需要远程访问 Remote API,需要将 Docker 守护进程绑定到一个网络接口上去。 12 | 13 | 绑定到守护进程到 4243 端口 14 | 15 | `sudo docker -d -H unix:///var/run/docker.sock -H 0.0.0.0:4243` 16 | 17 | 可以在其他机器测试连接 18 | 19 | `curl http://*.*.*.*:4243/info` 20 | 21 | 使用 python 的 Json 工具格式化一下返回数据 22 | 23 | `curl http://*.*.*.*:4243/info | python -mjson.tool` 24 | 25 | 会打印和本宿主机 `sudo docker info` 一样的信息。 26 | 27 | ``` 28 | { 29 | "Containers": 44, 30 | "Debug": 0, 31 | "Driver": "aufs", 32 | "DriverStatus": [ 33 | [ 34 | "Root Dir", 35 | "/var/lib/docker/aufs" 36 | ], 37 | [ 38 | "Dirs", 39 | "151" 40 | ] 41 | ], 42 | "ExecutionDriver": "native-0.2", 43 | "IPv4Forwarding": 1, 44 | "Images": 63, 45 | "IndexServerAddress": "https://index.docker.io/v1/", 46 | "InitPath": "/usr/lib/docker.io/dockerinit", 47 | "InitSha1": "6578c4a98eb5aaa1db564782fd990839ebca1b4d", 48 | "KernelVersion": "3.13.0-24-generic", 49 | "MemoryLimit": 1, 50 | "NEventsListener": 0, 51 | "NFd": 11, 52 | "NGoroutines": 10, 53 | "SwapLimit": 1 54 | } 55 | ``` 56 | 57 | ## 通过 API 来管理 Docker 镜像 58 | 59 | 获取 Docker 守护进程中所有镜像的列表 60 | 61 | ` curl http://*.*.*.*:4243/images/json | python -mjson.tool` 62 | 63 | ``` 64 | [ 65 | { 66 | "Created": 1444616406, 67 | "Id": "1f8ee6fd2bd681727f0218f0be1e63a22ca28e1ef37a918c6f7f7d379cdb5a8d", 68 | "ParentId": "c90d37781e07f71a49d084451b990fef4830487352f8a4a8acc075d817640d7f", 69 | "RepoTags": [ 70 | "titushuang/web:mytag" 71 | ], 72 | "Size": 0, 73 | "VirtualSize": 227737551 74 | }, 75 | { 76 | "Created": 1444340548, 77 | "Id": "cdd474520b8c2c0418260fc9dcd999c84fe35bd3a36304aeb47517a290e5a5c4", 78 | "ParentId": "f03f3645bde1a45143056ad6bc9d352199f13cdb01c6d59a1c972432c9fc7e97", 79 | "RepoTags": [ 80 | "ubuntu:latest" 81 | ], 82 | "Size": 0, 83 | "VirtualSize": 187958250 84 | } 85 | ] 86 | ``` 87 | 88 | 在 Docker Hub 中查找镜像 89 | 90 | `curl "http://*.*.*.*:4243/images/search?term=redis" | python -mjson.tool` 91 | 92 | ``` 93 | [ 94 | { 95 | "description": "Redis - current stable", 96 | "is_official": false, 97 | "is_trusted": true, 98 | "name": "ruo91/redis", 99 | "star_count": 0 100 | }, 101 | { 102 | "description": "Redis Atomic App", 103 | "is_official": false, 104 | "is_trusted": true, 105 | "name": "projectatomic/redis-centos7-atomicapp", 106 | "star_count": 0 107 | } 108 | ] 109 | ``` 110 | 111 | ## 通过 API 来管理 Docker 容器 112 | 113 | 显示所有正在运行的容器 114 | 115 | `curl -s "http://*.*.*.*:4243/containers/json" | python -mjson.tool` 116 | 117 | ``` 118 | [ 119 | { 120 | "Command": "/usr/sbin/apache2 -D FOREGROUND", 121 | "Created": 1445696729, 122 | "Id": "bd7acf1a0fad1dd6f8491ef5c556153493aa913922074a00c4dabaa594b33164", 123 | "Image": "titushuang/apache:latest", 124 | "Names": [ 125 | "/focused_torvalds" 126 | ], 127 | "Ports": [ 128 | { 129 | "IP": "0.0.0.0", 130 | "PrivatePort": 80, 131 | "PublicPort": 49153, 132 | "Type": "tcp" 133 | } 134 | ], 135 | "Status": "Up 2 minutes" 136 | } 137 | ] 138 | ``` 139 | 140 | 创建一个新容器 141 | 142 | ``` 143 | curl -X POST -H "Content-Type: application/json" \ 144 | "http://202.201.13.*:4243/containers/create?name=test_remote_api" \ 145 | -d '{ 146 | "Image":"titushuang/jekyll", 147 | "Hostname":"jekyll" 148 | }' 149 | ``` 150 | 151 | 启动一个容器 152 | 153 | ``` 154 | curl -X POST -H "Content-Type: application/json" \ 155 | "http://202.201.13.45:4243/containers/d23657590b09df5c1671cd723fe2ff5cf4d89378c2f0d91a55c4e83bf6d832a6\ 156 | start" \ 157 | -d '{ 158 | "PublishAllPorts":true 159 | }' 160 | ``` 161 | 162 | 创建和启动容器加起来就提供了与 Docker run 相同的功能。 163 | 164 | 显示容器详细信息 165 | 166 | ``` 167 | curl http://202.201.13.*:4243/containers/ \ 168 | d23657590b09df5c1671cd723fe2ff5cf4d89378c2f0d91a55c4e83bf6d832a6/json | \ 169 | python -mjson.tool 170 | ``` 171 | 172 | -------------------------------------------------------------------------------- /02-在测试中使用Docker.md: -------------------------------------------------------------------------------- 1 | 前面主要是了解 Docker 的基础知识和使用方法,接下来看看怎么在实际的开发和测试中使用 Docker 这一利器。 2 | 3 | #使用 Docker 测试静态网站 4 | 5 | ## 初始 Dockerfile 6 | 新建目录 sample 和 nginx,将 nginx 配置放在 nginx 目录下面。 7 | ``` 8 | ├── sample 9 | │ ├── Dockerfile 10 | │ └── nginx 11 | │ ├── global.conf 12 | │ └── nginx.conf 13 | 14 | ``` 15 | 16 | Dockerfile 17 | 18 | ``` 19 | # Version: 0.01 20 | FROM ubuntu 21 | MAINTAINER titushuang "ituzhi@163.com" 22 | ENV REFRESHED_AT 2015-10-12 23 | RUN apt-get update 24 | RUN apt-get -y -q install nginx 25 | RUN mkdir -p /var/www/html 26 | ADD nginx/global.conf /etc/nginx/conf.d/ 27 | ADD nginx/nginx.conf /etc/nginx/nginx.conf 28 | EXPOSE 80 29 | ``` 30 | 31 | ## 构建 Sample 网站和 Nginx 镜像 32 | 33 | `sudo docker build -t titushuang/nginx .` 34 | 35 | 目录下新建文件夹 website 存放网页文件 index.html 36 | ``` 37 | . 38 | ├── Dockerfile 39 | ├── nginx 40 | │ ├── global.conf 41 | │ └── nginx.conf 42 | └── website 43 | └── index.html 44 | ``` 45 | 46 | 网页文件 47 | 48 | ``` 49 |
50 | 51 |