Dockerizing an Nginx Service

docker

配置一台Nginx Server的Docker

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# Nginx Dockerfile
#
# Pull base image.
FROM docker.cn/docker/ubuntu:14.04
# Install Nginx.
RUN apt-get update && apt-get install -y software-properties-common
RUN \
add-apt-repository -y ppa:nginx/stable && \
apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/* && \
echo "\ndaemon off;" >> /etc/nginx/nginx.conf && \
chown -R www-data:www-data /var/lib/nginx
# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx"]
# Define working directory.
WORKDIR /etc/nginx
# Define default command.
CMD ["nginx"]
# Expose ports.
EXPOSE 80
EXPOSE 443

Build

构建 nginx 镜像:

1
$ sudo docker build -t lanvige/nginx:1.4.4 .

Run

将 nginx docker 的80端口绑定到本地80端口。

1
2
3
$ docker run -d -p 80:80 lanvige/nginx:1.4.4
$ sudo docker run -t -i -p 80:80 lanvige/nginx:1.4.4 /bin/bash

这时,就可以通过http://HOST_IP来访问了,看到Welcome to nginx on Debian!,说明一切正常。

也可以通过参数指定相应配置,绑定本地/共享目录:

1
2
3
4
5
6
$ docker run -d -p 80:80 -v <sites-enabled-dir>:/etc/nginx/sites-enabled -v <certs-dir>:/etc/nginx/certs -v <log-dir>:/var/log/nginx dockerfile/nginx
# Example:
$ sudo docker run -d -p 80:80 -v /var/nginx/sites-enabled:/etc/nginx/sites-enabled -v /var/nginx/certs:/etc/nginx/certs -v /var/nginx/log:/var/log/nginx lanvige/nginx:1.4.4
sudo docker run -d -p 80:80 -v /var/nginx/sites-enabled:/etc/nginx/sites-enabled -v /var/nginx/certs:/etc/nginx/certs -v /var/nginx/log:/var/log/nginx -v /var/www/lanvige:/var/www/lanvige lanvige/nginx:1.4.4

REF::