Watching for file changes with StatReloader I start by creating a Django project, running django-admin, inside the container shell:
Here’s a quick example of how I use the container as a development environment for Django apps. AdvertisementsĪnd now I can start my project! Step 5: Doing the coding So, when I run my Python web site in the container, I will be able to access it on my host operating system, using the localhost address. I set -net=host, which allows the container to share my host network.
DJANGO DOCKER TUTORIAL CODE
I give the container a name ( myapp), because I like to call my containers by their proper names! I also add -rm, so that when the container terminates, it’ll be deleted (although my source code files will still exist on my host OS).
If you’re using SELinux, you might need to add the :z modifier to the end, e.g.This will allow me to edit files on my host, and have them available inside the container, in real time. ), to make the current directory (PWD) on my host operating system also available inside the container. There are a few things going on this command, so I think they’re worth calling out: Advertisements I’m also declaring a couple of other packages too:
DJANGO DOCKER TUTORIAL INSTALL
Later, we’ll use pip, the Python package manager, to read this file, and install all my requirements into the container. I’m going to do this in a requirements.yml file. My first step is to declare all of my Python dependencies. Advertisements Step 1: Declare Python dependencies Join me on this little journey as I show you how I did it. A VM that I can create again and again, whenever I need a development environment to work in. It’s basically using a container as a single-purpose virtual machine. With a tool like Docker or Podman, I can create a containerised development environment, which contains everything I need to code my app, without having to install it all on my laptop. So, since I wanted to get something up and running quickly, I thought about creating a development environment using a tool that I already know: containers. Personally, if I have to do too much of this stuff, I just get bored and move on to something else. In fact, it can be a drain on your energy, when all you want to do is open up an IDE and start coding. Whenever you’re learning something new, doing all the initial setup work like this can be a drag. I know I have to learn things like pip, easy_install and venv, but what’s the best practice? What’s the best way to install this stuff on my laptop? I had just one (large) problem: navigating the world of Python versions and dependencies. ?)īut recently, I started out on creating a Python/Django application. docker/nginx/default.I’m not a Python developer. The NGINX configuration file forwards requests to the Python container if the path does not start with /static/. RUN SECRET_KEY=secret python3 manage.py collectstaticĬOPY docker/nginx/nf /etc/nginx/conf.d/nfĬOPY -from=build /usr/src/app/build/ /var/www/html/static docker/nginx/Dockerfile # Static AssetsĬOPY example /usr/src/app/example # MODIFY THIS LINE: YOUR FOLDERS ARE DIFFERENTĬOPY rapid /usr/src/app/rapid # MODIFY OR REMOVE THIS LINE: YOUR FOLDERS ARE DIFFERENT Therefore, adapt the Copy Python files section in the following Dockerfile to your needs.