使用docker init生成DockerFile

很多框架都带有脚手架工具,帮助初始化项目,可以快速开始开发。Docker 在最近的更新上也引入了 docker init 命令,帮忙生成 Dockerfile.dockerignoredocker-compose.yaml,帮助项目快速接入 Docker

安装docker desktop

sudo apt install gnome-terminal -y
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
wget -c -O /opt/docker-desktop.deb 'https://desktop.docker.com/linux/main/amd64/139021/docker-desktop-4.28.0-amd64.deb?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-linux-amd64&_gl=1*d0pd7l*_ga*MTgyMzM2NDY4MC4xNzEwMTI1Mjc1*_ga_XJWPQMJYHQ*MTcxMDgzMzgzOC4zLjEuMTcxMDgzNTczOC41Mi4wLjA.'
sudo apt-get update
sudo apt-get install /opt/docker-desktop.deb

python项目

mkdir app_py_demo;cd app_py_demo
touch app.py requirements.txt
vi app.py
# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_docker():
    return '<h1> hello world </h1>'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')
vi requirements.txt
Flask
gunicorn
docker init
docker compose build
docker compose up -d
curl localhost:8000
docker compose down