Skip to content

Commit 20d2e11

Browse files
authored
Merge pull request #1227 from DefectDojo/dev
Merge from Dev
2 parents e67cf8d + dbe75a7 commit 20d2e11

File tree

178 files changed

+73610
-421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+73610
-421
lines changed

DOCKER.md

Lines changed: 145 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,105 @@ Docker compose is not intended for production use.
44
If you want to deploy a containerized DefectDojo to a production environment,
55
use the [Helm and Kubernetes](KUBERNETES.md) approach.
66

7-
## Setup via Docker Compose
7+
## Prerequisites
8+
* Docker version
9+
* Installing with docker-compose requires at least docker 18.09.4 and docker-compose 1.24.0. See "Checking Docker versions" below for version errors during running docker-compose.
10+
* Proxies
11+
* If you're behind a corporate proxy check https://docs.docker.com/network/proxy/ .
812

9-
To start your DefectDojo instance on Docker Compose for the first time, just
10-
run:
13+
14+
## Setup via Docker Compose - introduction
15+
16+
DefectDojo needs several docker images to run. Two of them depend on DefectDojo code:
17+
18+
* django service - defectdojo/defectdojo-django image
19+
* nginx service - defectdojo/defectdojo-nginx image
20+
21+
The nginx image is build based on the django image.
22+
23+
Before running the application, it's advised to build local images to make sure that you'll be working on images consistent with your current code base.
24+
When running the application without building images, the application will run based on:
25+
* a previously locally built image if it exists in the docker cache
26+
* else the images pulled from dockerhub
27+
* https://hub.docker.com/r/defectdojo/defectdojo-django
28+
* https://hub.docker.com/r/defectdojo/defectdojo-nginx
29+
30+
31+
## Setup via Docker Compose - building and running the application
32+
### Building images
33+
34+
To build images and put them in your local docker cache, run:
1135

1236
```zsh
13-
. docker/aliases_release.sh
14-
docker-compose up
37+
docker-compose build
1538
```
1639

40+
To build a single image, run:
41+
42+
```zsh
43+
docker-compose build django
44+
```
1745
or
1846

47+
```
48+
docker-compose build nginx
49+
```
50+
51+
52+
### Run with Docker compose in release mode
53+
To run the application based on previously built image (or based on dockerhub images if none was locally built), run:
54+
1955
```zsh
20-
docker-compose -f docker-compose_base.yml -f docker-compose_uwsgi-release.yml up
56+
docker/setEnv.sh release
57+
docker-compose up
2158
```
2259

23-
This command will run the application based on images commited on dockerhub (or the last images built locally). If you need to be more up to date, see "Build images locally" below
60+
This will run the application based on docker-compose.yml only.
61+
62+
In this setup, you need to rebuild django and/or nginx images after each code change and restart the containers.
2463

25-
**NOTE:** Installing with docker-compose requires the latest version of docker and docker-compose - at least docker 18.09.4 and docker-compose 1.24.0. See "Checking Docker versions" below for version errors during running docker-compose up.
2664

27-
**NOTE:** Installing with docker-compose requires the latest version of docker and docker-compose - at least docker 18.09.4 and docker-compose 1.24.0. See "Checking Docker versions" below for version errors during running docker-compose up.
65+
### Run with Docker compose in development mode with hot-reloading
2866

