[关闭]
@ValenW 2016-05-08T09:16:05.000000Z 字数 1944 阅读 1252

Docker学习笔记

列表项

学习 博客 作业


Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。 项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,项目代码在 GitHub 上进行维护。
下面的图片比较了 Docker 和传统虚拟化方式的不同之处,可见容器是在操作系统层面上实现虚拟化,直接复用本地主机的操作系统,而传统方式则是在硬件层面实现。
传统VM与Docker对比
传统VM与Docker对比2

两者对比

特性 容器 虚拟机
启动 秒级 分钟级
硬盘使用 一般为 MB 一般为 GB
性能 接近原生 弱于
系统支持量 单机支持上千个容器 一般几十个

Ubuntu安装Docker

  1. $ curl -fsSL https://get.docker.com/ | sh
  2. $ curl -fsSL https://get.docker.com/gpg | sudo apt-key add -

然后执行命令$ docker run hello-world如果输出如下,表示安装成功。

  1. Unable to find image 'hello-world:latest' locally
  2. latest: Pulling from library/hello-world
  3. 535020c3e8ad: Pull complete
  4. af340544ed62: Pull complete
  5. Digest: sha256:a68868bfe696c00866942e8f5ca39e3e31b79c1e50feaee4ce5e28df2f051d5c
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker.
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10. 1. The Docker Engine CLI client contacted the Docker Engine daemon.
  11. 2. The Docker Engine daemon pulled the "hello-world" image from the Docker Hub.
  12. 3. The Docker Engine daemon created a new container from that image which runs the
  13. executable that produces the output you are currently reading.
  14. 4. The Docker Engine daemon streamed that output to the Docker Engine CLI client, which sent it
  15. to your terminal.
  16. To try something more ambitious, you can run an Ubuntu container with:
  17. $ docker run -it ubuntu bash
  18. Share images, automate workflows, and more with a free Docker Hub account:
  19. https://hub.docker.com
  20. For more examples and ideas, visit:
  21. https://docs.docker.com/userguide/

获取镜像

使用命令

  1. $ sudo docker pull ubuntu:12.04

就可以从Docker远程服务器中拉取ubuntu 12.04版本的镜像。

  1. $ sudo docker run -t -i ubuntu:14.04 /bin/bash

就可以进入ubuntu镜像的命令行了。

在镜像中部署应用

配置环境,以Node.js为例:

  1. apt-get install python-software-properties python g++ make
  2. add-apt-repository ppa:chris-lea/node.js
  3. apt-get update
  4. apt-get install nodejs npm

安装express

  1. npm install express -gd

初始化网站目录

  1. cd /mnt/
  2. express nodeserver
  3. cd nodeserver
  4. npm install

然后得编辑下 app.js

  1. vim app.js

先把默认端口号3000改为80

  1. app.set('port', process.env.PORT || 80);

再注释掉以下代码

  1. app.get('/', routes.index);
  2. app.get('/users', user.list);

然后用forever实现后台启动nodejs

  1. npm install forever
  2. forever start -w app.js

这样服务就启动了,可以用ip访问。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注