1、可以通过 docker pull 的方式从镜像源上载入nginx镜像
2、通过 Dockerfile 创建镜像
2.1、新建 Dockerfile 文件
[dkuser@c741 ~]$ mkdir centos_nginx
[dkuser@c741 ~]$ cd centos_nginx/
下载nginx包
[dkuser@c741 ~] $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
[dkuser@c741 centos_nginx]$ vi Dockerfile
# From表示使用centos:latest这个镜像为基础构建我们的镜像
FROM centos:latest
# 创建者的基本信息
MAINTAINER xiaozhou (xiaozhou@docker.com)
LABEL Discription="基于centos的nginx镜像" version="1.0"
# put nginx-1.8.0.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.8.0.tar.gz /usr/local/src
#安装nginx所依赖的包
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel \
&& yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel \
&& useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.8.0
WORKDIR /usr/local/src/nginx-1.8.0
# 编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-file-aio --with-http_ssl_module --with-http_realip_module \
--with-http_addition_module --with-http_xslt_module \
--with-http_image_filter_module --with-http_geoip_module \
--with-http_sub_module --with-http_dav_module --with-http_flv_module \
--with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_auth_request_module --with-http_random_index_module \
--with-http_secure_link_module --with-http_degradation_module \
--with-http_stub_status_module \
&& make && make install
# nginx_config
RUN mkdir /usr/local/nginx/logs && mkdir /usr/local/nginx/run \
&& sed -e '3i\user nginx;' -i /usr/local/nginx/conf/nginx.conf \
&& sed -e "9i\error_log /usr/local/nginx/logs/error.log;" -i /usr/local/nginx/conf/nginx.conf \
&& sed -e "12i\pid /usr/local/nginx/run/nginx.pid;" -i /usr/local/nginx/conf/nginx.conf \
&& sed -e "29i\ \taccess_log /usr/local/nginx/logs/access.log;" -i /usr/local/nginx/conf/nginx.conf
# logrotate
RUN touch /etc/logrotate.d/nginx \
&& echo -e "$LOGS_DIR/*.log {" >> /etc/logrotate.d/nginx \
&& echo -e "\tdaily" >> /etc/logrotate.d/nginx \
&& echo -e "\trotate" >> /etc/logrotate.d/nginx \
&& echo -e "\tmissingok" >> /etc/logrotate.d/nginx \
&& echo -e "\tdateext" >> /etc/logrotate.d/nginx \
&& echo -e "\tcompress" >> /etc/logrotate.d/nginx \
&& echo -e "\tdelaycompress" >> /etc/logrotate.d/nginx \
&& echo -e "\tnotifempty" >> /etc/logrotate.d/nginx \
&& echo -e "\tsharedscripts" >> /etc/logrotate.d/nginx \
&& echo -e "\tpostrotate" >> /etc/logrotate.d/nginx \
&& echo -e "\t/usr/bin/kill -USR1 \`cat $LOGS_DIR/nginx.pid\`" >> /etc/logrotate.d/nginx \
&& echo -e "\tendscript" >> /etc/logrotate.d/nginx \
&& echo -e "\t}" >> /etc/logrotate.d/nginx
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD /bin/sh -c 'nginx -g "daemon off;"'
2.2、执行创建镜像命令
[dkuser@c741 centos_nginx]$ docker build -t centos_nginx .
.......
make[1]: Leaving directory `/usr/local/src/nginx-1.8.0'
Removing intermediate container 2471c1cc8e93
---> dcd18de4a1c9
Step 16/16 : EXPOSE 80
---> Running in e80691a08812
Removing intermediate container e80691a08812
---> 3a69ebee9a56
Successfully built 3a69ebee9a56
Successfully tagged centos_nginx:latest
查看镜像
[dkuser@c741 centos_nginx]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx latest 3a69ebee9a56 52 seconds ago 745MB
3、运行容器
[dkuser@c741 centos_nginx]$ docker run -dit -p 80:80 centos_nginx
参数说明:
-d 后台运行
-i 交互模式
-t 分配tty设备,支持终端登录
4、登陆容器
[dkuser@c741 centos_nginx]$ docker attach objective_banzai
查看文件
[root@8b534d79f7c5 nginx-1.8.0]# ll
total 632
-rw-r--r-- 1 1001 1001 249124 Apr 21 2015 CHANGES
-rw-r--r-- 1 1001 1001 379021 Apr 21 2015 CHANGES.ru
-rw-r--r-- 1 1001 1001 1397 Apr 21 2015 LICENSE
-rw-r--r-- 1 root root 366 Apr 25 08:52 Makefile
-rw-r--r-- 1 1001 1001 49 Apr 21 2015 README
drwxr-xr-x 6 1001 1001 312 Apr 25 08:48 auto
.....
拷贝启动脚本,启动nginx
[root@8b534d79f7c5 nginx-1.8.0]# mkdir sbin
[root@8b534d79f7c5 nginx-1.8.0]# cp objs/nginx sbin/
[root@8b534d79f7c5 nginx-1.8.0]# cd sbin
[root@8b534d79f7c5 sbin]# ./nginx
访问nginx
[root@8b534d79f7c5 sbin]# curl http://localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
.....
注意:
不要使用 exit 和 ctrl + D 退出容器,因为这样会停止容器的运行,可以使用 ctrl + P + Q 退出容器
5、宿主机访问nginx
[dkuser@c741 centos_nginx]$ curl http://localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
.....
6、浏览器访问nginx
浏览器访问:
http://192.168.121.129:80
参考: