[英]Continuously develop and deploy a Django app with Visual Studio Code and Docker
I am developing a Django app locally with Visual Studio Code.我正在使用 Visual Studio Code 在本地开发一个 Django 应用程序。 In preparation for deployment I "dockerized" everything and now I am already able to run this container locally.在准备部署时,我将所有内容“dockerized”,现在我已经能够在本地运行这个容器了。
Before I try to build my Docker image somewhere else (I have Google Cloud Run in mind), I want to make sure that I still can debug my code.在我尝试在其他地方构建我的 Docker 图像之前(我想到了 Google Cloud Run),我想确保我仍然可以调试我的代码。
Withthe official 'Python in a container' tutorial I am able to set breakpoints and so on when my app runs locally with Docker.通过官方的“容器中的 Python”教程,当我的应用程序在 Docker 本地运行时,我可以设置断点等。
So I think the workflow will look like this:所以我认为工作流程将如下所示:
Does that sound like a reasonable plan or did I miss something important?这听起来像是一个合理的计划还是我错过了一些重要的事情? In the end, I am looking for an easy convenient way to continuously develop (and debug) a Django app with Visual Studio Code and deploy it with Docker.最后,我正在寻找一种简单方便的方法来使用 Visual Studio Code 持续开发(和调试)Django 应用程序并使用 Docker 部署它。
I've never used Google Cloud Run or smth, but based on experience with remote servers I can advice following approach.我从未使用过 Google Cloud Run 或 smth,但根据远程服务器的经验,我可以建议采用以下方法。 You can use github actions and docker hub .您可以使用github 操作和docker 集线器。 Cover your application or at least critical parts of it with tests which will ensure that everything important works properly.用测试覆盖您的应用程序或至少它的关键部分,以确保一切重要的工作正常。 You can set github actions up the way that your tests will be run everytime you push to your github repo.您可以将 github 操作设置为每次推送到 github 存储库时运行测试的方式。 If tests will be passed an image of your application (usually it's name is your_app:latest) will be updated on dockerhub allowing you to build from an image.如果测试将通过,您的应用程序的图像(通常它的名称是 your_app:latest)将在 dockerhub 上更新,允许您从图像构建。 It's a good practice to have multiple images.拥有多个图像是一种很好的做法。 For example you could have a stable version, say v.1.0
and a beta version your_app:latest
.例如,您可以有一个稳定版本,比如v.1.0
和一个 beta 版本your_app:latest
。 Thus you will be able to run your stable version on a production server, while beta version can be run on a development server.因此,您将能够在生产服务器上运行稳定版本,而测试版可以在开发服务器上运行。 Do not update stable versions, release new ones and keep existing ones.不要更新稳定版本,发布新版本并保留现有版本。
An example of how github actions file can look like: github 操作文件的示例:
name: your_app_workflow
on: [push]
jobs:
tests:
# run your tests here
push_to_docker_hub:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
needs: tests
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: your_repository_on_dockerhub
tag_with_ref: true
Maybe you know following, but I will mention it anyway.也许你知道以下,但我还是会提到它。 Django built in database is SQLite which is not reliable at all, thus if you are going to let others use your product, you MUST think of another database. Django 内置数据库是 SQLite 是不可靠的,所以如果你要让别人使用你的产品,你必须考虑另一个数据库。 Current standard in web industry is PostgreSQL. There are Mongo, Redis and others, but PostgreSQL is used the most. web行业目前的标准是PostgreSQL,还有Mongo、Redis等,但PostgreSQL用的最多。 Also, Django doesn't serve static and media files in production, so you will have to use proxy server such as Nginx. Nginx can not work with your Django app directly thus you will need an intermediary such as Gunicorn.此外,Django 不提供 static 和生产中的媒体文件,因此您必须使用代理服务器,例如 Nginx。Nginx 不能直接与您的 Django 应用程序一起使用,因此您需要一个中介,例如 Gunicorn。 Again, I don't know about Google Cloud Run but on a typical remote server you would do it this way.同样,我不知道 Google Cloud Run,但在典型的远程服务器上,您会这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.