MAC上安装Docker Desktop可以网站下载安装,也可以通过brew安装。这里记录brew安装的命令:
brew cask install docker
安装完成后,创建Dockerfile
FROM nginx RUN echo 'Hello this is mywebsite' > /usr/share/nginx/html/index.html
开启Goolge Cloud Container Registy权限:
gcloud auth configure-docker
创建Dockerfile:
from flask import Flask from datetime import datetime import socket app = Flask(__name__) @app.route("/") def hello(): now = datetime.now() html1 = str(socket.gethostname()) html2 = now.strftime("%m/%d/%Y, %H:%M:%S") html = "<h1>Time: " + html2 + " " + "Hostname is: " + html1 + "</h1> " return html if __name__ == "__main__": app.run(host='0.0.0.0', port=80)
安装列表requirements.txt:
Flask
程序代码app.py:
from flask import Flask import socket app = Flask(__name__) @app.route("/") def hello(): html = socket.gethostname() return html if __name__ == "__main__": app.run(host='0.0.0.0', port=80)
Build Docker Image并上传:
docker build ./ -t gcr.io/central-segment-217003/web-hostname:1.0 docker push gcr.io/central-segment-217003/web-hostname:1.0
用此docker image创建docker:
docker run -d -p 127.0.0.1:80:80 gcr.io/central-segment-217003/web-hostname:1.0 $ curl localhost 423fcf67c506
查看docker情况:
$docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 423fcf67c506 gcr.io/central-segment-217003/web-hostname:1.0 "python app.py" 41 seconds ago Up 39 seconds 127.0.0.1:80->80/tcp sweet_napier
和显示的hostname相同。