[Docker系列·2]搭建基於Docker的Nginx伺服器

六翁發表於2017-01-04

docker命令別名

~/.bashrc

# .bashrc

alias d="sudo docker”

docker的nginx工作目錄

/home/erichan/d/nginx

docker的Dockerfile

# Version: 0.0.1
FROM feuyeux/ssd
MAINTAINER Eric Han "feuyeux@gmail.com"
RUN apt-get update
RUN apt-get -yq install 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配置檔案

nginx/global.conf

[erichan@localhost nginx]$ cat nginx/global.conf

server {
        listen          0.0.0.0:80;
    server_name     _;
    root            /var/www/html/website;
    index           index.html index.htm;
    access_log /var/log/nginx/default_access.log;
    error_log /var/log/nginx/default_error.log;
}

nginx/nginx.conf

[erichan@localhost nginx]$ cat nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events { }
http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-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;
}

測試頁面

[erichan@localhost nginx]$ cat website/index.html
    <head>
      <title>Test website</title>
    </head>
    <body>
      <h1>This is a test website</h1>
      <p>learning The docker book.</p>
    </body>

啟動nginx伺服器

d run -d -p 80 --name website -v /home/erichan/d/nginx/website:/var/www/html/website feuyeux/nginx:1.0 nginx

檢視nginx程式

d ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                           NAMES
b92b30ce55b6        feuyeux/nginx:1.0   nginx               4 minutes ago       Up 4 minutes        22/tcp, 0.0.0.0:49153->80/tcp   website    

測試Nginx

[erichan@localhost nginx]$ curl http://localhost:49153
<head>
<title>Test website</title>
</head>
<body>
<h1>This is a test website</h1>    
</body>

修改本地檔案

[erichan@localhost nginx]$ nano /home/erichan/d/nginx/website/index.html 
[erichan@localhost nginx]$ cat /home/erichan/d/nginx/website/index.html 
<head>
  <title>Test website</title>
</head>
<body>
  <h1>This is a test website</h1>
  <p>I`m learning The docker book.</p>
</body>

測試Docker·Nginx

[erichan@localhost nginx]$ curl http://localhost:49153
<head>
  <title>Test website</title>
</head>
<body>
  <h1>This is a test website</h1>
  <p>I`m learning The docker book.</p>
</body>

停止程式並刪除容器

d kill $(d ps -q) && d rm $(d ps -a -q)

本文是《The Docker Book》的閱讀筆記

六翁


相關文章