简介

在日常的工作中,常常需要制作自己的项目的镜像,一般通过以下两种方式制作镜像:Docker commit、Dockerfile。

Docker commit命令

image.png

演示

运行中的镜像:

  1. [root@centos ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. fe02a3f5efd5 n:1 "nginx -g 'daemon of…" 14 hours ago Up 14 hours (healthy) 8080/tcp, 0.0.0.0:8080->80/tcp nginx
  4. [root@centos ~]# docker images
  5. REPOSITORY TAG IMAGE ID CREATED SIZE
  6. n 1 98d416ae609e 14 hours ago 152MB
  7. <none> <none> 1d7f959d2796 15 hours ago 152MB
  8. <none> <none> c5e27ed530d1 15 hours ago 127MB

docker commit:

  1. [root@centos ~]# docker commit nginx n:2
  2. sha256:db65fb75dfb5c6fdae6567606ecf0ac9caa8889d356288d99789cab7de71bb7d
  3. [root@centos ~]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. n 2 db65fb75dfb5 8 seconds ago 152MB
  6. n 1 98d416ae609e 15 hours ago 152MB
  7. <none> <none> 1d7f959d2796 15 hours ago 152MB
  8. <none> <none> c5e27ed530d1 15 hours ago 127MB

Docker build命令

演示案例文件:

  1. [root@centos doker-build]# ls
  2. app.py Dockerfile-build requirements.txt build-1.txt
  3. [root@centos doker-build]# pwd
  4. /tmp/doker-build

.dockerignore

忽略文件。有点类似git中的gitignore。也就是在打包的时候不会将ignore中的文件打包到镜像中。

  1. build-1.txt
  2. Dockerfile*

创建好容器,进入容器查看,可以看到在这里定义的文件不会打包到镜像中:
这里看到,没有打包dockerfile

  1. [root@centos doker-build]# docker exec -it app sh
  2. /tmp # ls
  3. __pycache__ app.py requirements.txt
  4. /tmp # ls -a
  5. . .. .dockerignore __pycache__ app.py requirements.txt

docer build -f

dockerfile:

  1. FROM python:3.7-alpine
  2. LABEL maintainer="Zuan"
  3. ARG kdir=/tmp
  4. # 工作路径设定为
  5. WORKDIR $kdir
  6. # 创建环境变量给 Flask使用
  7. ENV FLASK_APP app.py
  8. ENV FLASK_RUN_HOST 0.0.0.0
  9. # 复制app.py到容器内WORKDIR目录,这里的点表示当前目录所有文件的拷贝
  10. COPY . .
  11. # 安装 python的依赖
  12. RUN pip install flask
  13. # 映射端口
  14. EXPOSE 5000
  15. STOPSIGNAL SIGTERM
  16. # 为容器设置默认启动命令
  17. CMD ["flask", "run"]

指定文件dockerfile构建。

  1. [root@centos doker-build]# docker build -t app:v1 -f Dockerfile-build .
  2. Sending build context to Docker daemon 5.12kB
  3. Step 1/9 : FROM python:3.7-alpine
  4. ---> 6b73b71fd64e
  5. Step 2/9 : LABEL maintainer="Zuan"
  6. ---> Using cache
  7. ---> 912eef5e6a8e
  8. Step 3/9 : ARG kdir=/tmp
  9. ---> Running in 8c4046226f67
  10. Removing intermediate container 8c4046226f67
  11. ---> 0c98f51b9a6a
  12. Step 4/9 : WORKDIR $kdir
  13. ---> Running in 6fc9bfe925d2
  14. Removing intermediate container 6fc9bfe925d2
  15. ---> 0909fcb6c397
  16. Step 5/9 : ENV FLASK_APP app.py
  17. ---> Running in b7d5e54ca17d
  18. Removing intermediate container b7d5e54ca17d
  19. ---> 016ccfc413ad
  20. Step 6/9 : ENV FLASK_RUN_HOST 0.0.0.0
  21. ---> Running in f1fa9d5ced33
  22. Removing intermediate container f1fa9d5ced33
  23. ---> 8659a2ecba55
  24. Step 7/9 : COPY . .
  25. ---> 8d03a860a33c
  26. Step 8/9 : RUN pip install flask
  27. ---> Running in efe30465a54c
  28. Collecting flask
  29. Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
  30. Collecting Jinja2>=2.10.1
  31. Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
  32. Collecting Werkzeug>=0.15
  33. Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
  34. Collecting itsdangerous>=0.24
  35. Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
  36. Collecting click>=5.1
  37. Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
  38. Collecting MarkupSafe>=0.23
  39. Downloading MarkupSafe-1.1.1.tar.gz (19 kB)
  40. Building wheels for collected packages: MarkupSafe
  41. Building wheel for MarkupSafe (setup.py): started
  42. Building wheel for MarkupSafe (setup.py): finished with status 'done'
  43. Created wheel for MarkupSafe: filename=MarkupSafe-1.1.1-py3-none-any.whl size=12627 sha256=26f4b4c645af5f85252aa9585be3320b7fa5b1faa869f8515f455e5516efae93
  44. Stored in directory: /root/.cache/pip/wheels/b9/d9/ae/63bf9056b0a22b13ade9f6b9e08187c1bb71c47ef21a8c9924
  45. Successfully built MarkupSafe
  46. Installing collected packages: MarkupSafe, Jinja2, Werkzeug, itsdangerous, click, flask
  47. Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0
  48. Removing intermediate container efe30465a54c
  49. ---> a7c43e03f3d3
  50. Step 9/9 : EXPOSE 5000
  51. ---> Running in d597081597fd
  52. Removing intermediate container d597081597fd
  53. ---> 72ec01282e40
  54. Successfully built 72ec01282e40
  55. Successfully tagged app:v1

docker build -t

添加标签。

docker build —no-cache

不使用缓存。
默认情况下是使用缓存的。可以通过第二次构建镜像,看到第二次构建相同的镜像的时候,不用下载东西,速度很快。
使用—no-cache防止有些东西更改,但缓存中没有变,导致打包出来的镜像还是没有后面的变动等小问题。

  1. [root@centos doker-build]# docker build -t app:v1 -f Dockerfile-build . --no-cache

docker build —build-arg

构建时变量。
比如这里我指定工作目录:

  1. [root@centos doker-build]# docker build -t app:v1 -f Dockerfile-build --build-arg kdir=/root .
  2. Sending build context to Docker daemon 5.12kB
  3. Step 1/11 : FROM python:3.7-alpine
  4. ---> 6b73b71fd64e
  5. Step 2/11 : LABEL maintainer="Zuan"
  6. ---> Using cache
  7. ---> 92b61efd3741
  8. Step 3/11 : ARG kdir=/tmp
  9. ---> Using cache
  10. ---> fe0a04ba6121
  11. Step 4/11 : WORKDIR $kdir
  12. ---> Running in 3b4a175ddbe1
  13. Removing intermediate container 3b4a175ddbe1
  14. ---> 04b2e1cdf069
  15. Step 5/11 : ENV FLASK_APP app.py

打包完,启动,进入容器看到工作目录变成root了:

  1. [root@centos doker-build]# docker run -d --name app -p 5000:5000 app:v1
  2. d02181fbdbd765618db61f4dcd8a4429ee777bf4e44ccfd52c2cad661648f45b
  3. [root@centos doker-build]# docker exec -it app sh
  4. ~ # pwd
  5. /root

ARG指令变量

ARG的作用就像上面一个例子所演示。
在workfile中指定了ARG,但是构建的时候,需要临时改变的时候,可以通过build-arg来更改dockerfile定义的ARG。

演示

演示代码:
Dockiefile:

  1. FROM python:3.7-alpine
  2. LABEL maintainer="Zuan"
  3. ARG kdir=/tmp
  4. # 工作路径设定为
  5. WORKDIR $kdir
  6. # 创建环境变量给 Flask使用
  7. ENV FLASK_APP app.py
  8. ENV FLASK_RUN_HOST 0.0.0.0
  9. # 复制app.py到容器内/code目录,这里的点表示当前目录所有文件的拷贝
  10. COPY . .
  11. # 安装 python的依赖
  12. RUN pip install flask
  13. # 映射端口
  14. EXPOSE 5000
  15. STOPSIGNAL SIGTERM
  16. # 为容器设置默认启动命令
  17. CMD ["flask", "run"]

app.py:

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def hello():
  5. return 'Hello World!!'