[Docker系列·2] 搭建基于Docker的Nginx服务器

    xiaoxiao2025-11-27  14

    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》的阅读笔记

    六翁

    最新回复(0)