dockerfile中的指令
构建镜像
docker build . -t 仓库地址/仓库名称/镜像名称:标签
在dockerfile目录下构建
文件名为Dockerfile
FORM
# 指定基础镜像的下载位置,从哪里拉取进行
FROM one/nginx:v1
USER
# 指定运行的用户
FROM one/nginx:v1
USER nginx
WORKDIR
# 容器中切换目录
FROM one/nginx:v1
USER nginx
WORKDIR /usr/share/nginx/html
[root@fengling dockerfile]# cat Dockerfile
FROM 81.70.195.225/one/nginx:v1
USER nginx
WORKDIR /usr/share/nginx/html
[root@fengling dockerfile]# docker build . -t 81.70.195.225/one/nginx:v2
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM 81.70.195.225/one/nginx:v1
---> c39a868aad02
Step 2/3 : USER nginx
---> Running in 656bf9ea1e48
Removing intermediate container 656bf9ea1e48
---> f4ae19b10e34
Step 3/3 : WORKDIR /usr/share/nginx/html
---> Running in bb23a7c5525d
Removing intermediate container bb23a7c5525d
---> f541f62a8565
Successfully built f541f62a8565
Successfully tagged 81.70.195.225/one/nginx:v2
# 查看构建是否成功
[root@fengling dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
81.70.195.225/one/nginx v2 f541f62a8565 About a minute ago 133MB
81.70.195.225/one/nginx v1 c39a868aad02 8 days ago 133MB
nginx latest c39a868aad02 8 days ago 133MB
alpine latest d6e46aa2470d 3 weeks ago 5.57MB
one1/alpine v3.10.1 d6e46aa2470d 3 weeks ago 5.57MB
# 启动容器,查看是否和我们构建配置一样
# 用户为nginx
# 位置在/usr/share/nginx/html下
[root@fengling dockerfile]# docker run -it --rm --name nginx-v2 f541f62a8565 /bin/bash
nginx@6411f64bd5e4:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
nginx@6411f64bd5e4:/usr/share/nginx/html$ whoami
nginx
nginx@6411f64bd5e4:/usr/share/nginx/html$
ADD
# 将本地资源固化到容器中
ADD index.html /usr/share/nginx/html/index.html
RXPOSE
# 暴露端口
EXPOSE 80
[root@fengling dockerfile]# cat index.html
<h1>nginx 内容,移动到容器中</h1>
[root@fengling dockerfile]# cat Dockerfile
FROM 81.70.195.225/one/nginx:v1
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80
[root@fengling dockerfile]# docker build . -t 81.70.195.225/one/ngninx:v3
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM 81.70.195.225/one/nginx:v1
---> c39a868aad02
Step 2/3 : ADD index.html /usr/share/nginx/html/index.html
---> 202e4b26d584
Step 3/3 : EXPOSE 80
---> Running in fff7ab6cee18
Removing intermediate container fff7ab6cee18
---> bc47c3b53067
Successfully built bc47c3b53067
Successfully tagged 81.70.195.225/one/ngninx:v3
[root@fengling dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
81.70.195.225/one/ngninx v3 bc47c3b53067 About a minute ago 133MB
81.70.195.225/one/nginx v2 f541f62a8565 22 minutes ago 133MB
nginx latest c39a868aad02 8 days ago 133MB
81.70.195.225/one/nginx v1 c39a868aad02 8 days ago 133MB
alpine latest d6e46aa2470d 3 weeks ago 5.57MB
one1/alpine v3.10.1 d6e46aa2470d 3 weeks ago 5.57MB
# 直接后台运行的化就不用在手动启动nginx了
# docker run -d -p 8080:80 --name nginx-v3 81.70.195.225/one/ngninx:v3
[root@fengling dockerfile]# docker run --rm -it -p 8080:80 --name nginx-v3 81.70.195.225/one/ngninx:v3 /bin/bash
root@5d94b606a310:/#
root@5d94b606a310:/# nginx # 启动nginx
root@5d94b606a310:/# 111.196.240.202 - - [14/Nov/2020:01:58:02 +0000] "GET / HTTP/1.1" 200 43 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
2020/11/14 01:58:03 [error] 13#13: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.196.240.202, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "39.96.7.141:8080"
111.196.240.202 - - [14/Nov/2020:01:58:03 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
2020/11/14 01:58:03 [error] 13#13: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.196.240.202, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "39.96.7.141:8080"
111.196.240.202 - - [14/Nov/2020:01:58:03 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
# 宿主机上,查看映射端口
[root@fengling ~]# netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21964/mysqld
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 10017/redis-server
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2075/sshd
tcp 0 0 0.0.0.0:65529 0.0.0.0:* LISTEN 8775/xr
tcp6 0 0 :::8080 :::* LISTEN 31079/docker-proxy
udp 0 0 0.0.0.0:68 0.0.0.0:* 676/dhclient
udp 0 0 172.17.0.1:123 0.0.0.0:* 764/ntpd
udp 0 0 172.24.16.171:123 0.0.0.0:* 764/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 764/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 764/ntpd
udp 0 0 0.0.0.0:28795 0.0.0.0:* 676/dhclient
udp6 0 0 :::123 :::* 764/ntpd
udp6 0 0 :::63304 :::* 676/dhclient
使用宿主机IP访问,访问到了,乱码是因为编码格式的问题,现在不必理会
RUN
# 指定镜像构建时执行的命令
RUN yum -y install bind
ENV
# 指定环境变量
ENV VER export-libs.x86_64
# 先下载centos的镜像
[root@fengling dockerfile]# docker pull docker.io/centos:centos7
centos7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:8b65474bd89ef48e04c21db31d1b283558b114f7d34a001cdbaed747bc4dbd16
Status: Downloaded newer image for centos:centos7
docker.io/library/centos:centos7
# 重新打个tag
root@fengling dockerfile]# docker tag centos:centos7 81.70.195.225/one/centos:v1
[root@fengling dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
81.70.195.225/one/ngninx v3 bc47c3b53067 24 minutes ago 133MB
81.70.195.225/one/nginx v2 f541f62a8565 46 minutes ago 133MB
81.70.195.225/one/centos v1 8652b9f0cb4c 2 hours ago 204MB
centos centos7 8652b9f0cb4c 2 hours ago 204MB
81.70.195.225/one/nginx v1 c39a868aad02 8 days ago 133MB
nginx latest c39a868aad02 8 days ago 133MB
one1/alpine v3.10.1 d6e46aa2470d 3 weeks ago 5.57MB
alpine latest d6e46aa2470d 3 weeks ago 5.57MB
# 上传到私用仓库
root@fengling dockerfile]# docker push 81.70.195.225/one/centos:v1
The push refers to repository [81.70.195.225/one/centos]
174f56854903: Pushed
v1: digest: sha256:e4ca2ed0202e76be184e75fb26d14bf974193579039d5573fb2348664deef76e size: 529
# Dockerfile文件
FROM 81.70.195.225/one/centos:v1
ENV VER export-libs.x86_64
RUN yum -y install bind-$VER
# 构建镜像
[root@fengling dockerfile]# docker build . -t 81.70.195.225/one/centos-bind:v1
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM 81.70.195.225/one/centos:v1
---> 8652b9f0cb4c
Step 2/3 : ENV VER export-libs.x86_64
---> Running in 5ca1aff2209c
Removing intermediate container 5ca1aff2209c
---> 8b08d0e48728
Step 3/3 : RUN yum -y install bind-$VER
---> Running in 2c88c22dcd24
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.bfsu.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package bind-export-libs.x86_64 32:9.11.4-26.P2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
bind-export-libs x86_64 32:9.11.4-26.P2.el7 base 1.1 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 1.1 M
Installed size: 2.9 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/bind-export-libs-9.11.4-26.P2.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for bind-export-libs-9.11.4-26.P2.el7.x86_64.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
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-9.2009.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 32:bind-export-libs-9.11.4-26.P2.el7.x86_64 1/1
Verifying : 32:bind-export-libs-9.11.4-26.P2.el7.x86_64 1/1
Installed:
bind-export-libs.x86_64 32:9.11.4-26.P2.el7
Complete!
Removing intermediate container 2c88c22dcd24
---> d9af25a35c4b
Successfully built d9af25a35c4b
Successfully tagged 81.70.195.225/one/centos-bind:v1
[root@fengling dockerfile]# docker run -it --rm --name centos-bind 81.70.195.225/one/centos-bind:v1 /bin/bash
[root@c285b49717b1 /]# echo $VER
export-libs.x86_64
[root@c285b49717b1 /]# rpm -qa bind*
bind-export-libs-9.11.4-26.P2.el7.x86_64
bind-license-9.11.4-26.P2.el7.noarch
CMD
CMD ["httpd", "-D", "FOREGROUND"]
实例
FROM 81.70.195.225/one/centos:v1
RUN yum -y install httpd
CMD ["httpd", "-D", "FOREGROUND"]
[root@fengling dockerfile]# docker build . -t http:v1
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM 81.70.195.225/one/centos:v1
---> 8652b9f0cb4c
Step 2/3 : RUN yum -y install httpd
---> Running in be4b396ff257
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-95.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-95.el7.centos for package: httpd-2.4.6-95.el7.centos.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-95.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-95.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-95.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-95.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 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-95.el7.centos 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-95.el7.centos base 2.7 M
Installing for dependencies:
apr x86_64 1.4.8-7.el7 base 104 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-95.el7.centos base 93 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: 32 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/apr-1.4.8-7.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for apr-1.4.8-7.el7.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total 17 MB/s | 24 MB 00:01
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
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-9.2009.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-7.el7.x86_64 1/6
Installing : apr-util-1.5.2-6.el7.x86_64 2/6
Installing : httpd-tools-2.4.6-95.el7.centos.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-95.el7.centos.x86_64 6/6
Verifying : mailcap-2.1.41-2.el7.noarch 1/6
Verifying : apr-1.4.8-7.el7.x86_64 2/6
Verifying : apr-util-1.5.2-6.el7.x86_64 3/6
Verifying : httpd-tools-2.4.6-95.el7.centos.x86_64 4/6
Verifying : httpd-2.4.6-95.el7.centos.x86_64 5/6
Verifying : centos-logos-70.0.6-3.el7.centos.noarch 6/6
Installed:
httpd.x86_64 0:2.4.6-95.el7.centos
Dependency Installed:
apr.x86_64 0:1.4.8-7.el7
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-95.el7.centos
mailcap.noarch 0:2.1.41-2.el7
Complete!
Removing intermediate container be4b396ff257
---> d6691512a242
Step 3/3 : CMD ["httpd", "-D", "FOREGROUND"]
---> Running in 0df4085c037a
Removing intermediate container 0df4085c037a
---> 2f6382c01dc9
Successfully built 2f6382c01dc9
Successfully tagged http:v1
[root@fengling dockerfile]# docker run -itd --rm --name httpd-v1 -p8080:80 http:v1
0bc01c6ac6468b16be68e7710721f8aff5ea5a3c745a636ea6e7dca752307c22
[root@fengling dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0bc01c6ac646 http:v1 "httpd -D FOREGROUND" 6 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp httpd-v1
c285b49717b1 81.70.195.225/one/centos-bind:v1 "/bin/bash" About an hour ago Up About an hour centos-bind
ENTRYPOINT
ENTRYPOINT /entrypoint.sh
实例:
FROM 81.70.195.225/one/centos:v1
ADD entrypoint.sh /entrypoint.sh
RUN yum -q -y install epel-release && yum -y install nginx
ENTRYPOINT /entrypoint.sh
/data/dockerfile/entrypoint.sh
#!/bin/bash
/sbin/nginx -g "daemon off;"
nohup /sbin/nginx &
chmod +x /data/dockerfile/entrypoint.sh
# 构建镜像
[root@fengling dockerfile]# docker build . -t nginx-v1:v1
[root@fengling dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-v1 v1 b7e7d04fe461 9 seconds ago 397MB
<none> <none> 66634a33d760 About an hour ago 204MB
http v1 2f6382c01dc9 About an hour ago 315MB
81.70.195.225/one/centos-bind v1 d9af25a35c4b 2 hours ago 286MB
<none> <none> d72cd485fd35 2 hours ago 204MB
<none> <none> e55d7296ee63 2 hours ago 204MB
81.70.195.225/one/ngninx v3 bc47c3b53067 3 hours ago 133MB
81.70.195.225/one/nginx v2 f541f62a8565 3 hours ago 133MB
centos centos7 8652b9f0cb4c 4 hours ago 204MB
81.70.195.225/one/centos v1 8652b9f0cb4c 4 hours ago 204MB
81.70.195.225/one/nginx v1 c39a868aad02 8 days ago 133MB
nginx latest c39a868aad02 8 days ago 133MB
alpine latest d6e46aa2470d 3 weeks ago 5.57MB
one1/alpine v3.10.1 d6e46aa2470d 3 weeks ago 5.57MB
[root@fengling dockerfile]# docker run -it -p8080:80 --name nginx-v1 nginx-v1:v1