Skip to content

Commit 8c558b8

Browse files
authored
Merge pull request #1045 from ptrovatelli/fix-docker-with-override
Fix docker with override
2 parents 754db6a + 358c433 commit 8c558b8

15 files changed

+348
-88
lines changed

DOCKER.md

Lines changed: 146 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +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.
63+
64+
65+
### Run with Docker compose in development mode with hot-reloading
66+
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*
2498

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.
99+
```
100+
id -u
101+
```
26102

103+
### Access the application
27104
Navigate to <http://localhost:8080> where you can log in with username admin.
28-
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
29106
output of the initializer container, typically name 'django-defectdojo_initializer_1', or run the following:
30107

31108
```zsh
@@ -41,45 +118,38 @@ or:
41118
docker logs django-defectdojo_initializer_1
42119
```
43120

44-
If you ran DefectDojo with compose before and you want to prevent the
45-
initializer container from running again, define an environment variable
46-
DD_INITIALIZE=false to prevent re-initialization.
47-
48-
### Develop with Docker Compose
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.
49122

50-
For developing the easiset way to make changes is to startup DefectDojo in debug by running:
51-
52-
```zsh
53-
. docker/aliases_dev.sh
54-
docker-compose up
55-
```
123+
### Disable the database initialization
124+
The initializer container can be disabled by exporting: `export DD_INITIALIZE=false`.
56125

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

59-
```zsh
60-
docker-compose -f docker-compose_base.yml -f docker-compose_uwsgi-dev.yml up
61-
```
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`
62132

63-
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.
64134

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

67-
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+
```
68142

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

71-
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+
```
72150

73-
```zsh
74-
# Build Dev Compose
75-
docker-compose build
76151

77-
or:
78152

79-
# Build images
80-
docker build -t defectdojo/defectdojo-django -f Dockerfile.django .
81-
docker build -t defectdojo/defectdojo-nginx -f Dockerfile.nginx .
82-
```
83153

84154
### Clean up Docker Compose
85155

@@ -95,6 +165,43 @@ Removes all containers, networks and the database volume
95165
docker-compose down --volumes
96166
```
97167

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+
98205
## Checking Docker versions
99206

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

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

130-
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".
131238

132239
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:
133240

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

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

Dockerfile.django

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ COPY \
5656
docker/entrypoint-uwsgi.sh \
5757
docker/entrypoint-uwsgi-dev.sh \
5858
docker/entrypoint-unit-tests.sh \
59+
docker/entrypoint-unit-tests-devDocker.sh \
5960
docker/wait-for-it.sh \
6061
/
6162
COPY wsgi.py manage.py tests/unit-tests.sh ./

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ credentials.
2727
```sh
2828
git clone https://github.com/DefectDojo/django-DefectDojo
2929
cd django-DefectDojo
30-
docker-compose -f docker-compose_base.yml -f docker-compose_uwsgi-release.yml up
30+
# building
31+
docker-compose build
32+
# running
33+
docker-compose up
3134
```
3235

3336
Navigate to <http://localhost:8080>.

docker-compose.override.dev.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
version: '3.7'
3+
services:
4+
uwsgi:
5+
entrypoint: ['/wait-for-it.sh', 'mysql:3306', '-t', '30', '--', '/entrypoint-uwsgi-dev.sh']
6+
volumes:
7+
- '.:/app:z'
8+
environment:
9+
DD_DEBUG: 'on'
10+
celeryworker:
11+
volumes:
12+
- '.:/app:z'
13+
nginx:
14+
volumes:
15+
- './dojo/static/dojo:/usr/share/nginx/html/static/dojo'
16+
mysql:
17+
ports:
18+
- target: 3306
19+
published: 3306
20+
protocol: tcp
21+
mode: host
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
version: '3.7'
3+
services:
4+
nginx:
5+
image: hello-world
6+
entrypoint: []
7+
uwsgi:
8+
entrypoint: ['/wait-for-it.sh', 'mysql:3306', '-t', '30', '--', '/entrypoint-unit-tests-devDocker.sh']
9+
volumes:
10+
- '.:/app:z'
11+
environment:
12+
DD_DEBUG: 'on'
13+
DD_TEST_DATABASE_NAME: ${DD_TEST_DATABASE_NAME:-test_defectdojo}
14+
DD_DATABASE_NAME: ${DD_TEST_DATABASE_NAME:-test_defectdojo}
15+
celerybeat:
16+
environment:
17+
DD_DATABASE_URL: ${DD_TEST_DATABASE_URL:-mysql://defectdojo:defectdojo@mysql:3306/test_defectdojo}
18+
celeryworker:
19+
environment:
20+
DD_DATABASE_URL: ${DD_TEST_DATABASE_URL:-mysql://defectdojo:defectdojo@mysql:3306/test_defectdojo}
21+
initializer:
22+
environment:
23+
DD_INITIALIZE: 'false'
24+
mysql:
25+
ports:
26+
- target: 3306
27+
published: 3306
28+
protocol: tcp
29+
mode: host
30+
environment:
31+
MYSQL_DATABASE: ${DD_TEST_DATABASE_NAME:-test_defectdojo}
32+
volumes:
33+
- defectdojo_data_unittest:/var/lib/mysql
34+
volumes:
35+
defectdojo_data_unittest: {}

docker-compose_base.yml renamed to docker-compose.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
version: '3.7'
33
services:
44
nginx:
5-
image: defectdojo/defectdojo-nginx:latest
5+
build:
6+
context: ./
7+
dockerfile: Dockerfile.nginx
8+
image: defectdojo/defectdojo-nginx:${NGINX_VERSION:-latest}
69
depends_on:
710
- uwsgi
811
ports:
912
- target: 8080
1013
published: ${DD_PORT:-8080}
1114
protocol: tcp
1215
mode: host
16+
uwsgi:
17+
build:
18+
context: ./
19+
dockerfile: Dockerfile.django
20+
image: defectdojo/defectdojo-django:${DJANGO_VERSION:-latest}
21+
depends_on:
22+
- mysql
23+
entrypoint: ['/wait-for-it.sh', 'mysql:3306', '-t', '30', '--', '/entrypoint-uwsgi.sh']
24+
environment:
25+
DD_DEBUG: 'off'
26+
DD_ALLOWED_HOSTS: ${DD_ALLOWED_HOSTS:-*}
27+
DD_DATABASE_URL: ${DD_DATABASE_URL:-mysql://defectdojo:defectdojo@mysql:3306/defectdojo}
28+
DD_CELERY_BROKER_USER: ${DD_CELERY_BROKER_USER:-guest}
29+
DD_CELERY_BROKER_PASSWORD: ${DD_CELERY_BROKER_USER:-guest}
1330
celerybeat:
1431
image: defectdojo/defectdojo-django:latest
1532
depends_on:
@@ -51,5 +68,9 @@ services:
5168
MYSQL_PASSWORD: ${DD_DATABASE_PASSWORD:-defectdojo}
5269
MYSQL_DATABASE: ${DD_DATABASE_NAME:-defectdojo}
5370
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
71+
volumes:
72+
- defectdojo_data:/var/lib/mysql
5473
rabbitmq:
5574
image: rabbitmq:3.7
75+
volumes:
76+
defectdojo_data: {}

docker-compose_uwsgi-dev.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)