解析Dockerfile
Dockerfile就是一个描述镜像具体组成的具体文件,可以根据这个文件构建镜像。
FROM:指定基础镜像
FROM centos:7
MAINTAINER:镜像维护信息
MAINTAINER Benjamin Yang <133xxxx2282@163.com>
RUN:执行命令
RUN ["yum", "install", "httpd"] RUN yum install httpd
COPY:复制文本
COPY <源路径>... <目标路径> COPY ["<源路径1>",... "<目标路径>"] <源路径> 可以是多个、以及使用通配符,通配符规则满足Go的filepath.Match 规则,如:COPY *.txt /workdir/ COPY hom?.txt /mydir/ <目标路径>使用 COPY 指令,源文件的各种元数据都会保留。比如读、写、执行权限、文件变更时间等。
ADD:高级复制文件
ADD jdk-8u45-linux-x64.tar.gz / <源路径> 可以是一个 URL ,下载后的文件权限自动设置为 600 。
CMD:容器启动命令
shell 格式: CMD <命令> exec 格式: CMD ["可执行文件", "参数1", "参数2"...] CMD nginx -g "daemon off;" CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT:入口点
同CMD,指定容器启动程序及参数。
通过--entrypoint 参数在运行时替换。
启动容器时执行的Shell命令,同CMD类似,只是由ENTRYPOINT启动的程序不会被docker run命令行指定的参数所覆盖,而且,这些命令行参数会被当作参数传递给ENTRYPOINT指定指定的程序,例如:
PS:Dockerfile文件中也可以存在多个ENTRYPOINT指令,但仅有最后一个会生效。
示例: FROM ubuntu:16.04 RUN apt-get update \ && apt-get install -y curl \ && rm -rf /var/lib/apt/lists/* ENTRYPOINT [ "curl", "-s", "http://ip.cn" ] 追加-i参数 $ docker run myip -i ...... 当前 IP:61.148.226.66 来自:北京市 联通
ENV:设置环境变量
在其他指令中可以直接引用ENV设置的环境变量。
ENV MYSQL_ROOT_PASSWORD 123456 ENV JAVA_HOME /usr/local/jdk1.8.0_45
ARG:构建参数
与ENV不同的是,容器运行时不会存在这些环境变量。
可以用 docker build --build-arg <参数名>=<值> 来覆盖。
FROM centos:6 ARG user # ARG user=root USER $user
这时,我们在docker build时可以带上自定义参数user了,如下所示:
docker build --build-arg user=benjaminYang Dockerfile .
VOLUME:定义匿名卷
VOLUME ["/var/lib/mysql"]
PS:一般不会在Dockerfile中用到,更常见的还是在docker run的时候指定-v数据卷。
EXPOSE:暴露端口
声明容器运行的服务端口,例如:
EXPOSE 80 443
WORKDIR:指定工作目录
为RUN、CMD、ENTRYPOINT以及COPY和AND设置工作目录,例如:
WORKDIR /data
USER:指定当前用户
这个用户必须是事先建立好的,否则无法切换。 USER <用户名>
HEALTHCHECK:健康检查
HEALTHCHECK --interval=5m --timeout=3s --retries=3 \ CMD curl -f http:/localhost/ || exit 1
其中,一些选项的说明:
- --interval=DURATION (default: 30s):每隔多长时间探测一次,默认30秒
- -- timeout= DURATION (default: 30s):服务响应超时时长,默认30秒
- --start-period= DURATION (default: 0s):服务启动多久后开始探测,默认0秒
- --retries=N (default: 3):认为检测失败几次为宕机,默认3次
一些返回值的说明:
- 0:容器成功是健康的,随时可以使用
- 1:不健康的容器无法正常工作
- 2:保留不使用此退出代码
mkdir dockerfile/{tomcat,nginx,php} -p
示例一:
tomcat的镜像包构建
apache-tomcat-8.5.43.tar.gz
Dockerfile:
[root@harbor tomcat]# cat Dockerfile FROM centos:7 MAINTAINER www.ctnrs.com ENV VERSION=8.5.43 RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \ yum clean all && \ rm -rf /var/cache/yum/* COPY apache-tomcat-${VERSION}.tar.gz / RUN tar zxf apache-tomcat-${VERSION}.tar.gz && \ mv apache-tomcat-${VERSION} /usr/local/tomcat && \ rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \ mkdir /usr/local/tomcat/webapps/test && \ echo "ok" > /usr/local/tomcat/webapps/test/status.html && \ sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ENV PATH $PATH:/usr/local/tomcat/bin WORKDIR /usr/local/tomcat EXPOSE 8080 CMD ["catalina.sh", "run"]
构建:
[root@harbor tomcat]# docker build -t tomcat:v1 .
示例二:
nginx-1.15.5.tar.gz
nginx的编译 镜像
nginx.conf
[root@harbor nginx]# cat nginx.conf user nobody; worker_processes 4; worker_rlimit_nofile 65535; error_log logs/error.log notice; pid /var/run/nginx.pid; events { use epoll; worker_connections 4096; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log off; keepalive_timeout 65; client_max_body_size 64m; server { listen 80; server_name www.ctnrs.com; index index.php index.html; access_log logs/www.ctnrs.com_access.log; error_log logs/www.ctnrs.com_error.log; # location ~ .*\.(js|css|html|png|gif|jpg|jpeg)$ { location / { root /wwwroot; } location ~* \.php$ { root /wwwroot; fastcgi_pass lnmp_php:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
Dockerfile:
[root@harbor nginx]# cat Dockerfile FROM centos:7 LABEL maintainer www.ctnrs.com RUN yum install -y gcc gcc-c++ make \ openssl-devel pcre-devel gd-devel \ iproute net-tools telnet wget curl && \ yum clean all && \ rm -rf /var/cache/yum/* COPY nginx-1.15.5.tar.gz / RUN tar zxf nginx-1.15.5.tar.gz && \ cd nginx-1.15.5 && \ ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module && \ make -j 4 && make install && \ rm -rf /usr/local/nginx/html/* && \ echo "ok" >> /usr/local/nginx/html/status.html && \ cd / && rm -rf nginx* && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ENV PATH $PATH:/usr/local/nginx/sbin COPY nginx.conf /usr/local/nginx/conf/nginx.conf WORKDIR /usr/local/nginx EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
构建:
[root@harbor nginx]# docker build -t nginx:v1 .
示例三:
php编译镜像包
php-5.6.36.tar.gz
php-fpm.conf
[root@harbor php]# cat php-fpm.conf |egrep -v ';'| grep -v '^\s*$' [global] pid = /var/run/php-fpm.pid error_log = /usr/local/php/log/php-fpm.log log_level = warning emergency_restart_interval = 24h process_control_timeout = 5s daemonize = no rlimit_files = 10240 [www] user = nobody group = nobody listen = 0.0.0.0:9000 listen.owner = nobody listen.group = nobody pm = dynamic pm.max_children = 200 pm.start_servers = 50 pm.min_spare_servers = 50 pm.max_spare_servers = 100 pm.max_requests = 51200 pm.status_path = /status slowlog = log/$pool.log.slow request_slowlog_timeout = 10 request_terminate_timeout = 600 rlimit_files = 10240
php.ini
[root@harbor php]# cat php.ini |egrep -v ';'| grep -v '^\s*$' #去掉注释和空行 [PHP] engine = On short_open_tag = Off asp_tags = Off precision = 14 output_buffering = 4096 zlib.output_compression = Off implicit_flush = Off unserialize_callback_func = serialize_precision = 17 disable_functions = disable_classes = zend.enable_gc = On expose_php = On max_execution_time = 30 max_input_time = 60 memory_limit = 128M error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT display_errors = Off display_startup_errors = Off log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = Off html_errors = On variables_order = "GPCS" request_order = "GP" register_argc_argv = Off auto_globals_jit = On post_max_size = 100M auto_prepend_file = auto_append_file = default_mimetype = "text/html" default_charset = "UTF-8" doc_root = user_dir = enable_dl = Off file_uploads = On upload_max_filesize = 100M max_file_uploads = 20 allow_url_fopen = On allow_url_include = Off default_socket_timeout = 60 [CLI Server] cli_server.color = On [Date] date.timezone = Asia/Shanghai [filter] [iconv] [intl] [sqlite3] [Pcre] [Pdo] [Pdo_mysql] pdo_mysql.cache_size = 2000 pdo_mysql