A simple django website which has two services, one for gunicorn and the other for ngnix
61
Simple django website with two servicese (gunicorn, ngnix)
This repo contains code to spin up a boilerplate Django project with Docker Compose.
It will be hosted locally using Gunicorn and Nginx containers.
Run services in the background:
docker-compose up -d
Run services in the foreground:
docker-compose up --build
Inspect volume:
docker volume ls
and
docker volume inspect <volume name>
Prune unused volumes:
docker volume prune
View networks:
docker network ls
Bring services down:
docker-compose down
Open a bash session in a running container:
docker exec -it <container ID> /bin/bash
The docker-compose yaml file will first spin up the Gunicorn container that will run the Django project at port 8000
The entrypoint to the django_gunicorn service is entrypoint.sh. This script will do a database migration and it will also collect the static files used by the Django project.
The static files will be collected in STATIC_ROOT. This is the /static directory in the container.
This directory is mounted to a Docker volume on the local machine.
The next container that will be spun is Nginx. The Dockerfile for this container is in the /nginx folder. The Nginx configuration will interact with the Gunicorn service at port 8000 and it will also serve the static files in /static also mounted to the same volume.
You will be able to reach the Django project at 0.0.0.0:80. This is the Nginx endpoint that interacts with Gunicorn at 0.0.0.0:8000
To validate that the static files are being served correctly, you can visit 0.0.0.0:80/admin. This endpoint will show you the admin page with the correct style used. Gunicorn does not serve static files, so if you visit 0.0.0.0:8000/admin - the admin page will pop up without the default style.

$ docker
$ docker version
$ docker info
$ docker container run -it -p 80:80 nginx
$ docker container run -d -p 80:80 nginx
$ docker container run -d -p 80:80 nginx
$ docker container run -d -p 80:80 --name nginx-server nginx
$ docker container ls
OR
$ docker ps
$ docker container ls -a
$ docker container stop [ID]
$ docker stop $(docker ps -aq)
$ docker container rm [ID]
$ docker container rm -f [ID]
$ docker container rm [ID] [ID] [ID]
$ docker rm $(docker ps -aq)
$ docker container logs [NAME]
$ docker container top [NAME]
Docker containers are often compared to virtual machines but they are actually just processes running on your host os. In Windows/Mac, Docker runs in a mini-VM so to see the processes youll need to connect directly to that. On Linux however you can run "ps aux" and see the processes directly
$ docker image ls
$ docker pull [IMAGE]
$ docker image rm [IMAGE]
$ docker rmi $(docker images -a -q)
NGINX:
$ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)
APACHE:
$ docker container run -d -p 8080:80 --name apache httpd
MONGODB:
$ docker container run -d -p 27017:27017 --name mongo mongo
MYSQL:
$ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
$ docker container inspect [NAME]
$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]
$ docker container stats [NAME]
$ docker container run -it --name [NAME] nginx bash
For Git Bash, use "winpty"
$ winpty docker container run -it --name [NAME] nginx bash
$ docker container run -it --name ubuntu ubuntu
(no bash because ubuntu uses bash by default)
$ docker container run --rm -it --name [NAME] ubuntu
$ docker container start -ai ubuntu
$ docker container exec -it mysql bash
$ docker container run -it alpine sh
(use sh because it does not include bash) (alpine uses apk for its package manager - can install bash if you want)
$ docker container port [NAME]
$ docker network ls
$ docker network inspect [NETWORK_NAME]
("bridge" is default)
$ docker network create [NETWORK_NAME]
$ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx
$ docker network connect [NETWORK_NAME] [CONTAINER_NAME]
$ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]
$ docker network disconnect
$ docker image ls
Youll see that each image has a tag
$ docker image tag nginx usmanmusa/nginx
$ docker image push usmanmusa/nginx
$ docker login
$ docker image tag usmanmusa/dipense usmanmusa/dipense:1.0
$ docker image build -t [REPONAME] .
FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html
$ docker image build -t nginx-website
$ docker container run -p 80:80 --rm nginx-website
$ docker image tag nginx-website:latest usmanmusa/nginx-website:latest
$ docker image push usmanmusa/dipense
$ docker volume ls
$ docker volume prune
$ docker pull mysql
$ docker image inspect mysql
$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql
$ docker container inspect mysql
$ docker volume ls
There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes
$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
docker volume inspect mysql-db
TIP: Instead of typing out local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder
$ docker container run -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
$ docker container exec -it nginx bash
$ cd /usr/share/nginx/html
$ ls -al
$ touch test.txt
version: '2'
# same as
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve
services:
jekyll:
image: bretfisher/jekyll-serve
volumes:
- .:/site
ports:
- '80:4000'
docker-compose up
docker-compose up -d
docker-compose down
Content type
Image
Digest
sha256:261657ac2…
Size
36.3 MB
Last updated
almost 4 years ago
docker pull usmanmusa/simple_django_site:1