dockerfile构建自己的python镜像并上传到镜像仓库

dockerfile构建自己的python镜像并上传到镜像仓库,第1张

dockerfile构建自己的python镜像并上传到镜像仓库 0 知识学习
  1. 从大佬up老王那得知,dockfile制作python镜像无需像java程序一样要先打包成jar包或war包,直接基于含python环境的基础镜像制作即可

  2. dockerfile指令:RUN/CMD/ENTRYPOINT的区别

     RUN主要用于在容器中安装软件, *** 作文件和文件夹等等;
     CMD/ENTRYPOINT主要用于启动容器。
    
     CMD命令有两个特点:
     1. 多条CMD命令只会执行最后一条;
     2. 当运行容器时,指令的最后指定相关参数,则原来应执行的CMD命令将会被覆盖掉而不执行。
     ENTRYPOINT命令:
     3. 多条指令也只会执行最后一条;
     4. 运行容器时指令的最后指定相关参数,则ENTRYPOINT命令不会被覆盖掉仍然会执行。
     
     因此,这两条命令在使用范围上也有所不同:
     5. 由于CMD命令可以被`docker run`命令指定参数进行覆盖执行,因此可以用来设置默认启动命令。
     6. ENTRYPOINT命令一定会执行,因此常常用来启动应用程序或服务,最好不要使用CMD命令。
    
1 最简单的helloworld镜像构建步骤
root@master:/home/hqc# cd docker_learning/
# 创建一个专门用来学习测试docker相关的文件夹

root@master:/home/hqc/docker_learning# mkdir my-hello-world
root@master:/home/hqc/docker_learning# ls
	my-hello-world
# 创建测试这个项目的文件夹

root@master:/home/hqc/docker_learning# cd my-hello-world/
root@master:/home/hqc/docker_learning/my-hello-world# touch 	helloworld.py
root@master:/home/hqc/docker_learning/my-hello-world# ls
	helloworld.py
root@master:/home/hqc/docker_learning/my-hello-world# vim helloworld.py 
	#!/usr/bin/python
	# -*-coding: utf-8 -*-
	import os
	print("this is my hello-world")
# 进入项目文件夹并创建一个helloworld的python程序

root@master:/home/hqc/docker_learning/my-hello-world# vim Dockerfile
	FROM python # 基于python这个基础镜像构建
	MAINTAINER [email protected] # 创作者联系方式
	RUN mkdir /hqc-hello-world # 在这个容器中创建一个项目文件夹
	ADD helloworld.py /hqc-hello-world/ # 将本机中的py文件拷贝到容器中的项目文件夹中
	ENTRYPOINT ["python","/hqc-hello-world/helloworld.py"] # 可能是基于python环境运行这个文件夹中的py程序,具体还不是很明白
# 编写dockerfile文件

root@master:/home/hqc/docker_learning/my-hello-world# docker build -t hqc-helloworld:v1.1 .
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM python
latest: Pulling from library/python
647acf3d48c2: Pull complete 
b02967ef0034: Pull complete 
e1ad2231829e: Pull complete 
5576ce26bf1d: Pull complete 
a66b7f31b095: Pull complete 
05189b5b2762: Pull complete 
af08e8fda0d6: Pull complete 
287d56f7527b: Pull complete 
dc0580965fb6: Pull complete 
Digest: sha256:f44726de10d15558e465238b02966a8f83971fd85a4c4b95c263704e6a6012e9
Status: Downloaded newer image for python:latest # step1由于本地不存在python基础镜像,因此需要现场拉取
 ---> f48ea80eae5a
Step 2/5 : MAINTAINER [email protected]
 ---> Running in c2325b39f55b
Removing intermediate container c2325b39f55b
 ---> d2e44f980492
Step 3/5 : RUN mkdir /hqc-hello-world
 ---> Running in 4a3b033a423e
Removing intermediate container 4a3b033a423e
 ---> e60f9bdb29de
Step 4/5 : ADD helloworld.py /hqc-hello-world/
 ---> 48ffee2e027c
Step 5/5 : ENTRYPOINT ["python","/hqc-hello-world/helloworld.py"]
 ---> Running in e5437e4c6ec2
Removing intermediate container e5437e4c6ec2
 ---> fc123e34b960
Successfully built fc123e34b960
Successfully tagged hqc-helloworld:v1.1
# dockerfile构建该镜像

root@master:/home/hqc/docker_learning/my-hello-world# docker run hqc-helloworld:v1.1
	this is my hello-world
# 运行成功!
2 需要其他依赖的python程序镜像制作
root@master:/home/hqc# cd docker_learning/
root@master:/home/hqc/docker_learning# mkdir flask-hello-world
root@master:/home/hqc/docker_learning# cd flask-hello-world/
root@master:/home/hqc/docker_learning/flask-hello-world# touch flask-hello-world.py
root@master:/home/hqc/docker_learning/flask-hello-world# vim flask-hello-world.py 
	from flask import Flask
	app = Flask(__name__)

	@app.route('/')
	def hello_world():
    	return 'hello world'

	if __name__ == '__main__':
   	 app.run(debug=True,host='0.0.0.0')

