Use Docker to create a new Django project in one line
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 runis the command for running a docker container.-v ${PWD}/app:/appmaps 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 thestartprojectcommand. The${PWD}part is a command that prints the working directory, because thedocker runcommand requires a full path to the directory.-w /apptells the docker container to work from the /app directory which we mapped in the step above.python:3.9-alpineis the Docker image we are using (it’s available on the Docker Hub).sh -cis 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.2will 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/).










Leave a Reply
Want to join the discussion?Feel free to contribute!