67+
For development, use:
68+
69+
```zsh
70+
cp dojo/settings/settings.dist.py dojo/settings/settings.py
71+
docker/setEnv.sh dev
72+
docker-compose up
73+
```
74+
75+
This will run the application based on merged configurations from docker-compose.yml and docker-compose.override.dev.yml.
76+
77+
* Volumes are mounted to synchronize between the host and the containers :
78+
* static resources (nginx container)
79+
* python code (uwsgi and celeryworker containers).
80+
81+
* The `--py-autoreload 1` parameter in entrypoint-uwsgi-dev.sh will make uwsgi handle python hot-reloading for the **uwsgi** container.
82+
* Hot-reloading for the **celeryworker** container is not yet implemented. When working on deduplication for example, restart the celeryworker container with:
83+
84+
```
85+
docker restart django-defectdojo_celeryworker_1
86+
```
87+
88+
* The mysql port is forwarded to the host so that you can access your database from outside the container.
89+
90+
To update changes in static resources, served by nginx, just refresh the browser with ctrl + F5.
91+
92+
93+
*Notes about volume permissions*
94+
95+
*The manual copy of settings.py is sometimes required once after cloning the repository, on linux hosts when the host files cannot be modified from within the django container. In that case that copy in entrypoint-uwsgi-dev.sh fails.*
96+
97+
*Another way to fix this is changing `USER 1001` in Dockerfile.django to match your user uid and then rebuild the images. Get your user id with*
98+
99+
```
100+
id -u
101+
```
102+
103+
### Access the application
29104
Navigate to <http://localhost:8080> where you can log in with username admin.
30-
To find out the admin user’s password, check the very beginning of the console
105+
To find out the admin password, check the very beginning of the console
31106
output of the initializer container, typically name 'django-defectdojo_initializer_1', or run the following:
32107

33108
```zsh
@@ -43,45 +118,38 @@ or:
43118
docker logs django-defectdojo_initializer_1
44119
```
45120

46-
If you ran DefectDojo with compose before and you want to prevent the
47-
initializer container from running again, define an environment variable
48-
DD_INITIALIZE=false to prevent re-initialization.
49-
50-
### Develop with Docker Compose
51-
52-
For developing the easiset way to make changes is to startup DefectDojo in debug by running:
121+
Beware that when re-running the application several times, there may be several occurrences of "Admin password". In that case you should use the last occurrence.
53122

54-
```zsh
55-
. docker/aliases_dev.sh
56-
docker-compose up
57-
```
123+
### Disable the database initialization
124+
The initializer container can be disabled by exporting: `export DD_INITIALIZE=false`.
58125

59-
or
126+
This will ensure that the database remains unchanged when re-running the application, keeping your previous settings and admin password.
60127

61-
```zsh
62-
docker-compose -f docker-compose_base.yml -f docker-compose_uwsgi-dev.yml up
63-
```
128+
### Versioning
129+
In order to use a specific version when building the images and running the containers, set the environment with
130+
* For the nginx image: `NGINX_VERSION=x.y.z`
131+
* For the django image: `DJANGO_VERSION=x.y.z`
64132

65-
This starts the DefectDojo (uwsgi) container with manage.py and shares the local source directory so that changes to the code immediately restart the process.
133+
Building will tag the images with "x.y.z", then you can run the application based on a specific tagged images.
66134

67-
Navigate to the container directly, <http://localhost:8000>
135+
* Tagged images can be seen with:
68136

69-
The initializer container can be disabled by exporting: `export DD_INITIALIZE=false`
137+
```
138+
$ docker images
139+
REPOSITORY TAG IMAGE ID CREATED SIZE
140+
defectdojo/defectdojo-nginx 1.0.0 bc9c5f7bb4e5 About an hour ago 191MB
141+
```
70142

71-
### Build Images Locally
143+
* This will show on which tagged images the containers are running:
72144

73-
Build the docker containers locally for testing purposes.
145+
```
146+
$ docker ps
147+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
148+
aedc404d6dee defectdojo/defectdojo-nginx:1.0.0 "/entrypoint-nginx.sh" 2 minutes ago Up 2 minutes 80/tcp, 0.0.0.0:8080->8080/tcp django-defectdojo_nginx_1
149+
```
74150

75-
```zsh
76-
# Build Dev Compose
77-
docker-compose build
78151

79-
or:
80152

81-
# Build images
82-
docker build -t defectdojo/defectdojo-django -f Dockerfile.django .
83-
docker build -t defectdojo/defectdojo-nginx -f Dockerfile.nginx .
84-
```
85153

86154
### Clean up Docker Compose
87155

