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.pyhello docker
一个Dockerfile的基本结构
Dockerfile
FROM ubuntu:21.04RUN apt-get update && \DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y python3.9 python3-pip python3.9-devADD 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.242GBStep 1/4 : FROM ubuntu:21.0421.04: Pulling from library/ubuntu80d63867ecd7: Pull completeDigest: sha256:26cd4ff32a9c031eaca3d6f589a7799f28b34a539e1bd81acbf1a6efeec4b1ceStatus: Downloaded newer image for ubuntu:21.04---> de6f83bfe0b6Step 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...
镜像的分享
- 首先需要dockerhub上注册账号
- 根据自己的账号id修改镜像的tag
docker image tag <old_tag> <new_tag>
[root@localhost ~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEhello 1.0 e3a733c6921a 3 minutes ago 207MBnginx latest ea335eea17ab 6 days ago 141MBbusybox latest 7138284460ff 12 days ago 1.24MBubuntu 21.04 de6f83bfe0b6 7 weeks ago 80MBjenkins/jenkins lts 619aabbe0502 3 months ago 441MBprom/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 lsREPOSITORY TAG IMAGE ID CREATED SIZEhello 1.0 e3a733c6921a 10 minutes ago 207MBinsaneloafer/hello 1.0 e3a733c6921a 10 minutes ago 207MBnginx latest ea335eea17ab 6 days ago 141MBbusybox latest 7138284460ff 12 days ago 1.24MBubuntu 21.04 de6f83bfe0b6 7 weeks ago 80MBjenkins/jenkins lts 619aabbe0502 3 months ago 441MBprom/prometheus v2.20.0 0da625e71069 16 months ago 145MB
- 将image push到docker hub
- 登录docker hub:
docker login然后输入账号密码 - 进行image的push:
docker image push <image:tag>
[root@localhost ~]# docker loginLogin 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: insaneloaferPassword:WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded[root@localhost ~]#[root@localhost ~]# docker image push insaneloafer/hello:1.0The push refers to repository [docker.io/insaneloafer/hello]4dc361a50bd7: Pushedb64918d42f0f: Pushed14636cce64ea: Mounted from library/ubuntu1.0: digest: sha256:ad2157dfe0eae8456c98644165c3f1f3cb091d667078c760e16d227652225ce1 size: 948
- 查看docker hub上的镜像

镜像的拉取
docker pull <image:tag>
[root@localhost ~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest ea335eea17ab 6 days ago 141MBbusybox latest 7138284460ff 12 days ago 1.24MBjenkins/jenkins lts 619aabbe0502 3 months ago 441MBprom/prometheus v2.20.0 0da625e71069 16 months ago 145MB[root@localhost ~]#[root@localhost ~]#[root@localhost ~]# docker pull insaneloafer/hello:1.01.0: Pulling from insaneloafer/hello80d63867ecd7: Pull completeafd09be562fb: Pull complete1a5879efd499: Pull completeDigest: sha256:ad2157dfe0eae8456c98644165c3f1f3cb091d667078c760e16d227652225ce1Status: Downloaded newer image for insaneloafer/hello:1.0docker.io/insaneloafer/hello:1.0[root@localhost ~]#[root@localhost ~]# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEinsaneloafer/hello 1.0 e3a733c6921a 22 minutes ago 207MBnginx latest ea335eea17ab 6 days ago 141MBbusybox latest 7138284460ff 12 days ago 1.24MBjenkins/jenkins lts 619aabbe0502 3 months ago 441MBprom/prometheus v2.20.0 0da625e71069 16 months ago 145MB
