This page looks best with JavaScript enabled

Packaging a Django Development Environment with Docker from Scratch (5) Nginx

 ·  โ˜• 3 min read

1. Directory Structure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
โ”œโ”€โ”€ django-devops-nginx
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ””โ”€โ”€ nginx.repo
โ”œโ”€โ”€ django-devops-compose
โ”‚   โ”œโ”€โ”€ docker-compose.yml
โ”‚   โ”œโ”€โ”€ www
โ”‚       โ”œโ”€โ”€ conf
โ”‚       โ”‚   โ”œโ”€โ”€ nginx.conf
โ”‚       โ”œโ”€โ”€ log
โ”‚       โ”‚   โ”œโ”€โ”€ nginx
โ”‚       โ”‚   โ”‚    โ”œโ”€โ”€ access.log
โ”‚       โ”‚   โ”‚    โ”œโ”€โ”€ error.log

The Dockerfile for each service’s image is placed in its own folder. The django-devops-compose project finally assembles all the images; the ./www/ directory is mounted under each image’s /var/www/ directory, and is used to provide dynamic configuration information, the code directory, the data directory, and the log directory.

2. Dockerfile

File: django-devops-nginx/Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
FROM centos:7

ADD ./nginx.repo /etc/yum.repos.d/nginx.repo

RUN yum install -y nginx

RUN rm /etc/nginx/nginx.conf

RUN ln -sf /var/www/conf/nginx.conf  /etc/nginx/nginx.conf

CMD ["nginx", "-g", "daemon off;"]

EXPOSE 80

Nginx source file: django-devops-nginx/nginx.repo

1
2
3
4
5
6
7
8
9
[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/

gpgcheck=0

enabled=1

django-devops-compose/docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
version: '2'

services:

    nginx:
      build: ../django-devops-nginx/
      volumes:
        - ./www/:/var/www/
      # links:
      #  - uwsgi
      ports:
        - "80:80"
      expose:
        - "80"

www/conf/nginx.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
user nginx nginx;

worker_processes 2;

events {
  worker_connections 1024;
  use epoll;
}

http {
  include mime.types;
  default_type application/octet-stream;

  upstream django{
        # server unix:///var/www/conf/uwsgi.sock;
        server uwsgi:8000;
  }

  server {

      listen 80;
      server_name 127.0.0.1;
      charset utf-8;

      root   /var/www/app;

      access_log /var/www/log/nginx/access.log;
      error_log /var/www/log/nginx/error.log;

      location /meida {
          alias /var/www/app/media;
      }

      location /static {
          alias /var/www/app/static;
      }

      location / {
          #  include /var/www/conf/uwsgi.params;
          include     /etc/nginx/uwsgi_params;
          uwsgi_pass django;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }

  }
}

Before starting the project with the docker-compose up command, if the relevant images do not exist, they are built automatically and the containers are then started. Alternatively, enter the docker-devops-nginx directory and run docker build . to build the image immediately.

Use docker images to view the image information on the current host.

1
2
REPOSITORY                                 TAG                 IMAGE ID            CREATED              SIZE
djangodevopscompose_nginx                  latest              253f527fa13c        About a minute ago   275.6 MB

Now you can access it through http://127.0.0.1/static/index.html. In the local ./www/log/nginx/access.log you can see the access log information.

3. Debugging Methods

3.1 Logging into the Container over ssh

Start the container and log in over ssh:

1
2
3
4
docker run -it --rm  djangodevops_nginx  /bin/bash

[root@cb769cbf310d /]# uname -a
Linux cb769cbf310d 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

By logging into the container over ssh, you can use Linux commands to operate on the container directly. Write down these operation commands for writing the Dockerfile; this can significantly improve the efficiency of building images.

3.2 Viewing Logs

Because the Nginx configuration sets the log directory inside the mounted directory, you can view the logs locally and debug directly.

3.3 Using Linux as the Development Host

Docker’s support for Windows is not friendly enough. I use CentOS as the development system environment to package images. On the one hand, this avoids some Windows pitfallsโ€”under Windows, Docker also runs through the Linux environment provided by a virtual machine. On the other hand, since the base image defined by the Dockerfile is usually Linux as well, if the host is also Linux, it makes writing and debugging scripts more convenient.


ๅพฎไฟกๅ…ฌไผ—ๅท
WRITTEN BY
ๅพฎไฟกๅ…ฌไผ—ๅท