Docker alpine构建nginx
1,pull一个基础镜像
至于如何pull自己的镜像和构建镜像可以参考Docker Hub简单使用也可以pull本章中的docker hub的镜像
[root@gitlab conf]# docker pull marksugar/alpine
Using default tag: latest
latest: Pulling from marksugar/alpine
e110a4a17941: Already exists
92c6475c18dd: Pull complete
Digest: sha256:5ae1b32053247f3d251fe5ee56aa817d330cb007603272985a0aab0b79a4c9b3
Status: Downloaded newer image for marksugar/alpine:latest
2,Dockerfile
FROM marksugar/alpine
MAINTAINER wwww.111cn.net
RUN addgroup -g 499 -S nginx \
&& adduser -HDu 499 -s /sbin/nologin -g 'web server' -G nginx nginx \
&& cd /usr/local \
&& curl -sO http://nginx.org/download/nginx-1.10.1.tar.gz \
&& tar xf nginx-1.10.1.tar.gz \
&& rm -rf nginx-1.10.1.tar.gz \
&& cd nginx-1.10.1 \
&& apk --update --no-cache add geoip geoip-dev pcre libxslt gd openssl-dev pcre-dev zlib-dev build-base linux-headers libxslt-dev gd-dev openssl-dev libstdc++ libgcc patch logrotate supervisor inotify-tools && rm -rf /var/cache/apk/*
RUN RUN ./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
&& make && make install \
&& mkdir -p /var/tmp/nginx/{client,fastcgi,proxy,uwsgi} \
&& echo "daemon off;" >> /etc/nginx/ngnix.conf \
&& apk del wget
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]
3,build
docker build -t marksugar/nginx Dockerfile
4,run起来
[root@gitlab apline-nginx]# docker run --name mynginx -d -p 81:80 marksugar/nginx
e2b7783eacde80337801a1e1c5d0962abd0b2770e820b9523501682716b6b817
[root@gitlab apline-nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2b7783eacde marksugar/nginx "/usr/local/nginx/sbi" 2 seconds ago Up 1 seconds 0.0.0.0:81->80/tcp mynginx
b889bfd7a6ee marksugar/nginx:8.33 "/bin/bash" 19 hours ago Up 4 hours 0.0.0.0:80->80/tcp nginx
[root@gitlab apline-nginx]#
这时你会看到一个欢迎页面
nginx.png
1,Dockerfile
Docker挂载数据卷,可参考Docker网络和数据卷
挂载nginx配置文件和根目录
FROM marksugar/alpine
MAINTAINER wwww.111cn.net
RUN addgroup -g 499 -S nginx \
&& adduser -HDu 499 -s /sbin/nologin -g 'web server' -G nginx nginx \
&& cd /usr/local \
&& curl -sO http://nginx.org/download/nginx-1.10.1.tar.gz \
&& tar xf nginx-1.10.1.tar.gz \
&& rm -rf nginx-1.10.1.tar.gz \
&& apk --update --no-cache add geoip geoip-dev pcre libxslt gd openssl-dev \
pcre-dev zlib-dev build-base linux-headers libxslt-dev gd-dev openssl-dev \
libstdc++ libgcc patch logrotate supervisor inotify-tools
WORKDIR /usr/local/nginx-1.10.1
RUN ./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_mp4_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
&& make && make install \
&& mkdir -p /var/tmp/nginx/{client,fastcgi,proxy,uwsgi} \
&& echo "daemon off;" >> /etc/nginx/nginx.conf \
&& apk del wget \
&& rm -rf /usr/local/nginx-1.10.1 \
&& rm -rf /var/cache/apk/* \
&& rm -rf /etc/nginx
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]
2,挂载配置文件
run起来并且挂载nginx配置文件以及配置文件中定义的目录也被挂载
-v /data/nginx/conf:/etc/nginx/
-v /data/nginx/wwwlog:/data/nginx/wwwlog/
-v /data/nginx/wwwroot:/data/nginx/wwwroot/
这三个路径分别是将本地的conf文件映射到容器中nginx配置文件
[root@gitlab data]# tree /data/
/data/
└── nginx
├── conf
│ ├── conf.d
│ ├── default.d
│ ├── fastcgi.conf
│ ├── fastcgi_params
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── nginx.conf
│ ├── nginx.tar.gz
│ ├── scgi_params
│ ├── uwsgi_params
│ ├── vhost
│ │ └── www.conf
│ └── win-utf
├── wwwlog
│ ├── error_nginx.log
│ └── www.com.log
└── wwwroot
└── index.html
7 directories, 14 files
[root@gitlab data]#
[root@gitlab apline-nginx]# docker run --name mynginx -d -p 81:80 -v /data/nginx/conf:/etc/nginx/ -v /data/nginx/wwwlog:/data/nginx/wwwlog/ -v /data/nginx/wwwroot:/data/nginx/wwwroot marksugar/nginx
240c993352560f76a90d493d3f00253472c3e55858e02b345ad9c66fb40c4001
[root@gitlab apline-nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
240c99335256 marksugar/nginx "/usr/local/nginx/sbi" 23 minutes ago Up 23 minutes 0.0.0.0:81->80/tcp mynginx
3,nginx配置文件
nginx主配置文件基本一致,如下:
[root@gitlab conf]# cat nginx.conf
user nginx nginx;
error_log /data/nginx/wwwlog/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "msie6";
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
log_format upstream2 '$proxy_add_x_forwarded_for $remote_user [$time_local] "$request" $http_host'
'$body_bytes_sent "$http_referer" "$http_user_agent" $ssl_protocol $ssl_cipher'
'$request_time [$status] [$upstream_status] [$upstream_response_time] "$upstream_addr"';
###########################################################################################
include vhost/*.conf;
}
daemon off;
[root@gitlab conf]#
include vhost/www.conf
[root@gitlab vhost]# cat www.conf
server {
listen 80;
server_name localhost;
location / {
root /data/nginx/wwwroot;
index index.php index.html index.htm;
}
# location ~ .*\. (php|php5.3.27)?$ {
# root html/blog;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi.conf;
# }
access_log /data/nginx/wwwlog/www.com.log;
}
[root@gitlab vhost]#
index.html
[root@gitlab wwwroot]# cat /data/nginx/wwwroot/index.html
<h1>Centos7 To Apline</h1>
[root@gitlab wwwroot]#