使用Docker构建nginx静态网站

1. 建Dockerfile:

FROM ubuntu:14.04
MAINTAINER Marc LAW "[email protected]"
ENV REFRESHED_AT 2019-02-03
RUN apt-get -yqq update && apt-get -yqq install nginx
RUN mkdir -p /var/www/html/website
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

从ubuntu中拉取nginx

2. global.conf跟nginx.conf文件:

[m@localhost NginxWebSite]$ 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;
}
[m@localhost NginxWebSite]$ 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;
}

3. 构建镜像:

$ sudo docker build -t jamtur01/nginx .

4. 启动镜像:

$ sudo docker run -d -p 80 --name website -v $PWD/website:/var/www/html/website jamtur01/nginx nginx

-v 命令把宿主机的$PWD/website目录映射到容器内的nginx的html根目录.

5. 关掉selinux, 在$PWD/website里面建index.html文件, 并根据容器的映射端口(随机)测试访问吧.

猜你喜欢

转载自www.cnblogs.com/Montauk/p/10351265.html
今日推荐