一.用容器搭建简单网站
mkdir sample
在sample中编辑如下文件Dockefile,global.comf,nginx.conf:
Dockefile:
FROM ubuntu:latest
MAINTAINER wukong "xxxx.126.com"
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD global.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
global.comf
server {
listen 0.0.0.0:80;
server_name _;
root /var/www/html/website;
index index.html index.html;
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}
nginx.conf
user www-data;
worker_processes 4;
pid /run/ngingx.pid;
daemon off;
events{ }
http{
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octer-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
}
创建一个静态网站目录
mkdir website && cd website
echo "hello docker" > index.html
创建镜像sample
cd sample
docker build -t sample .
启动镜像
docker run -d -p 80 --name website5 -v $PWD/website:/var/www/html/website sample nginx
其中,-v $PWD/website:/var/www/html/website
把宿主的目录$PWD/website映射到了容器目录/var/www/html/website;
查看启动的容器:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe165675f464 sample "nginx" 18 seconds ago Up 16 seconds 0.0.0.0:32774->80/tcp website6
浏览器中输入:http://127.0.0.1:32774/index.html
显示hello docker
修改website/index.html
echo "hello nginx" > website/index.html
刷新网页,显示hello nginx
二 .容器之间的连接
现在启动一个容器,连接刚才启动的容器website5;
启动容器
docker run --name getwebpage --link website5:website -t -i ubuntu /bin/bash
其中,–link website5:website表示创建了到容器website5的连接,website是别名;
在新启动的容器getwebpage中,执行
apt-get install wget wget
wget http://website/index.html
可以看到,目录下生成了,index.html, 内容是:hello nginx