Coder Social home page Coder Social logo

demo-docker's Introduction

Demo Docker

As presented at Digital Product School.

You might need to add sudo before the docker commands.

Feel free to contact me for questions/help.

Prerequisites

Install docker and docker-compose so that you can see the version names from the following commands.

docker -v
docker-compose -v

Now you can do either of the following.

  1. Build and run the project from this repo
  2. Create the django project from scratch

1. Build and run the project from the repo

Clone and get into the project

git clone https://github.com/anindyaspaul/demo-docker.git
cd demo-docker

Run the services

docker-compose up --build

Visit localhost:8000

Create the db tables (optional)

Create a superuser (optional)

Take down the services

docker-compose down

2. Create project from scratch

Create project directory

mkdir demo-docker
cd demo-docker

List django dependencies

Create requirements.txt with the following content.

Django
psycopg2

Create Dockerfile

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

Build docker image

docker build -t demo .

List docker images

docker images

Create django project using django-admin

docker run -v ~/codes/dps/demo-docker:/code demo django-admin startproject demo .

Run the project

docker run -v ~/codes/dps/demo-docker:/code -p 8000:8000 demo python manage.py runserver 0.0.0.0:8000

Visit localhost:8000

Create docker compose file to run multiple services (for the db)

Create docker-compose.yml with the following content.

version: '3'

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

Add database configuration in the settings file

Replace the DATABASES entry in demo/settings.py with the following.

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

Run the server again

docker run -v ~/codes/dps/demo-docker:/code -p 8000:8000 demo python manage.py runserver 0.0.0.0:8000

Visit localhost:8000

Run django migrations to create db tables

docker-compose run web python manage.py makemigrations
docker-compose run web python manage.py migrate

Create a superuser for the admin

docker-compose run web python manage.py createsuperuser

Run the services using docker-compose

docker-compose up --build

Some useful commands

docker <command>
  • ps See running containers
  • ps -a See all containers
  • start containers
  • stop containers
  • restart containers
  • rm Remove containers
  • images See all images
  • rmi Remove images
  • image prune Remove intermediate layers to free up storage

Things you might do from here

You can try fiddling with it just to learn more about docker. E.g.

  • automate the makemigrations and the migrate commands.
  • automate the collectstatic command of django.
  • automate the createsuperuser command to pre-populate the database.
  • setup Nginx server that routes requests to the django server.
  • run django server in production mode, i.e., don't use runserver, use uwsgi or gunicorn with Nginx.
  • add Redis database that communicates through unix socket.

All of these should work by running just the docker-compose up --build command.

demo-docker's People

Contributors

anindyaspaul avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.