返回首页 - Notes - 2016

Docker 在 Web 开发中的简易应用


构建一个基础的操作系统镜像

Dockerfile 文件的内容:

FROM daocloud.io/ubuntu:16.04
MAINTAINER Ruchee "my@ruchee.com"

ADD sources.list /etc/apt/sources.list
RUN apt-get update

sources.list 文件的内容:

deb http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ xenial-backports main restricted universe multiverse

构建命令:docker build -t ruchee/ubuntu .


构建一个服务器镜像

Dockerfile 文件的内容:

FROM ruchee/ubuntu
MAINTAINER Ruchee "my@ruchee.com"

RUN apt-get install -y nginx
RUN mkdir -p /var/www/html

ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

nginx/global.confnginx/nginx.conf 等服务器配置文件自己在构建目录下准备好

构建命令:docker build -t ruchee/nginx .


创建容器并启动

  1. docker stop website
  2. docker rm website
  3. docker run --name website -d -p 8082:80 -v $PWD/website:/var/www/html/website ruchee/nginx nginx

website 目录下新建一个 index.html 文件,里面随便写些东西

使用 boot2docker ip 命令得到容器的访问 IP,用浏览器访问 http://该IP:8082,应该就会看到上述 index.html 的显示效果了

后续修改 website 目录下的文件都会即时生效,不需要对容器做任何改动


date:2016-05-23