• 生活的道路一旦选定,就要勇敢地走到底,决不回头。——左拉
  • 坚强的信心,能使平凡的人做出惊人的事业。——马尔顿
  • 人不可有傲气,但不可无傲骨。 --徐悲鸿
  • 古之立大志者,不惟有超世之才,亦必有坚韧不拔之志。 --苏轼
  • 时间像海绵里的水,只要你愿意挤,总还是有的。 --鲁迅

Dockerfile核心模块使用

DevOps zkinogg 2年前 (2020-08-25) 369次浏览 0个评论

定义

Dockerfile is nothing but the source code for building Docker image.
  - Docker can build images automatically by reading the instructions from Dockerfile.
  - A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
  -  Using docker build ,user can create an automated build that executes several command line instructions in succession.
  
  
  
Dockerfile只是构建Docker镜像的源代码。

- Docker可以通过读取Dockerfile中的指令自动生成镜像。

- Dockerfile是一个文本文档,包含用户可以在命令行上调用的所有命令来组装镜像。

- 使用docker build,用户可以创建一个连续执行多个命令行指令的自动构建。

dgoM7Q.png

4组核心的Dockerfile指令(8个)

USER/WORKDIR指令

[root@db01 ~]# vim /data/dockerfile/Dockerfile
FROM docker.io/zkinogg/nginx:v1.12.2    #镜像来源
USER nginx                               # 规定用什么用户起
WORKDIR /usr/share/nginx/html           # 在这个目录下执行WORKDIR可理解为shell中的cd


[root@db01 dockerfile]# docker build . -t docker.io/zkinogg/nginx:v1.12.2_with_user_workdir  #构建本目录下的dockerfile,-t是加一个tag标签with_user_workdir 加全镜像
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM docker.io/zkinogg/nginx:v1.12.2
 ---> 4037a5562b03
Step 2/3 : USER nginx
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in e914132359e3
Removing intermediate container e914132359e3
 ---> 3bd7d552ff14
Step 3/3 : WORKDIR /usr/share/nginx/html
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in fbc08157b933
Removing intermediate container fbc08157b933
 ---> 69e30e9b8c37
Successfully built 69e30e9b8c37
Successfully tagged zkinogg/nginx:v1.12.2_with_user_workdir



[root@db01 dockerfile]# docker run --rm -ti --name nginx123 zkinogg/nginx:v1.12.2_with_user_workdir /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
nginx@db33bc26a53b:/usr/share/nginx/html$ whoami
nginx
nginx@db33bc26a53b:/usr/share/nginx/html$ pwd
/usr/share/nginx/html

ADD/EXPOSE指令

[root@db01 ~]# vim /data/dockerfile/Dockerfile
FROM zkinogg/nginx:v1.12.2                  #镜像来源
ADD index.html /usr/share/nginx/html/index.html  # 把当前目录的index.html放到镜像里的nginx 的html目录下
EXPOSE 80   # 容器起来默认监听80端口

[root@db01 dockerfile]# cp /root/html/index.html .
[root@db01 dockerfile]# ll
total 8
-rw-r--r-- 1 root root   95 Aug 25 17:40 Dockerfile
-rw-r--r-- 1 root root 2381 Aug 25 17:48 index.html
[root@db01 dockerfile]# docker build . -t zkinogg/nginx:v1.12.2_with_index_exposeSending build context to Docker daemon   5.12kB
Step 1/3 : FROM docker.io/zkinogg/nginx:v1.12.2
 ---> 4037a5562b03
Step 2/3 : ADD index.html /usr/share/nginx/html/index.html
 ---> f441fe9bd8c9
Step 3/3 : EXPOSE 80
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in 86d4ea49e0c9
Removing intermediate container 86d4ea49e0c9
 ---> b1ed1b54051b
Successfully built b1ed1b54051b
Successfully tagged zkinogg/nginx:v1.12.2_with_index_expose

# 启动容器发现警告
[root@db01 dockerfile]# docker run --rm -d --name nginx123 -P zkinogg/nginx:v1.12.2_with_index_expose
WARNING: IPv4 forwarding is disabled. Networking will not work.
0c723fc9d9004d3ff6f2ef45d40ff6c6d99a33c957def9b0ba5d8b08be0bfd36

# WARNING: IPv4 forwarding is disabled. Networking will not work.
#警告:IPv4转发已禁用。网络是行不通的。

# 修改网卡配置
[root@db01 dockerfile]# vim /usr/lib/sysctl.d/00-system.conf 
 ##添加如下代码
 net.ipv4.ip_forward=1
# 重启网卡
[root@db01 dockerfile]# systemctl restart network
# 查看启动端口
[root@db01 dockerfile]# netstat -lntp
tcp6       0      0 :::32769                :::*                    LISTEN      15486/docker-proxy  




## 浏览器访问10.0.0.51:32769

dgos91.png