- Learn OpenShift
- Denis Zuev Artemii Kropachev Aleksey Usov
- 371字
- 2021-08-13 16:03:49
Dockerfile instructions
When the docker build command is run, Docker reads the provided Dockerfile from top to bottom, creating a separate layer for every instruction and placing it in the internal cache. If an instruction from Dockerfile is updated, it invalidates the respective caching layer and every subsequent one, forcing Docker to rebuild them when the docker build command is run again. Therefore, it's more effective to place the most malleable instructions at the end of Dockerfile, so that the number of invalidated layers is minimized and cache usage is maximized. For example, suppose we have a Dockerfile with the following contents:
$ cat Dockerfile
FROM centos:latest
RUN yum -y update
RUN yum -y install nginx, mariadb, php5, php5-mysql
RUN yum -y install httpd
CMD ["nginx", "-g", "daemon off;"]
In the example, if you choose to use MySQL instead of MariaDB, the layer created by the second RUN command, as well as the third one, will be invalidated, which for complex images means a noticeably longer build process.
Consider the following example. Docker includes images for minimal OSes. These base images can be used to build custom images on top of them. In the example, we will be using a CentOS 7 base image to create a web server container from scratch:
- First, we need to create a project directory:
$ mkdir custom_project; cd custom_project
Then, we create a Dockerfile with the following content:
$ cat Dockerfile
FROM centos:7
RUN yum install httpd -y
COPY index.html /var/www/html/index.html
ENTRYPOINT ["/usr/sbin/httpd","-D","FOREGROUND"]
- Create the index.html file:
$ echo "A new cool image" > index.html
- Build the image using docker build:
$ docker build -t new_httpd_image .
Sending build context to Docker daemon 3.072 kB
...
output truncated for brevity
...
Successfully built 4f2f77cd3026
- Finally, we can check that the new image exists and has all the required image layers:
$ docker history new_httpd_image
IMAGE CREATED CREATED BY SIZE COMMENT
4f2f77cd3026 20 hours ago /bin/sh -c #(nop) ENTRYPOINT ["/usr/sbin/htt 0 B
8f6eaacaae3c 20 hours ago /bin/sh -c #(nop) COPY file:318d7f73d4297ec33 17 B
e19d80cc688a 20 hours ago /bin/sh -c yum install httpd -y 129 MB
...
output truncated for brevity
...
- 大學(xué)計算機(jī)應(yīng)用基礎(chǔ)實踐教程(Windows 7+Office 2013)
- 白話區(qū)塊鏈
- Learning Android Intents
- Android物聯(lián)網(wǎng)開發(fā)細(xì)致入門與最佳實踐
- 嵌入式系統(tǒng)及其應(yīng)用(第三版)
- Linux服務(wù)器配置與管理
- Linux基礎(chǔ)使用與案例
- Windows 7實戰(zhàn)從入門到精通(超值版)
- 應(yīng)急指揮信息系統(tǒng)設(shè)計
- Raspberry Pi入門指南
- Windows 8完全自學(xué)手冊
- BuddyPress Theme Development
- Windows 8玩全不求人
- Azure Serverless Computing Cookbook
- 分布式實時處理系統(tǒng):原理、架構(gòu)與實現(xiàn)