keretrendszer
keretrendszer

Reputation: 35

How do you containerize a Django app for the purpose of running mulptiple instances?

The idea is to create a Django app what would serve as the backend for an Android application and would serve an web admin interface for managing the mobile application's data. Different sites of the company sometimes need different backends for the same android app (data has to be manageable completely separately). Application will be hosted on Windows server/s. How can I containerize the app so I can run multiple instances of it (listening on different ports of the same IP) and I can move it to different servers if needed and set up a new instance of it there? The Django development part I'm familiar with but I have never used Docker(nor other) containers before.

What I need: Either a tutorial or documentation that deals with this specific topic OR Ordered points with some articles or tips how to get this done.

Upvotes: 1

Views: 530

Answers (2)

khanadnanxyz
khanadnanxyz

Reputation: 46

I would recommend having a docker-compose file, having two services named differently and running on different ports, that's it

version: '2'

services:
  backend:
    ports:
      - host_port:container_port example
      - 8080:8000
    build:
      context: ./directory_containing_docker_file
      dockerfile: .
    restart: unless-stopped
    networks:
      - your-network

  :
    ports:
      - host_port:container_port
      - 8090:8000
    build:
      context: ./directory_containing_docker_file
      dockerfile: .
    restart: unless-stopped
    networks:
      - your-network


networks:
  your-network:
    driver: bridge

Upvotes: 0

xle
xle

Reputation: 310

Is this the kind of thing you wanted?

https://atrisaxena.github.io/projects/docker-basic-django-application-deployment/

The secret to having multiple instances is to map the ports when you run the container.

When you run

docker run -d -p 8000:8000 djangosite

you can change the port mapping by changing the 8000:8000 setting to any <host_port>:<container_port> you want.

e.g. if you follow the example above, you end up exposing port 8000 on the container (EXPOSE 8000 in the Dockerfile). The above command maps port 8000 on the host to 8000 on the container. If you want to then run a second instance of the container on port 8001, you simply run

docker run -d -p 8001:8000 djangosite

Then final step is to use a proxy such as nginx to map the ports on the docker host machine to URLs that are accessible via a browser (i.e. via ports 80 for http and 443 for https).

Regarding moving the container, you simply need to import the docker image that you built onto whichever docker host machine you want, no need to move the source code.

Does this answer your question?

P.S. It is worth noting that the tutorial above recommends running the Django server using manage.py runserver which is NOT the standard way of deploying a Django site. The proper way to do it is to use WSGI or similar (via apache, nginx, gunicorn, etc.) within the container to properly interface with the container boundaries. See https://docs.djangoproject.com/en/3.2/howto/deployment/ for more info on how to properly deploy the site. All of the methods detailed in the documentation can be done within the container (but take care not to make your container too bulky or it will weigh down your host machines).

P.P.S It is also not strictly necessary to tag your docker container to a remote repository as suggested in the linked article. You can build the container locally with docker build (see https://docs.docker.com/engine/reference/commandline/build/) and save the image as a file using docker save (see https://docs.docker.com/engine/reference/commandline/save/). You can then import the image to new hosts using docker load (https://docs.docker.com/engine/reference/commandline/load/).

N.B. Don't confuse docker save and docker load with docker export and docker import because they serve different functions. Read the docs for more info there. docker save and docker load work with images whereas docker export and docker import work directly with containers (i.e. specific instances of an image).

Upvotes: 1

Related Questions