Dockerfile介绍

image.png

Dockerfile常用指令

image.png

Dockerfile构建镜像

前端项目镜像构建与部署:Nginx

准备nginx包:nginx-1.15.5.tar.gz
Dockerfile

  1. FROM centos:7
  2. LABEL maintainer www.ctnrs.com
  3. RUN yum install -y gcc gcc-c++ make \
  4. openssl-devel pcre-devel gd-devel \
  5. iproute net-tools telnet wget curl && \
  6. yum clean all && \
  7. rm -rf /var/cache/yum/*
  8. ADD nginx-1.15.5.tar.gz /
  9. RUN cd nginx-1.15.5 && \
  10. ./configure --prefix=/usr/local/nginx \
  11. --with-http_ssl_module \
  12. --with-http_stub_status_module && \
  13. make -j 4 && make install && \
  14. mkdir /usr/local/nginx/conf/vhost && \
  15. cd / && rm -rf nginx* && \
  16. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  17. ENV PATH $PATH:/usr/local/nginx/sbin
  18. COPY nginx.conf /usr/local/nginx/conf/nginx.conf
  19. WORKDIR /usr/local/nginx
  20. EXPOSE 80
  21. CMD ["nginx", "-g", "daemon off;"]

nginx.conf

  1. user nobody;
  2. worker_processes 4;
  3. worker_rlimit_nofile 65535;
  4. error_log logs/error.log notice;
  5. pid /var/run/nginx.pid;
  6. events {
  7. use epoll;
  8. worker_connections 4096;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. access_log off;
  17. keepalive_timeout 65;
  18. client_max_body_size 64m;
  19. include /usr/local/nginx/conf/vhost/*.conf;
  20. server {
  21. listen 80;
  22. server_name localhost;
  23. index index.html;
  24. location / {
  25. root html;
  26. }
  27. }
  28. }

php.conf

  1. server {
  2. listen 80;
  3. server_name example.ctnrs.com;
  4. index index.php index.html;
  5. access_log logs/www.ctnrs.com_access.log;
  6. error_log logs/www.ctnrs.com_error.log;
  7. location / {
  8. root /wwwroot;
  9. }
  10. location ~* \.php$ {
  11. root /wwwroot;
  12. fastcgi_pass lnmp_php:9000;
  13. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  14. include fastcgi_params;
  15. }
  16. }

构建nginx镜像

  1. docker build -t nginx:v1 .

前端项目镜像构建与部署:Php

准备php包:php-5.6.36.tar.gz
Dockerfile

  1. FROM centos:7
  2. MAINTAINER www.ctnrs.com
  3. RUN yum install epel-release -y && \
  4. yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
  5. libcurl-devel libjpeg-devel libpng-devel openssl-devel \
  6. libmcrypt-devel libxslt-devel libtidy-devel autoconf \
  7. iproute net-tools telnet wget curl && \
  8. yum clean all && \
  9. rm -rf /var/cache/yum/*
  10. ADD php-5.6.36.tar.gz /
  11. RUN cd php-5.6.36 && \
  12. ./configure --prefix=/usr/local/php \
  13. --with-config-file-path=/usr/local/php/etc \
  14. --enable-fpm --enable-opcache \
  15. --with-mysql --with-mysqli --with-pdo-mysql \
  16. --with-openssl --with-zlib --with-curl --with-gd \
  17. --with-jpeg-dir --with-png-dir --with-freetype-dir \
  18. --enable-mbstring --with-mcrypt --enable-hash && \
  19. make -j 4 && make install && \
  20. cp php.ini-production /usr/local/php/etc/php.ini && \
  21. cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
  22. sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
  23. mkdir /usr/local/php/log && \
  24. cd / && rm -rf php* && \
  25. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  26. ENV PATH $PATH:/usr/local/php/sbin
  27. COPY php.ini /usr/local/php/etc/
  28. COPY php-fpm.conf /usr/local/php/etc/
  29. WORKDIR /usr/local/php
  30. EXPOSE 9000
  31. CMD ["php-fpm"]

php-fpm.conf

  1. ;;;;;;;;;;;;;;;;;;;;;
  2. ; FPM Configuration ;
  3. ;;;;;;;;;;;;;;;;;;;;;
  4. ; All relative paths in this configuration file are relative to PHP's install
  5. ; prefix (/usr/local/php). This prefix can be dynamically changed by using the
  6. ; '-p' argument from the command line.
  7. ; Include one or more files. If glob(3) exists, it is used to include a bunch of
  8. ; files from a glob(3) pattern. This directive can be used everywhere in the
  9. ; file.
  10. ; Relative path can also be used. They will be prefixed by:
  11. ; - the global prefix if it's been set (-p argument)
  12. ; - /usr/local/php otherwise
  13. ;include=etc/fpm.d/*.conf
  14. ;;;;;;;;;;;;;;;;;;
  15. ; Global Options ;
  16. ;;;;;;;;;;;;;;;;;;
  17. [global]
  18. ; Pid file
  19. ; Note: the default prefix is /usr/local/php/var
  20. ; Default Value: none
  21. pid = /var/run/php-fpm.pid
  22. ; Error log file
  23. ; If it's set to "syslog", log is sent to syslogd instead of being written
  24. ; in a local file.
  25. ; Note: the default prefix is /usr/local/php/var
  26. ; Default Value: log/php-fpm.log
  27. error_log = /usr/local/php/log/php-fpm.log
  28. ; syslog_facility is used to specify what type of program is logging the
  29. ; message. This lets syslogd specify that messages from different facilities
  30. ; will be handled differently.
  31. ; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
  32. ; Default Value: daemon
  33. ;syslog.facility = daemon
  34. ; syslog_ident is prepended to every message. If you have multiple FPM
  35. ; instances running on the same server, you can change the default value
  36. ; which must suit common needs.
  37. ; Default Value: php-fpm
  38. ;syslog.ident = php-fpm
  39. ; Log level
  40. ; Possible Values: alert, error, warning, notice, debug
  41. ; Default Value: notice
  42. log_level = warning
  43. ; If this number of child processes exit with SIGSEGV or SIGBUS within the time
  44. ; interval set by emergency_restart_interval then FPM will restart. A value
  45. ; of '0' means 'Off'.
  46. ; Default Value: 0
  47. ;emergency_restart_threshold = 0
  48. ; Interval of time used by emergency_restart_interval to determine when
  49. ; a graceful restart will be initiated. This can be useful to work around
  50. ; accidental corruptions in an accelerator's shared memory.
  51. ; Available Units: s(econds), m(inutes), h(ours), or d(ays)
  52. ; Default Unit: seconds
  53. ; Default Value: 0
  54. emergency_restart_interval = 24h
  55. ; Time limit for child processes to wait for a reaction on signals from master.
  56. ; Available units: s(econds), m(inutes), h(ours), or d(ays)
  57. ; Default Unit: seconds
  58. ; Default Value: 0
  59. process_control_timeout = 5s
  60. ; The maximum number of processes FPM will fork. This has been design to control
  61. ; the global number of processes when using dynamic PM within a lot of pools.
  62. ; Use it with caution.
  63. ; Note: A value of 0 indicates no limit
  64. ; Default Value: 0
  65. ; process.max = 128
  66. ; Specify the nice(2) priority to apply to the master process (only if set)
  67. ; The value can vary from -19 (highest priority) to 20 (lower priority)
  68. ; Note: - It will only work if the FPM master process is launched as root
  69. ; - The pool process will inherit the master process priority
  70. ; unless it specified otherwise
  71. ; Default Value: no set
  72. ; process.priority = -19
  73. ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
  74. ; Default Value: yes
  75. daemonize = no
  76. ; Set open file descriptor rlimit for the master process.
  77. ; Default Value: system defined value
  78. rlimit_files = 10240
  79. ; Set max core size rlimit for the master process.
  80. ; Possible Values: 'unlimited' or an integer greater or equal to 0
  81. ; Default Value: system defined value
  82. ;rlimit_core = 0
  83. ; Specify the event mechanism FPM will use. The following is available:
  84. ; - select (any POSIX os)
  85. ; - poll (any POSIX os)
  86. ; - epoll (linux >= 2.5.44)
  87. ; - kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
  88. ; - /dev/poll (Solaris >= 7)
  89. ; - port (Solaris >= 10)
  90. ; Default Value: not set (auto detection)
  91. ;events.mechanism = epoll
  92. ; When FPM is build with systemd integration, specify the interval,
  93. ; in second, between health report notification to systemd.
  94. ; Set to 0 to disable.
  95. ; Available Units: s(econds), m(inutes), h(ours)
  96. ; Default Unit: seconds
  97. ; Default value: 10
  98. ;systemd_interval = 10
  99. ;;;;;;;;;;;;;;;;;;;;
  100. ; Pool Definitions ;
  101. ;;;;;;;;;;;;;;;;;;;;
  102. ; Multiple pools of child processes may be started with different listening
  103. ; ports and different management options. The name of the pool will be
  104. ; used in logs and stats. There is no limitation on the number of pools which
  105. ; FPM can handle. Your system will tell you anyway :)
  106. ; Start a new pool named 'www'.
  107. ; the variable $pool can we used in any directive and will be replaced by the
  108. ; pool name ('www' here)
  109. [www]
  110. ; Per pool prefix
  111. ; It only applies on the following directives:
  112. ; - 'access.log'
  113. ; - 'slowlog'
  114. ; - 'listen' (unixsocket)
  115. ; - 'chroot'
  116. ; - 'chdir'
  117. ; - 'php_values'
  118. ; - 'php_admin_values'
  119. ; When not set, the global prefix (or /usr/local/php) applies instead.
  120. ; Note: This directive can also be relative to the global prefix.
  121. ; Default Value: none
  122. ;prefix = /path/to/pools/$pool
  123. ; Unix user/group of processes
  124. ; Note: The user is mandatory. If the group is not set, the default user's group
  125. ; will be used.
  126. user = nobody
  127. group = nobody
  128. ; The address on which to accept FastCGI requests.
  129. ; Valid syntaxes are:
  130. ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
  131. ; a specific port;
  132. ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
  133. ; a specific port;
  134. ; 'port' - to listen on a TCP socket to all IPv4 addresses on a
  135. ; specific port;
  136. ; '[::]:port' - to listen on a TCP socket to all addresses
  137. ; (IPv6 and IPv4-mapped) on a specific port;
  138. ; '/path/to/unix/socket' - to listen on a unix socket.
  139. ; Note: This value is mandatory.
  140. ;listen = 127.0.0.1:9000
  141. listen = 0.0.0.0:9000
  142. ; Set listen(2) backlog.
  143. ; Default Value: 65535 (-1 on FreeBSD and OpenBSD)
  144. ;listen.backlog = 65535
  145. ; Set permissions for unix socket, if one is used. In Linux, read/write
  146. ; permissions must be set in order to allow connections from a web server. Many
  147. ; BSD-derived systems allow connections regardless of permissions.
  148. ; Default Values: user and group are set as the running user
  149. ; mode is set to 0660
  150. listen.owner = nobody
  151. listen.group = nobody
  152. ;listen.mode = 0660
  153. ; When POSIX Access Control Lists are supported you can set them using
  154. ; these options, value is a comma separated list of user/group names.
  155. ; When set, listen.owner and listen.group are ignored
  156. ;listen.acl_users =
  157. ;listen.acl_groups =
  158. ; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
  159. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
  160. ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
  161. ; must be separated by a comma. If this value is left blank, connections will be
  162. ; accepted from any ip address.
  163. ; Default Value: any
  164. ; listen.allowed_clients = 127.0.0.1
  165. ; Specify the nice(2) priority to apply to the pool processes (only if set)
  166. ; The value can vary from -19 (highest priority) to 20 (lower priority)
  167. ; Note: - It will only work if the FPM master process is launched as root
  168. ; - The pool processes will inherit the master process priority
  169. ; unless it specified otherwise
  170. ; Default Value: no set
  171. ; process.priority = -19
  172. ; Set the process dumpable flag (PR_SET_DUMPABLE prctl) even if the process user
  173. ; or group is differrent than the master process user. It allows to create process
  174. ; core dump and ptrace the process for the pool user.
  175. ; Default Value: no
  176. ; process.dumpable = yes
  177. ; Choose how the process manager will control the number of child processes.
  178. ; Possible Values:
  179. ; static - a fixed number (pm.max_children) of child processes;
  180. ; dynamic - the number of child processes are set dynamically based on the
  181. ; following directives. With this process management, there will be
  182. ; always at least 1 children.
  183. ; pm.max_children - the maximum number of children that can
  184. ; be alive at the same time.
  185. ; pm.start_servers - the number of children created on startup.
  186. ; pm.min_spare_servers - the minimum number of children in 'idle'
  187. ; state (waiting to process). If the number
  188. ; of 'idle' processes is less than this
  189. ; number then some children will be created.
  190. ; pm.max_spare_servers - the maximum number of children in 'idle'
  191. ; state (waiting to process). If the number
  192. ; of 'idle' processes is greater than this
  193. ; number then some children will be killed.
  194. ; ondemand - no children are created at startup. Children will be forked when
  195. ; new requests will connect. The following parameter are used:
  196. ; pm.max_children - the maximum number of children that
  197. ; can be alive at the same time.
  198. ; pm.process_idle_timeout - The number of seconds after which
  199. ; an idle process will be killed.
  200. ; Note: This value is mandatory.
  201. pm = dynamic
  202. ; The number of child processes to be created when pm is set to 'static' and the
  203. ; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
  204. ; This value sets the limit on the number of simultaneous requests that will be
  205. ; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
  206. ; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
  207. ; CGI. The below defaults are based on a server without much resources. Don't
  208. ; forget to tweak pm.* to fit your needs.
  209. ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
  210. ; Note: This value is mandatory.
  211. pm.max_children = 200
  212. ; The number of child processes created on startup.
  213. ; Note: Used only when pm is set to 'dynamic'
  214. ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
  215. pm.start_servers = 50
  216. ; The desired minimum number of idle server processes.
  217. ; Note: Used only when pm is set to 'dynamic'
  218. ; Note: Mandatory when pm is set to 'dynamic'
  219. pm.min_spare_servers = 50
  220. ; The desired maximum number of idle server processes.
  221. ; Note: Used only when pm is set to 'dynamic'
  222. ; Note: Mandatory when pm is set to 'dynamic'
  223. pm.max_spare_servers = 100
  224. ; The number of seconds after which an idle process will be killed.
  225. ; Note: Used only when pm is set to 'ondemand'
  226. ; Default Value: 10s
  227. ;pm.process_idle_timeout = 10s;
  228. ; The number of requests each child process should execute before respawning.
  229. ; This can be useful to work around memory leaks in 3rd party libraries. For
  230. ; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
  231. ; Default Value: 0
  232. pm.max_requests = 51200
  233. ; The URI to view the FPM status page. If this value is not set, no URI will be
  234. ; recognized as a status page. It shows the following informations:
  235. ; pool - the name of the pool;
  236. ; process manager - static, dynamic or ondemand;
  237. ; start time - the date and time FPM has started;
  238. ; start since - number of seconds since FPM has started;
  239. ; accepted conn - the number of request accepted by the pool;
  240. ; listen queue - the number of request in the queue of pending
  241. ; connections (see backlog in listen(2));
  242. ; max listen queue - the maximum number of requests in the queue
  243. ; of pending connections since FPM has started;
  244. ; listen queue len - the size of the socket queue of pending connections;
  245. ; idle processes - the number of idle processes;
  246. ; active processes - the number of active processes;
  247. ; total processes - the number of idle + active processes;
  248. ; max active processes - the maximum number of active processes since FPM
  249. ; has started;
  250. ; max children reached - number of times, the process limit has been reached,
  251. ; when pm tries to start more children (works only for
  252. ; pm 'dynamic' and 'ondemand');
  253. ; Value are updated in real time.
  254. ; Example output:
  255. ; pool: www
  256. ; process manager: static
  257. ; start time: 01/Jul/2011:17:53:49 +0200
  258. ; start since: 62636
  259. ; accepted conn: 190460
  260. ; listen queue: 0
  261. ; max listen queue: 1
  262. ; listen queue len: 42
  263. ; idle processes: 4
  264. ; active processes: 11
  265. ; total processes: 15
  266. ; max active processes: 12
  267. ; max children reached: 0
  268. ;
  269. ; By default the status page output is formatted as text/plain. Passing either
  270. ; 'html', 'xml' or 'json' in the query string will return the corresponding
  271. ; output syntax. Example:
  272. ; http://www.foo.bar/status
  273. ; http://www.foo.bar/status?json
  274. ; http://www.foo.bar/status?html
  275. ; http://www.foo.bar/status?xml
  276. ;
  277. ; By default the status page only outputs short status. Passing 'full' in the
  278. ; query string will also return status for each pool process.
  279. ; Example:
  280. ; http://www.foo.bar/status?full
  281. ; http://www.foo.bar/status?json&full
  282. ; http://www.foo.bar/status?html&full
  283. ; http://www.foo.bar/status?xml&full
  284. ; The Full status returns for each process:
  285. ; pid - the PID of the process;
  286. ; state - the state of the process (Idle, Running, ...);
  287. ; start time - the date and time the process has started;
  288. ; start since - the number of seconds since the process has started;
  289. ; requests - the number of requests the process has served;
  290. ; request duration - the duration in µs of the requests;
  291. ; request method - the request method (GET, POST, ...);
  292. ; request URI - the request URI with the query string;
  293. ; content length - the content length of the request (only with POST);
  294. ; user - the user (PHP_AUTH_USER) (or '-' if not set);
  295. ; script - the main script called (or '-' if not set);
  296. ; last request cpu - the %cpu the last request consumed
  297. ; it's always 0 if the process is not in Idle state
  298. ; because CPU calculation is done when the request
  299. ; processing has terminated;
  300. ; last request memory - the max amount of memory the last request consumed
  301. ; it's always 0 if the process is not in Idle state
  302. ; because memory calculation is done when the request
  303. ; processing has terminated;
  304. ; If the process is in Idle state, then informations are related to the
  305. ; last request the process has served. Otherwise informations are related to
  306. ; the current request being served.
  307. ; Example output:
  308. ; ************************
  309. ; pid: 31330
  310. ; state: Running
  311. ; start time: 01/Jul/2011:17:53:49 +0200
  312. ; start since: 63087
  313. ; requests: 12808
  314. ; request duration: 1250261
  315. ; request method: GET
  316. ; request URI: /test_mem.php?N=10000
  317. ; content length: 0
  318. ; user: -
  319. ; script: /home/fat/web/docs/php/test_mem.php
  320. ; last request cpu: 0.00
  321. ; last request memory: 0
  322. ;
  323. ; Note: There is a real-time FPM status monitoring sample web page available
  324. ; It's available in: /usr/local/php/share/php/fpm/status.html
  325. ;
  326. ; Note: The value must start with a leading slash (/). The value can be
  327. ; anything, but it may not be a good idea to use the .php extension or it
  328. ; may conflict with a real PHP file.
  329. ; Default Value: not set
  330. pm.status_path = /status
  331. ; The ping URI to call the monitoring page of FPM. If this value is not set, no
  332. ; URI will be recognized as a ping page. This could be used to test from outside
  333. ; that FPM is alive and responding, or to
  334. ; - create a graph of FPM availability (rrd or such);
  335. ; - remove a server from a group if it is not responding (load balancing);
  336. ; - trigger alerts for the operating team (24/7).
  337. ; Note: The value must start with a leading slash (/). The value can be
  338. ; anything, but it may not be a good idea to use the .php extension or it
  339. ; may conflict with a real PHP file.
  340. ; Default Value: not set
  341. ;ping.path = /ping
  342. ; This directive may be used to customize the response of a ping request. The
  343. ; response is formatted as text/plain with a 200 response code.
  344. ; Default Value: pong
  345. ;ping.response = pong
  346. ; The access log file
  347. ; Default: not set
  348. ;access.log = log/$pool.access.log
  349. ; The access log format.
  350. ; The following syntax is allowed
  351. ; %%: the '%' character
  352. ; %C: %CPU used by the request
  353. ; it can accept the following format:
  354. ; - %{user}C for user CPU only
  355. ; - %{system}C for system CPU only
  356. ; - %{total}C for user + system CPU (default)
  357. ; %d: time taken to serve the request
  358. ; it can accept the following format:
  359. ; - %{seconds}d (default)
  360. ; - %{miliseconds}d
  361. ; - %{mili}d
  362. ; - %{microseconds}d
  363. ; - %{micro}d
  364. ; %e: an environment variable (same as $_ENV or $_SERVER)
  365. ; it must be associated with embraces to specify the name of the env
  366. ; variable. Some exemples:
  367. ; - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
  368. ; - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
  369. ; %f: script filename
  370. ; %l: content-length of the request (for POST request only)
  371. ; %m: request method
  372. ; %M: peak of memory allocated by PHP
  373. ; it can accept the following format:
  374. ; - %{bytes}M (default)
  375. ; - %{kilobytes}M
  376. ; - %{kilo}M
  377. ; - %{megabytes}M
  378. ; - %{mega}M
  379. ; %n: pool name
  380. ; %o: output header
  381. ; it must be associated with embraces to specify the name of the header:
  382. ; - %{Content-Type}o
  383. ; - %{X-Powered-By}o
  384. ; - %{Transfert-Encoding}o
  385. ; - ....
  386. ; %p: PID of the child that serviced the request
  387. ; %P: PID of the parent of the child that serviced the request
  388. ; %q: the query string
  389. ; %Q: the '?' character if query string exists
  390. ; %r: the request URI (without the query string, see %q and %Q)
  391. ; %R: remote IP address
  392. ; %s: status (response code)
  393. ; %t: server time the request was received
  394. ; it can accept a strftime(3) format:
  395. ; %d/%b/%Y:%H:%M:%S %z (default)
  396. ; %T: time the log has been written (the request has finished)
  397. ; it can accept a strftime(3) format:
  398. ; %d/%b/%Y:%H:%M:%S %z (default)
  399. ; %u: remote user
  400. ;
  401. ; Default: "%R - %u %t \"%m %r\" %s"
  402. ;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
  403. ; The log file for slow requests
  404. ; Default Value: not set
  405. ; Note: slowlog is mandatory if request_slowlog_timeout is set
  406. slowlog = log/$pool.log.slow
  407. ; The timeout for serving a single request after which a PHP backtrace will be
  408. ; dumped to the 'slowlog' file. A value of '0s' means 'off'.
  409. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  410. ; Default Value: 0
  411. request_slowlog_timeout = 10
  412. ; The timeout for serving a single request after which the worker process will
  413. ; be killed. This option should be used when the 'max_execution_time' ini option
  414. ; does not stop script execution for some reason. A value of '0' means 'off'.
  415. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
  416. ; Default Value: 0
  417. request_terminate_timeout = 600
  418. ; Set open file descriptor rlimit.
  419. ; Default Value: system defined value
  420. rlimit_files = 10240
  421. ; Set max core size rlimit.
  422. ; Possible Values: 'unlimited' or an integer greater or equal to 0
  423. ; Default Value: system defined value
  424. ;rlimit_core = 0
  425. ; Chroot to this directory at the start. This value must be defined as an
  426. ; absolute path. When this value is not set, chroot is not used.
  427. ; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
  428. ; of its subdirectories. If the pool prefix is not set, the global prefix
  429. ; will be used instead.
  430. ; Note: chrooting is a great security feature and should be used whenever
  431. ; possible. However, all PHP paths will be relative to the chroot
  432. ; (error_log, sessions.save_path, ...).
  433. ; Default Value: not set
  434. ;chroot =
  435. ; Chdir to this directory at the start.
  436. ; Note: relative path can be used.
  437. ; Default Value: current directory or / when chroot
  438. ;chdir = /var/www
  439. ; Redirect worker stdout and stderr into main error log. If not set, stdout and
  440. ; stderr will be redirected to /dev/null according to FastCGI specs.
  441. ; Note: on highloaded environement, this can cause some delay in the page
  442. ; process time (several ms).
  443. ; Default Value: no
  444. ;catch_workers_output = yes
  445. ; Clear environment in FPM workers
  446. ; Prevents arbitrary environment variables from reaching FPM worker processes
  447. ; by clearing the environment in workers before env vars specified in this
  448. ; pool configuration are added.
  449. ; Setting to "no" will make all environment variables available to PHP code
  450. ; via getenv(), $_ENV and $_SERVER.
  451. ; Default Value: yes
  452. ;clear_env = no
  453. ; Limits the extensions of the main script FPM will allow to parse. This can
  454. ; prevent configuration mistakes on the web server side. You should only limit
  455. ; FPM to .php extensions to prevent malicious users to use other extensions to
  456. ; exectute php code.
  457. ; Note: set an empty value to allow all extensions.
  458. ; Default Value: .php
  459. ;security.limit_extensions = .php .php3 .php4 .php5
  460. ; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
  461. ; the current environment.
  462. ; Default Value: clean env
  463. ;env[HOSTNAME] = $HOSTNAME
  464. ;env[PATH] = /usr/local/bin:/usr/bin:/bin
  465. ;env[TMP] = /tmp
  466. ;env[TMPDIR] = /tmp
  467. ;env[TEMP] = /tmp
  468. ; Additional php.ini defines, specific to this pool of workers. These settings
  469. ; overwrite the values previously defined in the php.ini. The directives are the
  470. ; same as the PHP SAPI:
  471. ; php_value/php_flag - you can set classic ini defines which can
  472. ; be overwritten from PHP call 'ini_set'.
  473. ; php_admin_value/php_admin_flag - these directives won't be overwritten by
  474. ; PHP call 'ini_set'
  475. ; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
  476. ; Defining 'extension' will load the corresponding shared extension from
  477. ; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
  478. ; overwrite previously defined php.ini values, but will append the new value
  479. ; instead.
  480. ; Note: path INI options can be relative and will be expanded with the prefix
  481. ; (pool, global or /usr/local/php)
  482. ; Default Value: nothing is defined by default except the values in php.ini and
  483. ; specified at startup with the -d argument
  484. ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
  485. ;php_flag[display_errors] = off
  486. ;php_admin_value[error_log] = /var/log/fpm-php.www.log
  487. ;php_admin_flag[log_errors] = on
  488. ;php_admin_value[memory_limit] = 32M

准备好php.ini文件
构建php镜像

docker build -t php:v1 .

前端项目镜像构建与部署:Tomcat

准备好tomcat包:apache-tomcat-8.5.43.tar.gz
准备好tomcat应用测试包:ROOT.war
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/*

ADD apache-tomcat-${VERSION}.tar.gz /usr/local/
RUN mv /usr/local/apache-tomcat-${VERSION} /usr/local/tomcat && \
    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"]

构建tomcat镜像

docker build -t tomcat:v1 .

Dockerfile2

FROM df_tomcat:v1
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ROOT.war /usr/local/tomcat/webapps/

基于tomcat:v1镜像构建应用镜像

docker build -t tomcat:v2 . -f Dockerfile2

前端项目镜像构建与部署:Java

准备好jar包:hello.jar
Dockerfile

FROM java:8-jdk-alpine
LABEL maintainer www.ctnrs.com
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk add -U tzdata && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY hello.jar /
EXPOSE 8888
CMD ["/bin/sh", "-c", "java -jar $JAVA_OPTS /hello.jar"]

构建java镜像

docker build -t java:v1 .

案例:容器化搭建个人博客系统

image.png

1、自定义网络
docker network create lnmp

2、创建Mysql容器
docker run -d \
--name lnmp_mysql \
--net lnmp \
--mount src=mysql-vol,dst=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASwordpress mysql:5.7 --character-set-server=utf8

3、创建PHP容器
docker run -d \
--name lnmp_php \
--net lnmp \
--mount src=wwwroot,dst=/wwwroot php:v1

4、创建Nginx容器
docker run -d \
--name lnmp_nginx \
--net lnmp \
-p 88:80 \
--mount src=wwwroot,dst=/wwwroot \
--mount type=build,src=$PWD/php.conf,dts=/usr/local/nginx/conf/vhost/php.conf nginx:v1

5、以wordpress博客为例
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
tar -zxvf /wordpress-4.9.4-zh_CN.tar.gz -C /var/lib/docker/volumes/wwwroot/_data/
mv wordpress/* /var/lib/docker/volumes/wwwroot/_data/

6、浏览器访问http://IP:88

编写Dockerfile最佳实战

  • 减少镜像层:一次RUN指令形成新的一层,尽量Shell命令都写在一行,减少镜像层。
  • 优化镜像大小:一次RUN形成新的一层,如果没有在同一层删除,无论文件是否最后删除,都会带到下一层,所以要在每一层清理对应的残留数据,减小镜像大小。
  • 减少网络传输时间:例如软件包、mvn仓库等。
  • 多阶段构建:代码编译、部署在一个Dockerfile完成,只会保留部署阶段产生的数据。
  • 选择最小的基础镜像:例如alpine。