Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


LXC - Linux Containers

Linux 上的 (可自动安装操作系统的) Linux 虚拟机

官网解释: Userspace tools for the Linux kernel containers


docker 的目标

docker 的功能

基于 LXC 与 AUFS 的高级容器引擎.


  • 类似虚拟机: 虚拟化, 隔离性, 安全性
  • 相比虚拟机: 占用资源少, 启动快
  • 移动性: 容易迁移, 快速部署
  • 可在虚拟机上使用


  • 只能在 linux 64-bit 使用
  • guest 系统只能是 linux


# Due to a bug in LXC, Docker works best on the 3.8 kernel
# install the backported kernel
sudo apt-get update
sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring
# reboot
sudo reboot
# Add the Docker repository to your apt sources list, update and install the lxc-docker package.
curl -s https://get.docker.io/ubuntu/ | sudo sh

docker 容器不能改 /etc/hosts, 可采用的做法是建一个 dns 容器, 将其他容器的 dns (docker file) 指到该 dns 容器



# 用 docker 运行一个服务,记录 ID
CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
# 输出服务的日志
sudo docker logs $CONTAINER_ID
# attach 服务,实时查看日志
sudo docker attach --sig-proxy=false $CONTAINER_ID
# -sig-proxy=false: Do not forward signals to the container; 
#     allows us to exit the attachment using Control-C without 
#     stopping the container.
# show all running process managed by docker
sudo docker ps
# stop
sudo docker stop $CONTAINER_ID

搭建 php 环境

# 开启 docker
sudo docker  run -i -t ubuntu:12.04 /bin/bash
#   -i, --interactive=false: Keep stdin open even if not attached
#   -t, --tty=false: Allocate a pseudo-tty
# 以下就都是在 docker 里的操作了
# 增加 repos
echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu precise main" >> /etc/apt/sources.list
echo "deb http://ppa.launchpad.net/ondrej/php5/ubuntu precise main" >> /etc/apt/sources.list
echo "deb http://archive.ubuntu.com/ubuntu/ precise universe" >> /etc/apt/sources.list
echo "deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/ubuntu precise main" >> /etc/apt/sources.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C E5267A6C 0xcbcb082a1bb943db
apt-get update
# /etc/mtab – a mounted file systems table to make MySQL happy	
cat /proc/mounts > /etc/mtab
# whiptail – for displaying dialogs in the CLI via perl
# mlocate – to make locate/updatedb work (to quickly find files)
apt-get install whiptail vim mlocate
# The EMP
apt-get install nginx php5-fpm mariadb-server php5-mysqlnd
# 新开一个 terminal, 对当前状态做提交
$ docker ps
ID                  IMAGE                            COMMAND             CREATED             STATUS              PORTS
b3f5e7de5ad4        ubuntu:12.04        /bin/bash           20 minutes ago       Up 11 hours ago
$ docker commit -m "initial LEMP package install" b3f5e7de5ad4 PreciseLempFreshInstall
$ docker images
REPOSITORY                TAG                 ID                  CREATED              SIZE
ubuntu                    12.04               8dbd9e392a96        3 months ago         131.5 MB (virtual 131.5 MB)
ubuntu                    12.10               b750fe79269d        3 months ago         24.65 kB (virtual 180.1 MB)
ubuntu                    latest              8dbd9e392a96        3 months ago         131.5 MB (virtual 131.5 MB)
ubuntu                    precise             8dbd9e392a96        3 months ago         131.5 MB (virtual 131.5 MB)
ubuntu                    quantal             b750fe79269d        3 months ago         24.65 kB (virtual 180.1 MB)
PreciseLempFreshInstall   latest              b6b4d496e1bc        About a minute ago   400.7 MB (virtual 532.2 MB)


参考: Docker:利用Linux容器实现可移植的应用部署

it/linux/lxc.txt · Last modified: 2014/06/16 18:36 by admin