Dockerfile 介绍

Docker can build images automatically by reading the instructions from a 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 users can create an automated build that executes several command-line instructions in succession.

https://docs.docker.com/engine/reference/builder/

  • Dockerfile是用于构建docker镜像的文件
  • Dockerfile里包含了构建镜像所需的“指令”
  • Dockerfile有其特定的语法规则

举例:执行一个Python程序

容器及进程,所以镜像就是一个运行这个进程所需要的环境。

假如我们要在一台ubuntu 21.04上运行下面这个hello.py的Python程序

hello.py的文件内容:

  1. print("hello docker")

第一步,准备Python环境

  1. apt-get update && \
  2. DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev

第一步,运行hello.py

  1. $ python3 hello.py
  2. hello docker

一个Dockerfile的基本结构

Dockerfile

  1. FROM ubuntu:21.04
  2. RUN apt-get update && \
  3. DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
  4. ADD hello.py /
  5. CMD ["python3", "/hello.py"]

镜像的构建

docker image build -t hello.py:1.0 .

  • -t:tag指定镜像名称和版本
  1. [root@localhost ~]# docker image build -t hello:1.0 .
  2. Sending build context to Docker daemon 3.242GB
  3. Step 1/4 : FROM ubuntu:21.04
  4. 21.04: Pulling from library/ubuntu
  5. 80d63867ecd7: Pull complete
  6. Digest: sha256:26cd4ff32a9c031eaca3d6f589a7799f28b34a539e1bd81acbf1a6efeec4b1ce
  7. Status: Downloaded newer image for ubuntu:21.04
  8. ---> de6f83bfe0b6
  9. Step 2/4 : RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-dev
  10. ---> Running in 5e8b3201ac57
  11. ...

镜像的分享

  1. 首先需要dockerhub上注册账号
  2. 根据自己的账号id修改镜像的tag
    docker image tag <old_tag> <new_tag>
  1. [root@localhost ~]# docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello 1.0 e3a733c6921a 3 minutes ago 207MB
  4. nginx latest ea335eea17ab 6 days ago 141MB
  5. busybox latest 7138284460ff 12 days ago 1.24MB
  6. ubuntu 21.04 de6f83bfe0b6 7 weeks ago 80MB
  7. jenkins/jenkins lts 619aabbe0502 3 months ago 441MB
  8. prom/prometheus v2.20.0 0da625e71069 16 months ago 145MB
  9. [root@localhost ~]#
  10. [root@localhost ~]# docker image tag hello:1.0 insaneloafer/hello:1.0
  11. [root@localhost ~]#
  12. [root@localhost ~]# docker image ls
  13. REPOSITORY TAG IMAGE ID CREATED SIZE
  14. hello 1.0 e3a733c6921a 10 minutes ago 207MB
  15. insaneloafer/hello 1.0 e3a733c6921a 10 minutes ago 207MB
  16. nginx latest ea335eea17ab 6 days ago 141MB
  17. busybox latest 7138284460ff 12 days ago 1.24MB
  18. ubuntu 21.04 de6f83bfe0b6 7 weeks ago 80MB
  19. jenkins/jenkins lts 619aabbe0502 3 months ago 441MB
  20. prom/prometheus v2.20.0 0da625e71069 16 months ago 145MB
  1. 将image push到docker hub
  • 登录docker hub:docker login 然后输入账号密码
  • 进行image的push:docker image push <image:tag>
  1. [root@localhost ~]# docker login
  2. Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
  3. Username: insaneloafer
  4. Password:
  5. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  6. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded
  9. [root@localhost ~]#
  10. [root@localhost ~]# docker image push insaneloafer/hello:1.0
  11. The push refers to repository [docker.io/insaneloafer/hello]
  12. 4dc361a50bd7: Pushed
  13. b64918d42f0f: Pushed
  14. 14636cce64ea: Mounted from library/ubuntu
  15. 1.0: digest: sha256:ad2157dfe0eae8456c98644165c3f1f3cb091d667078c760e16d227652225ce1 size: 948
  • 查看docker hub上的镜像
    四、Dockerfile介绍以及镜像的构建与分享 - 图1

镜像的拉取

docker pull <image:tag>

  1. [root@localhost ~]# docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest ea335eea17ab 6 days ago 141MB
  4. busybox latest 7138284460ff 12 days ago 1.24MB
  5. jenkins/jenkins lts 619aabbe0502 3 months ago 441MB
  6. prom/prometheus v2.20.0 0da625e71069 16 months ago 145MB
  7. [root@localhost ~]#
  8. [root@localhost ~]#
  9. [root@localhost ~]# docker pull insaneloafer/hello:1.0
  10. 1.0: Pulling from insaneloafer/hello
  11. 80d63867ecd7: Pull complete
  12. afd09be562fb: Pull complete
  13. 1a5879efd499: Pull complete
  14. Digest: sha256:ad2157dfe0eae8456c98644165c3f1f3cb091d667078c760e16d227652225ce1
  15. Status: Downloaded newer image for insaneloafer/hello:1.0
  16. docker.io/insaneloafer/hello:1.0
  17. [root@localhost ~]#
  18. [root@localhost ~]# docker image ls
  19. REPOSITORY TAG IMAGE ID CREATED SIZE
  20. insaneloafer/hello 1.0 e3a733c6921a 22 minutes ago 207MB
  21. nginx latest ea335eea17ab 6 days ago 141MB
  22. busybox latest 7138284460ff 12 days ago 1.24MB
  23. jenkins/jenkins lts 619aabbe0502 3 months ago 441MB
  24. prom/prometheus v2.20.0 0da625e71069 16 months ago 145MB