From 4f48d351a1513782024ce413d702204b8320e7eb Mon Sep 17 00:00:00 2001 From: Danny Chan Date: Fri, 16 Jun 2023 00:16:18 -0400 Subject: [PATCH 1/2] Add docker configuration --- .dockerignore | 2 + Dockerfile | 57 +++++++++++++++++++++++ docker-compose.dev.yml | 52 +++++++++++++++++++++ nginx.docker.conf | 101 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.dev.yml create mode 100644 nginx.docker.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b7424716 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +**/node_modules +ve \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..10fd8086 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +FROM ubuntu:20.04 +ARG DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y wget curl tar nano make npm python3.8 python3.8-venv python3-pip nginx + +# Download Node.js tarball +RUN curl -o node.tar.gz -L https://nodejs.org/download/release/v18.16.0/node-v18.16.0-linux-x64.tar.gz + +# Extract Node.js +RUN tar xzf node.tar.gz --strip-components=1 -C /usr/local + +# Cleanup +RUN rm node.tar.gz + +# Add Node.js to PATH +ENV PATH="/usr/local/bin:${PATH}" + +# Verify installation +RUN node --version && npm --version +# ENV PATH=”/node-v18.16.0-linux-x64/bin:${PATH}” +# RUN node -v + +# Download and extract Redis source code +RUN wget http://download.redis.io/releases/redis-5.0.7.tar.gz && \ + tar xzf redis-5.0.7.tar.gz + +# Build Redis +RUN cd redis-5.0.7 && \ + make + +# Install Redis +RUN cd redis-5.0.7 && \ + make install + +# Cleanup unnecessary files +RUN rm -rf redis-5.0.7.tar.gz redis-5.0.7 + +# Create Django App +RUN mkdir -p /app +WORKDIR /app + +# Add Django App +COPY . . +RUN rm -rf /ve + +# Install node packages for svelte +RUN npm install + +# Configure Nginx reverse-proxy to work with our current make configuration +COPY nginx.docker.conf /etc/nginx/sites-available/default + +# Expose port 80 for Nginx +EXPOSE 80 +EXPOSE 8000 +EXPOSE 8125 + +CMD ["make", "runserver"] diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 00000000..c72b7089 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,52 @@ +version: '3.8' +services: + postgres: + image: postgres:13.11-alpine + container_name: 3demos_postgres + restart: always + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + ports: + - '5432:5432' + volumes: + - pgdata:/var/lib/postgresql/data + # redis: + # image: redis:5.0.7-alpine + # container_name: 3demos_redis + # restart: always + # ports: + # - '6379:6379' + # volumes: + # - redisdata:/data + django: + container_name: 3demos_app + build: + context: . + dockerfile: Dockerfile + links: + - postgres:postgres + # - redis:redis + ports: + - 80:80 + - 8000:8000 + - 8125:8125 + # - 8000:8000 # Daphne Web for websockets + command: + - /bin/bash + - -c + - | + # service nginx start + # mkdir /var/log/django + # touch /var/log/django/mathplayground.log + nohup redis-server & + npm run build + make runserver + # npm run dev + volumes: + - ./media/src:/app/media/src/ + - ./mathplayground:/app/mathplayground + - ./node_modules:/app/node_modules +volumes: + pgdata: + # redisdata: \ No newline at end of file diff --git a/nginx.docker.conf b/nginx.docker.conf new file mode 100644 index 00000000..868f49ba --- /dev/null +++ b/nginx.docker.conf @@ -0,0 +1,101 @@ +## +# You should look at the following URL's in order to grasp a solid understanding +# of Nginx configuration files in order to fully unleash the power of Nginx. +# https://www.nginx.com/resources/wiki/start/ +# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ +# https://wiki.debian.org/Nginx/DirectoryStructure +# +# In most cases, administrators will remove this file from sites-enabled/ and +# leave it as reference inside of sites-available where it will continue to be +# updated by the nginx packaging team. +# +# This file will automatically load configuration files provided by other +# applications, such as Drupal or Wordpress. These applications will be made +# available underneath a path with that package name, such as /drupal8. +# +# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. +## + +# Default server configuration +# +server { + listen 80; + server_name localhost; + + location / { + proxy_pass http://localhost:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + # listen 80 default_server; + #listen [::]:80 default_server; + + # SSL configuration + # + # listen 443 ssl default_server; + # listen [::]:443 ssl default_server; + # + # Note: You should disable gzip for SSL traffic. + # See: https://bugs.debian.org/773332 + # + # Read up on ssl_ciphers to ensure a secure configuration. + # See: https://bugs.debian.org/765782 + # + # Self signed certs generated by the ssl-cert package + # Don't use them in a production server! + # + # include snippets/snakeoil.conf; + + # root /var/www/html; + + # Add index.php to the list if you are using PHP + # index index.html index.htm index.nginx-debian.html; + + # server_name _; + + # location / { + # First attempt to serve request as file, then + # as directory, then fall back to displaying a 404. + # try_files $uri $uri/ =404; + # } + + # pass PHP scripts to FastCGI server + # + #location ~ \.php$ { + # include snippets/fastcgi-php.conf; + # + # # With php-fpm (or other unix sockets): + # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; + # # With php-cgi (or other tcp sockets): + # fastcgi_pass 127.0.0.1:9000; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} +} + + +# Virtual Host configuration for example.com +# +# You can move that to a different file under sites-available/ and symlink that +# to sites-enabled/ to enable it. +# +#server { +# listen 80; +# listen [::]:80; +# +# server_name example.com; +# +# root /var/www/example.com; +# index index.html; +# +# location / { +# try_files $uri $uri/ =404; +# } +#} \ No newline at end of file From a0e12eaa9562f1d44557f6e114d8025e31d76ce7 Mon Sep 17 00:00:00 2001 From: Danny Chan Date: Fri, 16 Jun 2023 14:55:32 -0400 Subject: [PATCH 2/2] YCV-154 Update README for docker instructions --- README.md | 21 +++++++++++++++++++++ docker-compose.dev.yml | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b455d37..e1746e37 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,24 @@ npm run dev Or, make a one-time production build of the applcation with `npm run build`. Both of these commands will update the compiled JS at `media/mathplayground/build/`. + +### Using Docker in Develo0pment + +# What's included +`.dockerignore` - This files is used to ignore anything you don't want to be copied inside the docker image +`Dockerfile` - The docker image instructions of what to include to build the image. +`nginx.docker.conf` - The nginx instructions inside docker for reverse proxy to mimic our infrastructure setup, this is copied to the docker image when built + +# Versions +- ubuntu 20.04 base image +- redis 5.0.7 (which is used in production) +- node 18.16.0 (which is used in Jenkins during our build process) +- python 3.8.10 + +# Running docker +Run `docker compose -f docker-compose.dev.yml up` to start the containers in your terminal. These instructions are unique for using it for development purpose because the `volumnes` are mapped to your local directory. Any changes that is made locally will reflect directly inside the container. + +** Currently only working for Django +** TODO: Fix Svelte hot module re-loading from within docker container + + diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index c72b7089..2f8f2a86 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -36,7 +36,7 @@ services: - /bin/bash - -c - | - # service nginx start + service nginx start # mkdir /var/log/django # touch /var/log/django/mathplayground.log nohup redis-server &