# 创建专用文件夹并在其中创建python程序

root@master:/home/hqc/docker_learning/flask-hello-world# pip install flask
	Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.4 itsdangerous-1.1.0
# 安装flask

root@master:/home/hqc/docker_learning/flask-hello-world# python flask-hello-world.py 
	 * Serving Flask app "flask-hello-world" (lazy loading)
	 * Environment: production
	   WARNING: This is a development server. Do not use it in a production deployment.
	   Use a production WSGI server instead.
	 * Debug mode: on
	 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
	 * Restarting with stat
	 * Debugger is active!
	 * Debugger PIN: 182-111-029
	127.0.0.1 - - [29/Nov/2021 19:54:52] "GET / HTTP/1.1" 200 -
	127.0.0.1 - - [29/Nov/2021 19:54:52] "GET /favicon.ico HTTP/1.1" 404 -
# 运行python程序,成功!访问网址为:http://127.0.0.1:5000/

root@master:/home/hqc/docker_learning/flask-hello-world# vim Dockerfile
	FROM python
	# LABEL "[email protected]"
	RUN pip install flask # 事先已经安装过了,所以很快
	COPY flask-hello-world.py /flask-hello-world/ # 将本机中的py文件拷贝到容器的/flask-hello-world/下
	WORKDIR /flask-hello-world # 切换工作目录为容器中的/flask-hello-world下
	# 暴露端口,否则只有本地可访问
	EXPOSE 5000
	CMD ["python", "flask-hello-world.py"] # 执行程序

root@master:/home/hqc/docker_learning/flask-hello-world# docker build -t flask-hello-world:v1.0 .
# 构建镜像1.0版本

root@master:/home/hqc/docker_learning/flask-hello-world# docker images
	REPOSITORY                                             TAG       IMAGE ID       CREATED         SIZE
	flask-hello-world                                      v1.0      6c3d77891c6a   2 minutes ago   928MB
# 查看镜像,存在

root@master:/home/hqc/docker_learning/flask-hello-world# docker run flask-hello-world:v1.0
	 * Serving Flask app 'flask-hello-world' (lazy loading)
	 * Environment: production
	   WARNING: This is a development server. Do not use it in a production deployment.
	   Use a production WSGI server instead.
	 * Debug mode: on
	 * Running on all addresses.
	   WARNING: This is a development server. Do not use it in a production deployment.
	 * Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
	 * Restarting with stat
	 * Debugger is active!
	 * Debugger PIN: 127-875-497
	172.17.0.1 - - [29/Nov/2021 12:12:18] "GET / HTTP/1.1" 200 -
	172.17.0.1 - - [29/Nov/2021 12:12:18] "GET /favicon.ico HTTP/1.1" 404 -
# 运行该docker镜像。由输出信息可知,访问地址为http://172.17.0.2:5000/


运行成功!

3 上传docker镜像到dockerhub中

访问dockerhub

第一步:登录dockerhub
root@master:/home/hqc/docker_learning/flask-hello-world# docker login -u error12
	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
# 登录成功!其中error12为我的dockerhub账户ID

第二步:给镜像新打一个标签

不然将会push失败

root@master:/home/hqc/docker_learning/flask-hello-world# docker push flask-hello-world:v1.0
	The push refers to repository [docker.io/library/flask-hello-world]
	d22d05d670e3: Preparing 
	8e0da839bae9: Preparing 
	42ce61e841fa: Preparing 
	a9b125f164c3: Preparing 
	e24045f8c247: Preparing 
	b7b662b31e70: Waiting 
	6f5234c0aacd: Waiting 
	8a5844586fdb: Waiting 
	a4aba4e59b40: Waiting 
	5499f2905579: Waiting 
	a36ba9e322f7: Waiting 
	denied: requested access to the resource is denied
# push失败

root@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a dockerhub/flask-hello-world:v1.0
root@master:/home/hqc/docker_learning/flask-hello-world# docker images
REPOSITORY                                             TAG       IMAGE ID       CREATED          SIZE
flask-hello-world                                      v1.0      6c3d77891c6a   38 minutes ago   928MB
dockerhub/flask-hello-world                            v1.0      6c3d77891c6a   38 minutes ago   928MB
# 新打一个标签,并查看镜像

root@master:/home/hqc/docker_learning/flask-hello-world# docker push dockerhub/flask-hello-world:v1.0
	denied: requested access to the resource is denied
# 仍然push失败,原因是:新打的标签名不对(dockerhub/flask-hello-world:v1.0),/前面应该为对应的账户ID。

