简体   繁体   中英

Continuously develop and deploy a Django app with Visual Studio Code and Docker

I am developing a Django app locally with Visual Studio Code. In preparation for deployment I "dockerized" everything and now I am already able to run this container locally.

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.

Withthe official 'Python in a container' tutorial I am able to set breakpoints and so on when my app runs locally with Docker.

So I think the workflow will look like this:

  1. I develop my app locally and debug it within Visual Studio Code.
  2. For further debugging I can do this locally with Docker as described above.
  3. When everything looks good I push this container to Google Cloud Run or whatever.

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.

I've never used Google Cloud Run or smth, but based on experience with remote servers I can advice following approach. You can use github actions and docker hub . 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. 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. 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 . 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:

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. Current standard in web industry is PostgreSQL. There are Mongo, Redis and others, but PostgreSQL is used the most. 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. Again, I don't know about Google Cloud Run but on a typical remote server you would do it this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM