虚拟化容器,大数据,DBA,中间件,监控。

解析Dockerfile

29 12月
作者:admin|分类:容器虚拟化

解析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 .

 

image.png

 

示例二:

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 .

image.png

 

示例三:

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.default_socket= [Phar] [mail function] SMTP = localhost smtp_port = 25 mail.add_x_header = On [SQL] sql.safe_mode = Off [ODBC] odbc.allow_persistent = On odbc.check_persistent = On odbc.max_persistent = -1 odbc.max_links = -1 odbc.defaultlrl = 4096 odbc.defaultbinmode = 1 [Interbase] ibase.allow_persistent = 1 ibase.max_persistent = -1 ibase.max_links = -1 ibase.timestampformat = "%Y-%m-%d %H:%M:%S" ibase.dateformat = "%Y-%m-%d" ibase.timeformat = "%H:%M:%S" [MySQL] mysql.allow_local_infile = On mysql.allow_persistent = On mysql.cache_size = 2000 mysql.max_persistent = -1 mysql.max_links = -1 mysql.default_port = mysql.default_socket = mysql.default_host = mysql.default_user = mysql.default_password = mysql.connect_timeout = 60 mysql.trace_mode = Off [MySQLi] mysqli.max_persistent = -1 mysqli.allow_persistent = On mysqli.max_links = -1 mysqli.cache_size = 2000 mysqli.default_port = 3306 mysqli.default_socket = mysqli.default_host = mysqli.default_user = mysqli.default_pw = mysqli.reconnect = Off [mysqlnd] mysqlnd.collect_statistics = On mysqlnd.collect_memory_statistics = Off [OCI8] [PostgreSQL] pgsql.allow_persistent = On pgsql.auto_reset_persistent = Off pgsql.max_persistent = -1 pgsql.max_links = -1 pgsql.ignore_notice = 0 pgsql.log_notice = 0 [Sybase-CT] sybct.allow_persistent = On sybct.max_persistent = -1 sybct.max_links = -1 sybct.min_server_severity = 10 sybct.min_client_severity = 10 [bcmath] bcmath.scale = 0 [browscap] [Session] session.save_handler = files session.use_strict_mode = 0 session.use_cookies = 1 session.use_only_cookies = 1 session.name = PHPSESSID session.auto_start = 0 session.cookie_lifetime = 0 session.cookie_path = / session.cookie_domain = session.cookie_httponly = session.serialize_handler = php session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440 session.referer_check = session.cache_limiter = nocache session.cache_expire = 180 session.use_trans_sid = 0 session.hash_function = 0 session.hash_bits_per_character = 5 url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry" [MSSQL] mssql.allow_persistent = On mssql.max_persistent = -1 mssql.max_links = -1 mssql.min_error_severity = 10 mssql.min_message_severity = 10 mssql.compatibility_mode = Off mssql.secure_connection = Off [Assertion] [COM] [mbstring] [gd] [exif] [Tidy] tidy.clean_output = Off [soap] soap.wsdl_cache_enabled=1 soap.wsdl_cache_dir="/tmp" soap.wsdl_cache_ttl=86400 soap.wsdl_cache_limit = 5 [sysvshm] [ldap] ldap.max_links = -1 [mcrypt] [dba] [opcache] opcache.enable=1 opcache.enable_cli=0 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=20000 opcache.use_cwd=1 opcache.validate_timestamps=1 opcache.revalidate_freq=0 [curl] [openssl]

Dockerfile:

[root@harbor php]# cat Dockerfile FROM centos:7 MAINTAINER www.ctnrs.com RUN yum install epel-release -y && \  yum install -y gcc gcc-c++ make gd-devel libxml2-devel \  libcurl-devel libjpeg-devel libpng-devel openssl-devel \  libmcrypt-devel libxslt-devel libtidy-devel autoconf \  iproute net-tools telnet wget curl && \  yum clean all && \  rm -rf /var/cache/yum/*  COPY php-5.6.36.tar.gz / RUN tar zxf php-5.6.36.tar.gz && \  cd php-5.6.36 && \  ./configure --prefix=/usr/local/php \  --with-config-file-path=/usr/local/php/etc \  --enable-fpm --enable-opcache \  --with-mysql --with-mysqli --with-pdo-mysql \  --with-openssl --with-zlib --with-curl --with-gd \  --with-jpeg-dir --with-png-dir --with-freetype-dir \  --enable-mbstring --with-mcrypt --enable-hash && \  make -j 4 && make install && \  cp php.ini-production /usr/local/php/etc/php.ini && \  cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \  sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \  mkdir /usr/local/php/log && \  cd / && rm -rf php* && \  ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  ENV PATH $PATH:/usr/local/php/sbin COPY php.ini /usr/local/php/etc/ COPY php-fpm.conf /usr/local/php/etc/ WORKDIR /usr/local/php EXPOSE 9000 CMD ["php-fpm"]

构建:

 

[root@harbor php]# docker build -t php:v1 .


 

 

posted @ 2020-03-09 08:47  benjamin杨  阅读( 579)  评论( 0)  编辑  收藏  举报
刷新评论刷新页面返回顶部
浏览505 评论0
返回
目录
返回
首页
基于docker构建cicd平台 vscode10个必装的插件