Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。它们简化了从头到尾的流程并极大的简化了部署工作。Dockerfile从FROM命令开始,紧接着跟随者各种方法,命令和参数。其产出为一个新的可以用于创建容器的镜像。
RUN和CMD看起来挺像,但是CMD用来指定容器启动时用到的命令,只能有一条,如: CMD ["/bin/bash", "/usr/local/nginx/sbin/nginx", "-c", "/usr/local/nginx/conf/nginx.conf"]
使用dockerfile创建一个nginx编译安装镜像。
#Dockerfile to build Nginx Install Containers
#Based on CentOS
FROM centos_with_net
#author/maintainer
MAINTAINER adai adai_mail@163.com
#install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib-devel make openssl-devel
#Download source packages of Nginx
ADD http://nginx.org/download/nginx-1.12.1.tar.gz ./
#Install Nginx
RUN tar zxvf nginx-1.12.1.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.12.1 && ./configure --prefix=/usr/local/nginx && make && make install
#Config Nginx
RUN rm -fv /usr/local/nginx/conf/nginx.conf
COPY .nginx_conf /usr/local/nginx/conf/nginx.conf
#Expose ports
EXPOSE 80
#Set the default commands to execute
#when creating a new container
#ENTRYPOINT /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
##暂时用不到
准备nginx配置文件:
[root@study ~]# vim .nginx_conf
user nobody nobody;
#定义启动Nginx的用户
worker_processes 2;
#定义子进程数目
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#指定Nginx最多可打开的文件数目
events
{
use epoll;
worker_connections 6000;
#进程最大连接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
#虚拟主机
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
#配置PHP解析
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
[root@study ~]# docker build -t centos_nginx ./
##-t:指定文件名称
##./:存放位置
Successfully built 30a0120b1624
[root@study ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_nginx latest 30a0120b1624 42 seconds ago 364.5 MB
build时出现以下错误:
WARNING IPv4 forwarding is disabled. Networking will not work
无法获取网络服务!!!
原因是net.ipv4内核参数为开启,解决办法:
[root@study ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
##默认为0
重启网络:
[root@study ~]# systemctl restart network
[root@study ~]# /etc/sysctl.conf
net.ipv4.ip_forward = 1
然后再重新build即可!
[root@study ~]# docker run -itd --name test_nginx -P centos_nginx bash
[root@study ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63dfcb93b0db centos_nginx "bash" 5 seconds ago Up 3 seconds 0.0.0.0:32769->80/tcp test_nginx
##此处容器80端口映射到母机的32769端口
[root@study ~]# docker exec -it test_nginx bash
启动nginx服务:
[root@63dfcb93b0db /]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@63dfcb93b0db /]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 28/nginx: master pr
##启动成功!
[root@63dfcb93b0db /]# curl localhost
<title>Welcome to nginx!</title>
在母机访问:
[root@study ~]# curl 127.0.0.1:32769
<title>Welcome to nginx!</title>
Finish!
(adsbygoogle = window.adsbygoogle || []).push({});