第三步:push镜像到dockerhub公有仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a error12/flask-hello-world:v1.0 
root@master:/home/hqc/docker_learning/flask-hello-world# docker push error12/flask-hello-world:v1.0
The push refers to repository [docker.io/error12/flask-hello-world]
d22d05d670e3: Pushed 
8e0da839bae9: Pushed 
42ce61e841fa: Mounted from error12/hqc-helloworld 
a9b125f164c3: Mounted from error12/hqc-helloworld 
e24045f8c247: Mounted from error12/hqc-helloworld 
b7b662b31e70: Mounted from error12/hqc-helloworld 
6f5234c0aacd: Mounted from error12/hqc-helloworld 
8a5844586fdb: Mounted from error12/hqc-helloworld 
a4aba4e59b40: Mounted from error12/hqc-helloworld 
5499f2905579: Mounted from error12/hqc-helloworld 
a36ba9e322f7: Mounted from error12/hqc-helloworld 
v1.0: digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a size: 2636
# 更改之后,成功!!!

dockerhub界面中也可以查看,并且提供下载。

4 上传镜像到阿里云镜像私有仓库 01 注册阿里云账户 02 创建个人实例


里边可新建命名空间(注意:个人实例只提供三个命名空间,需谨慎创建)和镜像仓库。

03 创建命名空间

04 创建镜像仓库

代码源应该选择本地仓库

  1. 可在阿里云平台界面中直接创建,可选择公有或私有仓库;
  2. 也可通过命令行创建
# 首先得先退出之前登录的dockerhub仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker logout
	Removing login credentials for https://index.docker.io/v1/

# 登录阿里云仓库
root@master:/home/hqc/docker_learning/flask-hello-world# docker login --username=errorhqc兮 registry.cn-beijing.aliyuncs.com
	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@master:/home/hqc/docker_learning/flask-hello-world# docker tag 6c3d77891c6a registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
# 和上传到dockerhub类似,也需要打一个新标签

root@master:/home/hqc/docker_learning/flask-hello-world# docker images
	REPOSITORY                                                       TAG       IMAGE ID       CREATED          SIZE
	registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world   v1.0      6c3d77891c6a   59 minutes ago   928MB
	dockerhub/flask-hello-world                                      v1.0      6c3d77891c6a   59 minutes ago   928MB
	error12/flask-hello-world                                        v1.0      6c3d77891c6a   59 minutes ago   928MB
	flask-hello-world                                                v1.0      6c3d77891c6a   59 minutes ago   928MB
# 查看镜像

root@master:/home/hqc/docker_learning/flask-hello-world# docker push registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
	The push refers to repository [registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world]
	d22d05d670e3: Pushed 
	8e0da839bae9: Pushed 
	42ce61e841fa: Mounted from hqc-k8s/docker_images 
	a9b125f164c3: Mounted from hqc-k8s/docker_images 
	e24045f8c247: Mounted from hqc-k8s/docker_images 
	b7b662b31e70: Mounted from hqc-k8s/docker_images 
	6f5234c0aacd: Mounted from hqc-k8s/docker_images 
	8a5844586fdb: Mounted from hqc-k8s/docker_images 
	a4aba4e59b40: Mounted from hqc-k8s/docker_images 
	5499f2905579: Mounted from hqc-k8s/docker_images 
	a36ba9e322f7: Mounted from hqc-k8s/docker_images 
	v1.0: digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a size: 2636
# push成功!并且发现速度非常快,至少比dockerhub快

可见,push成功,自动创建了一个默认为私有的镜像仓库。

具体 *** 作官方有详细介绍:

5 在其他节点上拉取阿里云私有仓库的镜像
# 登录我的阿里云仓库
root@node01:/home/user# docker login --username=errorhqc兮 registry.cn-beijing.aliyuncs.com
	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@node01:/home/user# docker pull registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
	v1.0: Pulling from hqc-k8s/ali-flask-hello-world
	647acf3d48c2: Pull complete 
	b02967ef0034: Pull complete 
	e1ad2231829e: Pull complete 
	5576ce26bf1d: Pull complete 
	a66b7f31b095: Pull complete 
	05189b5b2762: Pull complete 
	af08e8fda0d6: Pull complete 
	287d56f7527b: Pull complete 
	dc0580965fb6: Pull complete 
	c1147962ae93: Pull complete 
	9310a3ae1b50: Pull complete 
	Digest: sha256:af862fb9415a8121710e7d87b0ddb085df8a70cc744d6ebe4a8cd64f6d242b2a
	Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
	registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world:v1.0
# 拉取成功

root@node01:/home/user# docker images
	REPOSITORY                                                       TAG       IMAGE ID       CREATED         SIZE
	registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-flask-hello-world   v1.0      6c3d77891c6a   14 hours ago    928MB
# 查看镜像

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651287.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存