Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Docker Fig real Django


May 22, 2021 Docker From entry to practice


Table of contents


Get started with Django Fig

We will now use Fig to configure and run a Django/PostgreSQL app. Before you do this, make sure that Fig is installed.

Three necessary documents need to be set up before all work can begin.
The first step, because the app is going to run in a Docker container that meets all the environment Dockerfile container is going to install by editing the Dockerfile file. H ere's what it's all about:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

The above specifies that the app will use a mirror with Python installed and the necessary dependent packages. For more information on how to write a Dockerfile file, you can view Mirror Creation and Dockerfile for use.

The second step is to .txt the specific dependent package name that needs to be installed in the requirements.txt file.

Django
psycopg2

It's as simple as that.
In the third step, fig.yml file will associate everything. I t describes the composition of the app (a web service and a database), the Docker image used, the connection between the images, the volume mounted to the container, and the ports open to the service.

db:
  image: postgres
web:
  build: .
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db

Check fig.yml for more detailed workings.

Now we can fig run app with the fig run command.

$ fig run web django-admin.py startproject figexample .

Fig first uses Dockerfile a mirror for the web service, and then uses the image django-admin.py startproject figexample .

This will generate a Django app in the current directory.

$ ls
Dockerfile       fig.yml          figexample       manage.py       requirements.txt

First, we're going to set up the connection information for the database for the app. Replace the figexample/settings.py the node content DATABASES = ... . . .

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

This information is fixed in the postgres Docker image.
Then, run fig up

Recreating myapp_db_1...
Recreating myapp_web_1...
Attaching to myapp_db_1, myapp_web_1
myapp_db_1 |
myapp_db_1 | PostgreSQL stand-alone backend 9.1.11
myapp_db_1 | 2014-01-27 12:17:03 UTC LOG:  database system is ready to accept connections
myapp_db_1 | 2014-01-27 12:17:03 UTC LOG:  autovacuum launcher started
myapp_web_1 | Validating models...
myapp_web_1 |
myapp_web_1 | 0 errors found
myapp_web_1 | January 27, 2014 - 12:12:40
myapp_web_1 | Django version 1.6.1, using settings 'figexample.settings'
myapp_web_1 | Starting development server at http://0.0.0.0:8000/
myapp_web_1 | Quit the server with CONTROL-C.

This web app has already started listening to 5000 ports in your docker daemon (if you're using boot2docker, boot2docker ip you'll see its address).

You can also run other administrative commands on Docker, such as for fig up run the following commands at another terminal:

$ fig run web python manage.py syncdb