@@ -97,6 +165,43 @@ Removes all containers, networks and the database volume
97165
docker-compose down --volumes
98166
```
99167

168+
### Run the unit-tests with docker
169+
#### Introduction
170+
The unit-tests are under `dojo/unittests`
171+
172+
173+
174+
#### Running the unit-tests
175+
This will run all the tests and leave the uwsgi container up:
176+
177+
```
178+
cp dojo/settings/settings.dist.py dojo/settings/settings.py
179+
docker/setEnv.sh unit_tests
180+
docker-compose up
181+
```
182+
Enter the container to run more tests:
183+
184+
```
185+
docker exec -it django-defectdojo_uwsgi_1 bash
186+
```
187+
Rerun all the tests:
188+
189+
```
190+
python manage.py test dojo.unittests --keepdb
191+
```
192+
193+
Run all the tests from a python file. Example:
194+
195+
```
196+
python manage.py test dojo.unittests.test_dependency_check_parser --keepdb
197+
```
198+
199+
Run a single test. Example:
200+
201+
```
202+
python manage.py test dojo.unittests.test_dependency_check_parser.TestDependencyCheckParser.test_parse_without_file_has_no_findings --keepdb
203+
```
204+
100205
## Checking Docker versions
101206

102207
Run the following to determine the versions for docker and docker-compose:
@@ -129,7 +234,7 @@ OpenSSL version: OpenSSL 1.0.1t 3 May 2016
129234

130235
In this case, both docker (version 17.09.0-ce) and docker-compose (1.18.0) need to be updated.
131236

132-
Follow [Dockers' documentation](https://docs.docker.com/install/) for your OS to get the lastest version of Docker. For the docker command, most OSes have a built-in update mechanism like "apt upgrade".
237+
Follow [Dockers' documentation](https://docs.docker.com/install/) for your OS to get the latest version of Docker. For the docker command, most OSes have a built-in update mechanism like "apt upgrade".
133238

134239
Docker Compose isn't packaged like Docker and you'll need to manually update an existing install if using Linux. For Linux, either follow the instructions in the [Docker Compose documentation](https://docs.docker.com/compose/install/) or use the shell script below. The script below will update docker-compose to the latest version automatically. You will need to make the script executable and have sudo privileges to upgrade docker-compose:
135240

@@ -147,7 +252,7 @@ echo "Note: docker-compose version $VERSION will be downloaded from:"
147252
echo "https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m)"
148253
echo "Enter sudo password to install docker-compose"
149254

150-
# Download and install lastest docker compose
255+
# Download and install latest docker compose
151256
sudo curl -L https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m) -o $DESTINATION
152257
sudo chmod +x $DESTINATION
153258

Dockerfile.django

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
11

22
# code: language=Dockerfile
3-
FROM python:2
3+
4+
# The code for the build image should be idendical with the code in
5+
# Dockerfile.nginx to use the caching mechanism of Docker.
6+
7+
FROM python:2 as build
48
WORKDIR /app
59
RUN \
610
apt-get -y update && \
711
apt-get -y install \
812
dnsutils \
913
mysql-client \
1014
postgresql-client \
15+
xmlsec1 \
16+
&& \
17+
apt-get clean && \
18+
rm -rf /var/lib/apt/lists && \
19+
true
20+
COPY requirements.txt ./
21+
RUN pip wheel --wheel-dir=/tmp/wheels -r ./requirements.txt
22+
23+
FROM python:2-slim
24+
WORKDIR /app
25+
RUN \
26+
apt-get -y update && \
27+
# ugly fix to install postgresql-client without errors
28+
mkdir -p /usr/share/man/man1 /usr/share/man/man7 && \
29+
apt-get -y install --no-install-recommends \
30+
# libopenjp2-7 libjpeg62 libtiff5 are required by the pillow package
31+
libopenjp2-7 \
32+
libjpeg62 \
33+
libtiff5 \
34+
dnsutils \
35+
mysql-client \
36+
libmariadbclient18 \
37+
xmlsec1 \
38+
# only required for the dbshell (used by the initializer job)
39+
postgresql-client \
1140
&& \
1241
apt-get clean && \
1342
rm -rf /var/lib/apt/lists && \
1443
true
44+
RUN pip install --no-cache-dir --upgrade pip
45+
COPY --from=build /tmp/wheels /tmp/wheels
1546
COPY requirements.txt ./
16-
RUN pip install -r ./requirements.txt
47+
RUN pip install \
48+
--no-cache-dir \
49+
--no-index \
50+
--find-links=/tmp/wheels \
51+
-r ./requirements.txt
1752
COPY \
1853
docker/entrypoint-celery-beat.sh \
1954
docker/entrypoint-celery-worker.sh \
2055
docker/entrypoint-initializer.sh \
2156
docker/entrypoint-uwsgi.sh \
2257
docker/entrypoint-uwsgi-dev.sh \
2358
docker/entrypoint-unit-tests.sh \
59+
docker/entrypoint-unit-tests-devDocker.sh \
2460
docker/wait-for-it.sh \
2561
/
2662
COPY wsgi.py manage.py tests/unit-tests.sh ./
@@ -38,7 +74,7 @@ USER 1001
3874
ENV \
3975
DD_ADMIN_USER=admin \
4076
41-
DD_ADMIN_PASSWORD= \
77+
DD_ADMIN_PASSWORD='' \
4278
DD_ADMIN_FIRST_NAME=Administrator \
4379
DD_ADMIN_LAST_NAME=User \
4480
DD_ALLOWED_HOSTS="*" \

Dockerfile.nginx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# code: language=Dockerfile
2-
FROM defectdojo/defectdojo-django:latest AS build
2+
3+
# The code for the build image should be idendical with the code in
4+
# Dockerfile.django to use the caching mechanism of Docker.
5+
6+
FROM python:2 as build
7+
WORKDIR /app
8+
RUN \
9+
apt-get -y update && \
10+
apt-get -y install \
11+
dnsutils \
12+
mysql-client \
13+
postgresql-client \
14+
xmlsec1 \
15+
&& \
16+
apt-get clean && \
17+
rm -rf /var/lib/apt/lists && \
18+
true
19+
COPY requirements.txt ./
20+
RUN pip wheel --wheel-dir=/tmp/wheels -r ./requirements.txt
21+
22+
FROM build AS collectstatic
323

424
USER root
525
RUN \
@@ -15,7 +35,18 @@ RUN \
1535
apt-get clean && \
1636
rm -rf /var/lib/apt/lists && \
1737
true
38+
39+
RUN pip install \
40+
--no-cache-dir \
41+
--no-index \
42+
--find-links=/tmp/wheels \
43+
-r ./requirements.txt
44+
1845
COPY components/ ./components/
46+
COPY manage.py ./
47+
COPY dojo/ ./dojo/
48+
RUN \
49+
cp dojo/settings/settings.dist.py dojo/settings/settings.py
1950
RUN \
2051
cd components && \
2152
yarn && \
@@ -24,7 +55,7 @@ RUN \
2455
true
2556

2657
FROM nginx
27-
COPY --from=build /app/static/ /usr/share/nginx/html/static/
58+
COPY --from=collectstatic /app/static/ /usr/share/nginx/html/static/
2859
COPY wsgi_params nginx/nginx.conf /etc/nginx/
2960
COPY docker/entrypoint-nginx.sh /
3061
RUN \

PULL_REQUEST_TEMPLATE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Please submit your pull requests to the 'dev' branch.
22

33
When submitting a pull request, please make sure you have completed the following checklist:
44

5-
- [ ] Your code is flake8 compliant (DefectDojo's code isn't currently flake8 compliant, but we're trying to correct that.)
5+
- [ ] Your code is flake8 compliant
66
- [ ] If this is a new feature and not a bug fix, you've included the proper documentation in the ReadTheDocs documentation folder. https://github.com/DefectDojo/Documentation/tree/master/docs or provide feature documentation in the PR.
7-
- [ ] Model changes should include the necessary migrations in the dojo/dd_migrations folder.
7+
- [ ] Model changes must include the necessary migrations in the dojo/dd_migrations folder.
88
- [ ] Add applicable tests to the unit tests.

0 commit comments

Comments
 (0)