美文网首页K8sdocker容器技术
十四、Dockerfile介绍以及镜像的构建与分享

十四、Dockerfile介绍以及镜像的构建与分享

作者: InsaneLoafer | 来源:发表于2021-12-06 13:11 被阅读0次

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的文件内容:

print("hello docker")

第一步,准备Python环境

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

第一步,运行hello.py

$ python3 hello.py
hello docker

一个Dockerfile的基本结构

Dockerfile

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

镜像的构建

docker image build -t hello.py:1.0 .

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

镜像的分享

  1. 首先需要dockerhub上注册账号
  2. 根据自己的账号id修改镜像的tag
    docker image tag <old_tag> <new_tag>
[root@localhost ~]# docker image ls
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
hello             1.0       e3a733c6921a   3 minutes ago   207MB
nginx             latest    ea335eea17ab   6 days ago      141MB
busybox           latest    7138284460ff   12 days ago     1.24MB
ubuntu            21.04     de6f83bfe0b6   7 weeks ago     80MB
jenkins/jenkins   lts       619aabbe0502   3 months ago    441MB
prom/prometheus   v2.20.0   0da625e71069   16 months ago   145MB
[root@localhost ~]#
[root@localhost ~]# docker image tag hello:1.0 insaneloafer/hello:1.0
[root@localhost ~]#
[root@localhost ~]# docker image ls
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
hello                1.0       e3a733c6921a   10 minutes ago   207MB
insaneloafer/hello   1.0       e3a733c6921a   10 minutes ago   207MB
nginx                latest    ea335eea17ab   6 days ago       141MB
busybox              latest    7138284460ff   12 days ago      1.24MB
ubuntu               21.04     de6f83bfe0b6   7 weeks ago      80MB
jenkins/jenkins      lts       619aabbe0502   3 months ago     441MB
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>
[root@localhost ~]# docker login
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.
Username: insaneloafer
Password:
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 ~]#
[root@localhost ~]# docker image push insaneloafer/hello:1.0
The push refers to repository [docker.io/insaneloafer/hello]
4dc361a50bd7: Pushed
b64918d42f0f: Pushed
14636cce64ea: Mounted from library/ubuntu
1.0: digest: sha256:ad2157dfe0eae8456c98644165c3f1f3cb091d667078c760e16d227652225ce1 size: 948
  • 查看docker hub上的镜像


    image.png

镜像的拉取

docker pull <image:tag>

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

相关文章

网友评论

    本文标题:十四、Dockerfile介绍以及镜像的构建与分享

    本文链接:https://www.haomeiwen.com/subject/jsyntrtx.html