Skip to content

Latest commit

 

History

History
60 lines (41 loc) · 2.18 KB

File metadata and controls

60 lines (41 loc) · 2.18 KB

Containerize a Django application using Visual Studio Code

This lab teaches you how to use Visual Studio Code's Docker extension to build a Docker container for an existing Django web application which takes log messages and stores them in an SQLite database.

The container that you use in this lab will be used in other labs that teach you how to publish a Docker container.

Prerequisites

If you're doing this lab outside of the Microsoft booth at PyCon 2019, you'll need the following tools installed on your local machine:

  1. Docker Desktop
  2. Visual Studio Code
  3. The VS Code Docker Extension

Open workspace and build a development container

  1. Open the lab folder with Visual Studio Code:

    cd 1-vscode-django-docker
    code-insiders .
  2. Run Ctrl-Shift-P and type Add Docker files to Workspace.

  3. Following the prompts select Python and port 8000.

  4. Change the RUN and CMD lines in the Dockerfile to the following:

    # Using pip:
    RUN python3 -m pip install -r requirements.txt
    RUN python3 manage.py migrate
    CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
  5. Right-click on docker-compose.yml and click Compose up.

  6. Open http://localhost:8000 in the browser to view the app.

(Optional) Build a production-ready container

If you want to build a container using a production webserver using nginx and uwsgi, you can follow the steps below.

The application already contains a uwsgi.ini file which defines how to run the web server, so you only need to make some small modifications to the Dockerfile.

  1. Remove the CMD line from the Dockerfile.

  2. Change the FROM line to:

    FROM tiangolo/uwsgi-nginx
  3. Replace the EXPOSE line with

    ENV LISTEN_PORT=8000
  4. Right-click on docker-compose.yml and click Compose up.

  5. Open http://localhost:8000 in the browser to view the app.