Docker鏡像建立方法有三種,分別爲基於已有鏡像建立、基於本地模板建立以及基於Dockerfile建立。html
這裏主要記錄下基於Dockerfile建立方法,Dockerfile是由一組指令組成的文件,其中每條指令對應Linux中的一條命令,Docker程序將讀取Dockerfile中的指令生成指定鏡像。web
Dockerfile結構大體分爲四個部分:基礎鏡像信息、維護者信息、鏡像操做指令和容器啓動時執行指令 。Dockerfile每行支持一條指令,每條指令可帶多個參數,支持使用以#號開頭的註釋。docker
指令 | 含義 |
---|---|
FROM鏡像 | 指定新鏡像所基於的鏡像,必須爲第一條指令 |
MAINTAINER 名字 | 新鏡像的維護人信息 |
RUN 命令 | 在所基於的鏡像上執行命令,並提交到新鏡像中 |
EXPOSE端口號 | 指定新鏡像加載到Docker時開啓的端口號 |
ENV 環境變量 變量值 | 設置一個環境變量的值,會被後面的RUN使用 |
ADD 源文件/目錄 目標文件/目錄 | 將源文件複製到目標文件,源文件要與Docker位於同一目錄下,或者爲一個URL |
COPY 源文件/目錄 目標文件/目錄 | 將本地主機上的源文件/目錄複製到目標地點,源文件/目錄要與Dockerfile在同一目錄下 |
VOLUME["目錄"] | 在容器中建立一個掛載點 |
USER 用戶名 /UID | 指定運行容器時的用戶 |
WORKDIR 路徑 | 爲後續的RUN、CMD、ENTRYPOINT指定工做目錄 |
ONBUILD命令 | 指定所生成的鏡像做爲一個基礎鏡像時所要運行的命令 |
CMD["要運行的程序","參數1","參數2"] | 指定啓動容器時運行的命令或腳本,只能有一條CMD命令,多條時只有最後一條被執行 |
mkdir apache cd apache/
vim Dockerfileapache
#基於centos鏡像 FROM centos #維護人的信息 MAINTAINER The CentOS Project <303323496@qq.com> #安裝httpd軟件包 RUN yum -y update RUN yum -y install httpd #開啓80端口 EXPOSE 80 #複製網站首頁文件至鏡像中web站點下 ADD index.html /var/www/html/index.html #複製該腳本至鏡像中,並修改其權限 ADD run.sh /run.sh RUN chmod 775 /run.sh #當啓動容器時執行的腳本文件 CMD ["/run.sh"]
vim run.shvim
#!/bin/bash #清空httpd緩存文件 rm -rf /run/httpd/* #啓動httpd服務 exec /usr/sbin/apachectl -D FOREGROUND
echo "Thiss is Nginx webapp." > index.html
ls
docker build -t httpd:centos .centos
Sending build context to Docker daemon 4.096kB Step 1/9 : FROM centos ---> 5182e96772bf Step 2/9 : MAINTAINER The CentOS Project <303323496@qq.com> ---> Running in 6d7c8eae519d ---> 46fc589066ff Removing intermediate container 6d7c8eae519d Step 3/9 : RUN yum -y update ---> Running in 444105af6041 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.zju.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package centos-release.x86_64 0:7-5.1804.1.el7.centos will be updated ---> Package centos-release.x86_64 0:7-5.1804.4.el7.centos will be an update --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Updating: centos-release x86_64 7-5.1804.4.el7.centos updates 25 k Transaction Summary ================================================================================ Upgrade 1 Package Total download size: 25 k Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. Public key for centos-release-7-5.1804.4.el7.centos.x86_64.rpm is not installed Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 warning: /var/cache/yum/x86_64/7/updates/packages/centos-release-7-5.1804.4.el7.centos.x86_64.rpm:Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-5.1804.1.el7.centos.x86_64 (@Updates) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Updating : centos-release-7-5.1804.4.el7.centos.x86_64 1/2 Cleanup : centos-release-7-5.1804.1.el7.centos.x86_64 2/2 Verifying : centos-release-7-5.1804.4.el7.centos.x86_64 1/2 Verifying : centos-release-7-5.1804.1.el7.centos.x86_64 2/2 Updated: centos-release.x86_64 0:7-5.1804.4.el7.centos Complete! ---> 8a4bb67da908 Removing intermediate container 444105af6041 Step 4/9 : RUN yum -y install httpd ---> Running in 982f008f5f3c Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed --> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed ---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed ---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed ---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.4.6-80.el7.centos.1 updates 2.7 M Installing for dependencies: apr x86_64 1.4.8-3.el7_4.1 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k centos-logos noarch 70.0.6-3.el7.centos base 21 M httpd-tools x86_64 2.4.6-80.el7.centos.1 updates 90 k mailcap noarch 2.1.41-2.el7 base 31 k Transaction Summary ================================================================================ Install 1 Package (+5 Dependent packages) Total download size: 24 M Installed size: 31 M Downloading packages: -------------------------------------------------------------------------------- Total 1.1 MB/s | 24 MB 00:21 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.4.8-3.el7_4.1.x86_64 1/6 Installing : apr-util-1.5.2-6.el7.x86_64 2/6 Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64 3/6 Installing : centos-logos-70.0.6-3.el7.centos.noarch 4/6 Installing : mailcap-2.1.41-2.el7.noarch 5/6 Installing : httpd-2.4.6-80.el7.centos.1.x86_64 6/6 Verifying : mailcap-2.1.41-2.el7.noarch 1/6 Verifying : httpd-tools-2.4.6-80.el7.centos.1.x86_64 2/6 Verifying : apr-util-1.5.2-6.el7.x86_64 3/6 Verifying : httpd-2.4.6-80.el7.centos.1.x86_64 4/6 Verifying : apr-1.4.8-3.el7_4.1.x86_64 5/6 Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6 Installed: httpd.x86_64 0:2.4.6-80.el7.centos.1 Dependency Installed: apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 centos-logos.noarch 0:70.0.6-3.el7.centos httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7 Complete! ---> d3f0d6f963cb Removing intermediate container 982f008f5f3c Step 5/9 : EXPOSE 80 ---> Running in b511725feca7 ---> 7f7887f5875e Removing intermediate container b511725feca7 Step 6/9 : ADD index.html /var/www/html/index.html ---> 3c5953c9fd43 Removing intermediate container 85dad653f265 Step 7/9 : ADD run.sh /run.sh ---> 41980651502b Removing intermediate container 91ff7e24daa4 Step 8/9 : RUN chmod 775 /run.sh ---> Running in 7aebb5d8f194 ---> f673460e23c7 Removing intermediate container 7aebb5d8f194 Step 9/9 : CMD /run.sh ---> Running in 9c0f5c4f1e32 ---> 551b9fc524ba Removing intermediate container 9c0f5c4f1e32 Successfully built 551b9fc524ba Successfully tagged httpd:centos
docker run -d -p 80:80 --name=httpd httpd:centos
docker ps
http://192.168.96.44:80