Docker and VSCode logos on an orange background

If you already have Docker on your machine, you can create a new Django project (using any version) using a single command.

Create in an app/ directory:

docker run -v ${PWD}/app:/app -w /app python:3.9-alpine sh -c "pip install Django==3.2 && django-admin startproject app ."

Create in the current working directory:

docker run -v ${PWD}:/app -w /app python:3.9-alpine sh -c "pip install Django==3.2 && django-admin startproject app ."

This is useful if you need to create a project quickly, but don’t want to install a specific version of Django globally.

I’ll explain how it works below:

  • docker run is the command for running a docker container.
  • -v ${PWD}/app:/app maps a volume from the app/ directory in our project to the /app directory in the Docker container. We need this so that our Django project files end up on our local filesystem when we run the startproject command. The ${PWD} part is a command that prints the working directory, because the docker run command requires a full path to the directory.
  • -w /app tells the docker container to work from the /app directory which we mapped in the step above.
  • python:3.9-alpine is the Docker image we are using (it’s available on the Docker Hub).
  • sh -c is the start of the command which we will run inside our Docker image. Whatever we pass in after this bit will be executed as a shell command.
  • pip install Django==3.2 will install Django version 3.2 inside the Docker container when it starts.
  • && is for running multiple shell commands on one line.
  • django-admin startproject app . is the Django CLI command for starting a new project. We’re calling the project app, and providing the . character so the project is created in our current directory (otherwise it will add a new subdirectory at app/app/).
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.