Dockerfile制作容器镜像&上传到阿里云私人镜像容器仓库
作者:myluzh 分类: Docker 长度:2482 阅读:509
0x01 前言
阿里云容器镜像仓库个人版本是不收费的,只不过限制3个命名空间与300个仓库。
地址为:https://cr.console.aliyun.com/cn-hangzhou/instances
创建一个命名空间,然后创建一个名为hellohttp的仓库
0x02 创建一个python http应用
父文件夹名称hellohttp,文件名为main.py。
运行起来后访问http8080端口会显示一些测试信息。
import http.server
import socketserver
import socket
class MyHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# 输出 "Hello, World!"
self.wfile.write(b"Hello, World!<br>")
# 输出本机hostname和ip地址
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
self.wfile.write(f"Hostname: {hostname}<br>".encode())
self.wfile.write(f"IP Address: {ip_address}".encode())
PORT = 8080
# 启动服务器
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
print(f"Serving at http://localhost:{PORT}")
httpd.serve_forever()
0x03 dockerfile文件 打包当前应用
FROM python:3.9-slim-buster
RUN echo "deb http://mirrors.aliyun.com/debian/ buster main" > /etc/apt/sources.list
RUN apt-get update && apt-get install -y lsof net-tools curl
WORKDIR /app
ADD . /app
EXPOSE 8080
CMD ["python", "main.py"]
# 构建镜像
docker build -t hellok8s:latest .
0x04 上传镜像到镜像私有仓库
1.查看打包后要上传的镜像ID
IMAGE ID 79d9f614e468
-
登录阿里云Docker Registry
docker login --username=myluzh registry.cn-hangzhou.aliyuncs.com -
将镜像推送到Registry(这里的hellohttp就是仓库名)
docker login --username=myluzh registry.cn-hangzhou.aliyuncs.com
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:[镜像版本号] -
从Registry中拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:[镜像版本号]
0x05 下拉私有镜像并运行
1.下拉私有仓库镜像
docker pull registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:v1
2.运行容器,映射端口
docker run -d -p 8080:8080 --name hellohttp registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:v1
3.查看
curl 127.0.0.1:8080
Hello, World!
Hostname: b6a43ca3795d
IP Address: 172.17.0.2%