-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Docker configuration #497
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7f69f92
Show static files in docker setup
Hritik14 ce64bce
Remove remaining traces of DJANGO_DEV
Hritik14 55faf69
Use nginx server in docker with gunicorn support
Hritik14 be53bb7
Add Makefile
Hritik14 24d8b1d
Update CI unit tests to use Makefile
Hritik14 d24dcb2
Update Travis CI to use Makefile
Hritik14 e01dc44
Make Settings consistent with scancodeio
Hritik14 25292dd
Refine Docker installation
Hritik14 1ab0b28
Add Docker related documentation
Hritik14 c024620
Revert Use cache for pip in CI test
Hritik14 b69dee2
Remove TravisCI tests
Hritik14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.git | ||
.github | ||
|
||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,5 +48,4 @@ jobs: | |
POSTGRES_HOST: localhost | ||
VC_DB_USER: postgres | ||
POSTGRES_PORT: 5432 | ||
DJANGO_DEV: 1 | ||
GH_TOKEN: 1 | ||
GH_TOKEN: 1 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
FROM python@sha256:e9b7e3b4e9569808066c5901b8a9ad315a9f14ae8d3949ece22ae339fff2cad0 | ||
FROM python:3.8 | ||
|
||
# PYTHONUNBUFFERED=1 ensures that the python output is set straight | ||
# to the terminal without buffering it first | ||
# Force unbuffered stdout and stderr (i.e. they are flushed to terminal immediately) | ||
ENV PYTHONUNBUFFERED 1 | ||
RUN mkdir /vulnerablecode | ||
WORKDIR /vulnerablecode | ||
ADD . /vulnerablecode/ | ||
RUN pip install -r requirements.txt && \ | ||
DJANGO_DEV=1 python manage.py collectstatic | ||
|
||
LABEL "base_image": "pkg:docker/python@sha256%3Ae9b7e3b4e9569808066c5901b8a9ad315a9f14ae8d3949ece22ae339fff2cad0" | ||
Hritik14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LABEL "dockerfile_url": "https://github.com/nexB/vulnerablecode/blob/develop/Dockerfile" | ||
LABEL "homepage_url": "https://github.com/nexB/vulnerablecode" | ||
LABEL "license": "Apache-2.0" | ||
RUN mkdir /opt/vulnerablecode && \ | ||
mkdir -p /var/vulnerablecode/static/ | ||
WORKDIR /opt/vulnerablecode | ||
COPY . . | ||
RUN python -m pip install --upgrade pip && \ | ||
pip install -r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# http://nexb.com and https://github.com/nexB/scancode.io | ||
# The ScanCode.io software is licensed under the Apache License version 2.0. | ||
# Data generated with ScanCode.io is provided as-is without warranties. | ||
# ScanCode is a trademark of nexB Inc. | ||
# | ||
# You may not use this software except in compliance with the License. | ||
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software distributed | ||
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
# CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
# | ||
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
# ScanCode.io should be considered or used as legal advice. Consult an Attorney | ||
# for any legal advice. | ||
# | ||
# ScanCode.io is a free software code scanning tool from nexB Inc. and others. | ||
# Visit https://github.com/nexB/scancode.io for support and download. | ||
# Modified for VulnerableCode use | ||
|
||
# Python version can be specified with `$ PYTHON_EXE=python3.x make conf` | ||
PYTHON_EXE?=python3 | ||
VENV=venv | ||
ACTIVATE?=. ${VENV}/bin/activate; | ||
VIRTUALENV_PYZ=etc/thirdparty/virtualenv.pyz | ||
BLACK_ARGS=-l 100 . | ||
# Do not depend on Python to generate the SECRET_KEY | ||
GET_SECRET_KEY=`base64 /dev/urandom | head -c50` | ||
# Customize with `$ make envfile ENV_FILE=/etc/vulnerablecode/.env` | ||
ENV_FILE=.env | ||
# Customize with `$ make postgres VULNERABLECODE_DB_PASSWORD=YOUR_PASSWORD` | ||
VULNERABLECODE_DB_PASSWORD=vulnerablecode | ||
|
||
# Use sudo for postgres, but only on Linux | ||
UNAME := $(shell uname) | ||
ifeq ($(UNAME), Linux) | ||
SUDO_POSTGRES=sudo -u postgres | ||
else | ||
SUDO_POSTGRES= | ||
endif | ||
|
||
virtualenv: | ||
@echo "-> Bootstrap the virtualenv with PYTHON_EXE=${PYTHON_EXE}" | ||
@${PYTHON_EXE} ${VIRTUALENV_PYZ} --never-download --no-periodic-update ${VENV} | ||
|
||
conf: virtualenv | ||
@echo "-> Install dependencies" | ||
@${ACTIVATE} pip install -r requirements.txt | ||
|
||
dev: conf | ||
@echo "-> Configure and install development dependencies" | ||
@${ACTIVATE} pip install -r requirements-dev.txt | ||
|
||
envfile: | ||
@echo "-> Create the .env file and generate a secret key" | ||
@if test -f ${ENV_FILE}; then echo ".env file exists already"; exit 1; fi | ||
@mkdir -p $(shell dirname ${ENV_FILE}) && touch ${ENV_FILE} | ||
@echo SECRET_KEY=\"${GET_SECRET_KEY}\" > ${ENV_FILE} | ||
|
||
check: | ||
@echo "-> Run black validation" | ||
@${ACTIVATE} black --check ${BLACK_ARGS} | ||
|
||
black: | ||
@echo "-> Apply black code formatter" | ||
${VENV}/bin/black ${BLACK_ARGS} | ||
|
||
valid: black | ||
|
||
clean: | ||
@echo "-> Clean the Python env" | ||
rm -rm ${VENV} | ||
|
||
migrate: | ||
@echo "-> Apply database migrations" | ||
${ACTIVATE} ./manage.py migrate | ||
|
||
postgres: | ||
@echo "-> Configure PostgreSQL database" | ||
@echo "-> Create database user 'vulnerablecode'" | ||
${SUDO_POSTGRES} createuser --no-createrole --no-superuser --login --inherit --createdb vulnerablecode || true | ||
${SUDO_POSTGRES} psql -c "alter user vulnerablecode with encrypted password '${VULNERABLECODE_DB_PASSWORD}';" || true | ||
@echo "-> Drop 'vulnerablecode' database" | ||
${SUDO_POSTGRES} dropdb vulnerablecode || true | ||
@echo "-> Create 'vulnerablecode' database" | ||
${SUDO_POSTGRES} createdb --encoding=utf-8 --owner=vulnerablecode vulnerablecode | ||
@$(MAKE) migrate | ||
|
||
sqlite: | ||
@echo "-> Configure SQLite database" | ||
@echo VULNERABLECODE_DB_ENGINE=\"django.db.backends.sqlite3\" >> ${ENV_FILE} | ||
@echo VULNERABLECODE_DB_NAME=\"sqlite3.db\" >> ${ENV_FILE} | ||
@$(MAKE) migrate | ||
|
||
run: | ||
${ACTIVATE} ./manage.py runserver | ||
|
||
test: | ||
@echo "-> Run the test suite" | ||
${ACTIVATE} ${PYTHON_EXE} -m pytest -v -m "not webtest" | ||
|
||
package: conf | ||
@echo "-> Create a VulnerableCode package for offline installation" | ||
@echo "-> Fetch dependencies in thirdparty/ for offline installation" | ||
rm -rf thirdparty && mkdir thirdparty | ||
${VENV}/bin/pip download -r requirements.txt --no-cache-dir --dest thirdparty | ||
@echo "-> Create package in dist/ for offline installation" | ||
${VENV}/bin/python setup.py sdist | ||
|
||
install: virtualenv | ||
@echo "-> Install and configure the Python env with base dependencies, offline" | ||
${VENV}/bin/pip install --upgrade --no-index --no-cache-dir --find-links=thirdparty -e . | ||
|
||
.PHONY: virtualenv conf dev envfile install check valid clean migrate postgres sqlite run test package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
version: '3' | ||
|
||
services: | ||
web: | ||
environment: | ||
- DJANGO_DEV=1 | ||
- VC_DB_HOST=db | ||
db: | ||
image: postgres | ||
env_file: | ||
- docker.env | ||
volumes: | ||
- db_data:/var/lib/postgresql/data/ | ||
|
||
vulnerablecode: | ||
build: . | ||
command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000" | ||
container_name: "vulnerablecode" | ||
command: /bin/sh -c " | ||
./manage.py migrate && | ||
./manage.py collectstatic --no-input --clear && | ||
gunicorn vulnerablecode.wsgi:application -u nobody -g nogroup --bind :8000 --timeout 600 --workers 2" | ||
env_file: | ||
- docker.env | ||
volumes: | ||
- .:/vulnerablecode | ||
ports: | ||
- "8000:8000" | ||
- static:/var/vulnerablecode/static/ | ||
restart: on-failure | ||
depends_on: | ||
- db | ||
db: | ||
image: postgres | ||
environment: | ||
- POSTGRES_DB=vulnerablecode | ||
- POSTGRES_USER=vulnerablecode | ||
- POSTGRES_PASSWORD=vulnerablecode | ||
|
||
nginx: | ||
image: nginx | ||
env_file: | ||
- docker.env | ||
volumes: | ||
- static:/var/vulnerablecode/static/ | ||
- ./etc/nginx/templates/:/etc/nginx/templates/ | ||
ports: | ||
- ${NGINX_PORT:-8000}:80 | ||
depends_on: | ||
- vulnerablecode | ||
|
||
|
||
volumes: | ||
static: | ||
db_data: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
POSTGRES_DB=vulnerablecode | ||
POSTGRES_USER=vulnerablecode | ||
POSTGRES_PASSWORD=vulnerablecode | ||
|
||
DJANGO_SETTINGS_MODULE=vulnerablecode.settings | ||
VULNERABLECODE_DB_HOST=db | ||
|
||
GUNICORN_SERVER=vulnerablecode |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you remove collectstatic in the dockerfile?
For me it makes more sense to have the collectstatic in the base docker image rather to do it at every startup in the docker compose.
We have vulnerable code setup in kubernetes and this change did break it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tardyp sorry for the break.
@Hritik14 @tdruez do you have any insights?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No specific reason from my side, except that the same structure is being used in our other project.
https://github.com/nexB/scancode.io/blob/main/docker-compose.yml
If we revert, we should revert both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see in docker-compose that you actully do the collectstatic in a shared volume so that the static is served by nginx.
I think this works well for this docker-compose design.
It won't work well in a kubernetes deployment as in kubernetes there is usually several instance of the webapp container which will compete doing the migration and doing the static collect.
In my kubernetes design I didn't bother serving the static files using a dedicated static server.
I think it make sense to ship the docker container with static files inside it, even if in the docker-compose you regenerate it. This will anyway allow the two strategies, with or without static file server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, looks like django is not recommending serving static file by the app. I though this was only a performance best practice (which I don't bother with my 4 users), but they say it is also insecure.
https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve
So I think I will have to deploy an nginx as well..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Staticfiles are only supposed to be served by a proper webserver or cdn. You could serve them via the development server using the
--insecure
flag, and it is as it sounds like - insecure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we would have any trouble having the shared volume even if we invoke collectstatic inside the docker image. The best rationale I could think of is, because staticfiles is meant for production, it should be invoked in production.