build Django/PostgreSQL application with Docker Compose

STEP1: install docker and docker compose
1. sudo apt-get install docker-ce docker-ce-cli containerd.io
2. sudo groupadd docker
3. ubuntu@ubuntu2004:~$ sudo usermod -aG docker $USER (you should be able to run docker with user ubuntu now)
4. sudo curl -L “https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
5. sudo chmod +x /usr/local/bin/docker-compose

STEP2: create Django_Compose folder and files:
Dockerfile
requirements.txt
docker-compose.yml

ubuntu@ubuntu2004:~/Django_Compose$ cat Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

ubuntu@ubuntu2004:~/Django_Compose$ cat requirements.txt
Django>=3.0,<4.0
psycopg2-binary>=2.8

ubuntu@ubuntu2004:~/Django_Compose$ cat docker-compose.yml
version: "3.9"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

STEP3: Create a Django project
sudo docker-compose run web django-admin startproject composeexample .
sudo chown -R $USER:$USER .

STEP4: edit composeexample/settings.py
Replace the DATABASES = … with the following:
and also update ALLOWED_HOSTS = [‘*’]

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

STEP5 : start docker compose with command “docker-compose up”

ubuntu@ubuntu2004:~/Django_Compose$ docker-compose up
Starting django_compose_db_1 ... done
Starting django_compose_web_1 ... done
Attaching to django_compose_db_1, django_compose_web_1
db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2021-03-11 17:34:58.762 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2021-03-11 17:34:58.762 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2021-03-11 17:34:58.763 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2021-03-11 17:34:58.765 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2021-03-11 17:34:58.770 UTC [25] LOG:  database system was shut down at 2021-03-11 17:25:27 UTC
db_1   | 2021-03-11 17:34:58.775 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | March 11, 2021 - 17:35:00
web_1  | Django version 3.1.7, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/

12 Replies to “build Django/PostgreSQL application with Docker Compose”

  1. I was suggested this website by my cousin. I’m not
    sure whether this post is written by him as nobody else know such detailed
    about my trouble. You’re amazing! Thanks!

  2. Its like you read my thoughts! You seem to understand so much about
    this, like you wrote the e-book in it or something. I feel that you just can do with some
    p.c. to drive the message home a bit, but other than that,
    that is magnificent blog. A fantastic read.
    I’ll definitely be back.

  3. I’m extremely impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it’s rare to see a nice blog like this one today.

    Also visit my blog post – http://www.diigo.com

  4. Howdy! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
    My website goes over a lot of the same subjects as
    yours and I think we could greatly benefit
    from each other. If you are interested feel free to shoot me an e-mail.

    I look forward to hearing from you! Awesome blog by the way!

    My site: Pellamore Cream

  5. Thanks for the sensible critique. Me & my neighbor were just preparing to do some research about this.
    We got a grab a book from our local library but I think I learned
    more from this post. I’m very glad to see such magnificent info being shared freely out
    there.

    Also visit my website; Lilian

  6. Great – I should definitely pronounce, impressed with your
    website. I had no trouble navigating through all tabs as well as related information ended up being truly easy to do to access.
    I recently found what I hoped for before you know it at
    all. Reasonably unusual. Is likely to appreciate it for those who add forums or something,
    website theme . a tones way for your client to
    communicate. Nice task.

    My page :: http://librarius.main.jp

  7. hello!,I really like your writing so a lot!

    share we keep up a correspondence extra about your post on AOL?

    I require a specialist on this space to resolve my problem.
    Maybe that’s you! Having a look forward to see you.

    My website; Alpha Extracts

Leave a Reply

Your email address will not be published. Required fields are marked *