您的位置:首页 > 运维架构 > Docker

Docker容器虚拟化(四)—dockerfile的格式与应用(with实例)

2017-10-15 00:00 489 查看

Dockerfile

Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。它们简化了从头到尾的流程并极大的简化了部署工作。Dockerfile从FROM命令开始,紧接着跟随者各种方法,命令和参数。其产出为一个新的可以用于创建容器的镜像。

Dockerfile 语法

FROM:指定基于哪个镜像

格式:FROM < image >或者FROM < image >:< tag > ,如:

FROM centos;FROM centos:latest

MAINTAINER:指定作者信息

格式:MAINTAINER < name及联系方式 >,如:

MAINTAINER adai adai@adailinux.com

RUN:镜像操作指令

格式:RUN < commond >或者RUN [ "executable","param1","param2" ],如:

RUN yum install -y httpd;RUN [ "/bin/bash","-c","echo hello" ]

CMD(三种格式):

CMD ["executable","param1","param2"]

CMD command param1 param2

CMD ["param1", "param2"]

RUN和CMD看起来挺像,但是CMD用来指定容器启动时用到的命令,只能有一条,如:

CMD ["/bin/bash", "/usr/local/nginx/sbin/nginx", "-c", "/usr/local/nginx/conf/nginx.conf"]

EXPOSE

格式:EXPOSE < port> [ < port >... ] ,如:

EXPOSE 22 80 8443

这个用来指定要映射出去的端口,比如容器内部我们启动了sshd和nginx,所以我们需要把22和80端口暴漏出去。这个需要配合-P(大写)来工作,也就是说在启动容器时,需要加上-P,让它自动分配。如果想指定具体的端口,也可以使用-p(小写)来指定。

ENV:环境变量

格式 ENV < key > < value >,如:

ENV PATH /usr/local/mysql/bin:$PATH,它主要是为后续的RUN指令提供一个环境变量,我们也可以定义一些自定义的变量,如:ENV MYSQL_version 5.6。

ADD

格式:add < src > < dest >

将本地的一个文件或目录拷贝到容器的某个目录里。 其中src为Dockerfile所在目录的相对路径,它也可以是一个url。比如:

ADD < conf/vhosts > < /usr/local/nginx/conf >

COPY

格式同add。。

使用方法和add一样,不同的是,它不支持url。

ENTRYPOINT

格式:类似CMD

容器启动时要执行的命令,它和CMD很像,也是只有一条生效,如果写多个只有最后一条有效。和CMD不同是:

CMD 是可以被 docker run 指令覆盖的,而ENTRYPOINT不能覆盖。

比如,容器名字为adai,我们在Dockerfile中指定如下CMD:CMD [“/bin/echo”, “test”],启动容器的命令是:docker run adai,这样会输出test。

假如启动容器的命令是docker run -it adai /bin/bash什么都不会输出。

ENTRYPOINT不会被覆盖,而且会比CMD或者docker run指定的命令要靠前执行,如:

ENTRYPOINT ["echo", "test"]

docker run -it adai 123

则会输入test 123,这相当于要执行命令echo test 123。

VOLUME

格式:VOLUME ["/data"]

创建一个可以从本地主机或其他容器挂载的挂载点。

USER

格式:USER daemon

指定运行容器的用户

WORKDIR

格式 WORKDIR /path/to/workdir

为后续的RUN、CMD或者ENTRYPOINT指定工作目录。

Dockerfile示例

使用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即可!

测试镜像centos_nginx

[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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Dockerfile