dockerfile 是用來構(gòu)建docker鏡像的文件!命令參數(shù)腳本!
構(gòu)建步驟:
- 編寫一個(gè)dockerfile 文件
- docker build 構(gòu)建成一個(gè)鏡像
- docker run 運(yùn)行鏡像
- docker push 發(fā)布鏡像(DockerHub、阿里云鏡像倉庫)
# 以contes為例 查看 dockerhub 上 contes 的 dockerfile
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20200809" \
org.opencontainers.image.title="CentOS Base Image" \
org.opencontainers.image.vendor="CentOS" \
org.opencontainers.image.licenses="GPL-2.0-only" \
org.opencontainers.image.created="2020-08-09 00:00:00+01:00"
CMD ["/bin/bash"]
DockerFile構(gòu)建過程
基礎(chǔ)知識(shí):
- 每個(gè)保留關(guān)鍵字(指令)都必須是大寫字母
- 執(zhí)行順序從上到下順序執(zhí)行
- 表示注釋
- 每個(gè)指令都會(huì)創(chuàng)建提交一個(gè)新的鏡像層,并提交
dockerfile是面向開發(fā)的,我們以后要發(fā)布項(xiàng)目,做鏡像,就需要編寫dockerfile文件,這個(gè)文件十分簡單!
Docker鏡像逐漸成為企業(yè)交付的標(biāo)準(zhǔn)!
DockerFile:構(gòu)建鏡像,定義了一切的步驟,源代碼;
DockerImages:通過DockerFile構(gòu)建生成的一個(gè)鏡像,這個(gè)鏡像就是我們最終發(fā)布和運(yùn)行的產(chǎn)品!
Docker容器:容器就是鏡像運(yùn)行起來提供服務(wù)!
DockerFile的指令
FROM # 基礎(chǔ)鏡像
MAINTAINER # 鏡像是誰寫的 姓名 + 郵箱
RUN # 鏡像構(gòu)建的時(shí)候需要運(yùn)行的命令
ADD # 步驟 eg:tomcat鏡像---》放入tomcat的壓縮包!添加內(nèi)容
WORKDIR # 鏡像的工作目錄
VOLUME # 掛載的目錄位置
EXPOST # 暴露端口配置
CMD # 指定這個(gè)容器啟動(dòng)的時(shí)候要運(yùn)行的命令,只有最后一個(gè)會(huì)生效,可被替代
ENTRYPOINT # 指定這個(gè)容器啟動(dòng)的時(shí)候要運(yùn)行的命令,可以追加命令
ONBUILD # 當(dāng)構(gòu)建一個(gè)被繼承的 DockerFile ,這個(gè)時(shí)候就會(huì)運(yùn)行 ONBUILD 的指令,觸發(fā)指令
COPY # 類似ADD,將我們的文件拷貝到鏡像中
ENV # 構(gòu)建的時(shí)候設(shè)置環(huán)境變量
實(shí)戰(zhàn)測(cè)試
Docker Hub 中 99% 的鏡像都是從 FROM scratch 這個(gè)基礎(chǔ)鏡像過來的 ,然后配置我們需要的軟件和配置來進(jìn)行構(gòu)建
創(chuàng)建一個(gè)自己的centos
# 1、編寫DockerFile的文件
FROM centos
# 作者信息
MAINTAINER yinxiaodong<m15010969094@163.com>
# 配置環(huán)境變量
ENV MYPATH /user/local
# 指定鏡像的工作目錄
WORKDIR $MYPATH
RUN yum install -y vim
RUN yum install -y net-tools
# 暴露80端口
EXPOSE 80
CMD echo $MYPATH
CMD echo "---------end--------"
CMD /bin/bash
# 2、通過文件構(gòu)建鏡像
# 命令 docker build -f dockerfile文件路徑 -t 鏡像名:版本號(hào) .
[root@localhost dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/9 : FROM centos
---> 0d120b6ccaa8
Step 2/9 : MAINTAINER yinxiaodong<m15010969094@163.com>
---> Running in f79b12ffb083
Removing intermediate container f79b12ffb083
---> 8ee53d3f7a65
Step 3/9 : ENV MYPATH /user/local
---> Running in 659a2c96d5f7
Removing intermediate container 659a2c96d5f7
---> 59ab131ef44c
Step 4/9 : WORKDIR $MYPATH
---> Running in 0b4b8f9c65bb
Removing intermediate container 0b4b8f9c65bb
---> 408b06671488
Step 5/9 : RUN yum install -y net-tools
---> Running in 151f81148a87
CentOS-8 - AppStream 118 kB/s | 5.8 MB 00:50
CentOS-8 - Base 218 kB/s | 2.2 MB 00:10
CentOS-8 - Extras 1.8 kB/s | 7.3 kB 00:04
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 323 k
Installed size: 1.0 M
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm 219 kB/s | 323 kB 00:01
--------------------------------------------------------------------------------
Total 66 kB/s | 323 kB 00:04
warning: /var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - Base 162 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.51.20160912git.el8.x86_64
Complete!
Removing intermediate container 151f81148a87
---> c0a0546c9b2a
Step 6/9 : EXPOSE 80
---> Running in 42000936515d
Removing intermediate container 42000936515d
---> fe68114ecf3f
Step 7/9 : CMD echo $MYPATH
---> Running in c393fc53a354
Removing intermediate container c393fc53a354
---> a6924276bf90
Step 8/9 : CMD echo "---------end--------"
---> Running in 5994de56f0a1
Removing intermediate container 5994de56f0a1
---> a8ba0ebb3770
Step 9/9 : CMD /bin/bash
---> Running in d1fa2d436363
Removing intermediate container d1fa2d436363
---> 41bb76be4884
Successfully built 41bb76be4884
Successfully tagged mycentos:0.1
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 41bb76be4884 7 seconds ago 246MB
centos 1.0 d840628c30a9 6 hours ago 215MB
centos latest 0d120b6ccaa8 2 days ago 215MB
# 啟動(dòng) mycentos 使用 ifconfig 命令查看ip信息
[root@localhost dockerfile]# docker run -it mycentos:0.1
[root@494c2bc72263 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 6 bytes 516 (516.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 查看所在目錄 /user/local 正是我們?cè)赿ockerfile中指定的目錄
[root@494c2bc72263 local]# pwd
/user/local
查看鏡像的構(gòu)建歷史記錄
# 命令 docker history 鏡像id
[root@localhost ~]# docker history 41bb76be4884
IMAGE CREATED CREATED BY SIZE COMMENT
41bb76be4884 16 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
a8ba0ebb3770 16 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
a6924276bf90 16 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
fe68114ecf3f 16 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
c0a0546c9b2a 16 minutes ago /bin/sh -c yum install -y net-tools 31.3MB
408b06671488 17 minutes ago /bin/sh -c #(nop) WORKDIR /user/local 0B
59ab131ef44c 17 minutes ago /bin/sh -c #(nop) ENV MYPATH=/user/local 0B
8ee53d3f7a65 17 minutes ago /bin/sh -c #(nop) MAINTAINER yinxiaodong<m1… 0B
0d120b6ccaa8 2 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 days ago /bin/sh -c #(nop) ADD file:538afc0c5c964ce0d… 215MB
我們平時(shí)拿到一個(gè)鏡像,可以研究一下它是怎么做的---->docker history 鏡像id
CMD 和 ENTRYPOINT 的區(qū)別
CMD # 指定這個(gè)容器啟動(dòng)的時(shí)候要運(yùn)行的命令,只有最后一個(gè)會(huì)生效,可被替代
ENTRYPOINT # 指定這個(gè)容器啟動(dòng)的時(shí)候要運(yùn)行的命令,可以追加命令
# 測(cè)試 CMD
# 編寫DockerFile的文件
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
# 根據(jù) DockerFile 構(gòu)建鏡像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.1 .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 0d120b6ccaa8
Step 2/2 : CMD ["ls","-a"]
---> Running in b3f8ba72222b
Removing intermediate container b3f8ba72222b
---> 561e47f88730
Successfully built 561e47f88730
Successfully tagged testcmd:0.1 # 構(gòu)建成功
# 查看鏡像
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testcmd 0.1 561e47f88730 6 seconds ago 215MB
centos latest 0d120b6ccaa8 2 days ago 215MB
# 啟動(dòng)鏡像 發(fā)現(xiàn)ls -a命令生效
[root@localhost dockerfile]# docker run -it testcmd:0.1
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
# 啟動(dòng)命令中 追加一個(gè) -l, 我們期望的是 ls -a -l,但是 報(bào)錯(cuò),這里將 ls -a 替換成了 -l,最終的命令是 -l 所以報(bào)錯(cuò)。
[root@localhost dockerfile]# docker run -it 561e47f88730 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH": unknown.
# 將啟動(dòng)命令替換成 docker run -it testcmd:0.1 ls -al, 成功打印詳細(xì)信息;
[root@localhost dockerfile]# docker run -it testcmd:0.1 ls -al
total 0
drwxr-xr-x. 1 root root 6 Aug 13 08:20 .
drwxr-xr-x. 1 root root 6 Aug 13 08:20 ..
-rwxr-xr-x. 1 root root 0 Aug 13 08:20 .dockerenv
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 13 08:20 dev
drwxr-xr-x. 1 root root 66 Aug 13 08:20 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 123 root root 0 Aug 13 08:20 proc
dr-xr-x---. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 11 root root 163 Aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var
# 測(cè)試二:現(xiàn)在我們把 DockerFile 中 CMD 替換成 ENTRYPOINT 后重新構(gòu)建,運(yùn)行 看看結(jié)果
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
ENTRYPOINT ["ls","-a"]
# 重新構(gòu)建鏡像
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t testcmd:0.2 .
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 0d120b6ccaa8
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in c634ca09fabe
Removing intermediate container c634ca09fabe
---> 52d295395f08
Successfully built 52d295395f08
Successfully tagged testcmd:0.2
# 查看鏡像
[root@localhost dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
testcmd 0.2 52d295395f08 7 seconds ago 215MB
testcmd 0.1 561e47f88730 12 minutes ago 215MB
centos latest 0d120b6ccaa8 2 days ago 215MB
# 運(yùn)行testcmd:0.2鏡像 并追加 -l ,發(fā)現(xiàn) 打印出了詳細(xì)信息
[root@localhost dockerfile]# docker run -it testcmd:0.2 -l
total 0
drwxr-xr-x. 1 root root 6 Aug 13 08:17 .
drwxr-xr-x. 1 root root 6 Aug 13 08:17 ..
-rwxr-xr-x. 1 root root 0 Aug 13 08:17 .dockerenv
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Aug 13 08:17 dev
drwxr-xr-x. 1 root root 66 Aug 13 08:17 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 121 root root 0 Aug 13 08:17 proc
dr-xr-x---. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 11 root root 163 Aug 9 21:40 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Aug 11 09:58 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var
DockerFile中很多命令都十分相似,我們需要了解他的區(qū)別,最好的學(xué)習(xí)就是對(duì)比進(jìn)行測(cè)試查看效果!
實(shí)戰(zhàn):Tomcat鏡像
1、準(zhǔn)備鏡像文件 tomcat壓縮包 jdk壓縮包
[root@localhost tomcat]# ls
apache-tomcat-8.5.43.tar.gz Dockerfile jdk-8u211-linux-x64.tar.gz read.txt
2、編寫dockerfile文件, 官方命名Dockerfile,build會(huì)自動(dòng)尋找這個(gè)文件,不需要 -f 指定!
# 引入基礎(chǔ)
FROM centos
# 作者信息
MAINTAINER yinxiaodoong<m15010969094@163.com>
# copy文件read.txt到容器的 /usr/local/read.txt 目錄下
COPY read.txt /usr/local/read.txt
# 添加jdk tomcat ---> 自動(dòng)解壓
ADD jdk-8u211-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.43.tar.gz /usr/local/
# 安裝 vim 命令
RUN yum install -y vim
# 配置 MYPATH
ENV MYPATH /usr/local/
# 指定 進(jìn)入容器默認(rèn)路徑 /user/local/
WORKDIR $MYPATH
# 配置jdk環(huán)境變量
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV CLASSPATH $JAVA_HOME/lib/dt.jat:$JAVA_HOME/lib/tools.jar
# 配置tomcat環(huán)境變量
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.43
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.43
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 暴露8080端口
EXPOSE 8080
# /usr/local/tomcat/webapps#
# 啟動(dòng)容器時(shí) 啟動(dòng)tomcat
CMD /usr/local/apache-tomcat-8.5.43/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.5.43/logs/catalina.out
# 構(gòu)建tomcat鏡像
[root@localhost tomcat]# docker build -t mytomcat
[root@localhost tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat latest a9707559afa1 8 seconds ago 693MB
centos latest 0d120b6ccaa8 4 days ago 215MB
[root@localhost tomcat]# docker run -d -p 8081:8080 --name mytomcat--01 -v /home/yinxiaodong/build/tomcat/test:/usr/local/apache-tomcat-8.5.43/webapps/test -v /home/yinxiaodong/build/tomcat/logs:/usr/local/apache-tomcat-8.5.43/logs mytomcat
發(fā)布自己的鏡像
發(fā)布到 dockerHub
地址 https://hub.docker.com/ 注冊(cè)自己的賬號(hào)
確定自己的賬號(hào)可以登錄
在我們的服務(wù)器上提交
[root@localhost logs]# docker login --help
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
[root@localhost logs]#
[root@localhost logs]# docker login -u xxx -p xxx
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
登錄完畢后就可以提交鏡像了
[root@localhost logs]# docker push xxx/mytomcat:1.0
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。