From 79fac3d889f759f6d2d97b5152159466fcd614fb Mon Sep 17 00:00:00 2001 From: joaquintz Date: Fri, 31 Aug 2018 06:48:04 +0200 Subject: [PATCH 1/9] Added initial version of Dockerfile. --- Dockerfile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7fc2373 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.6-alpine + +WORKDIR /app +# Copy our current directory. +ADD . /app + +# This will be shown on docker build +RUN python --version +RUN pip install --trusted-host pypi.python.org -r requirements.txt + +# This means that you can access the cointainer's port 80, not that our actual +# machine will have the port 5000 accessible. +EXPOSE 5000 + + +# Run app. +CMD FLASK_APP=main.py flask run +# CMD gunicorn --bind 0.0.0.0:${PORT} application.app:app From a3a1e8bda0ba15cfbf2b19656ec1395b088edabb Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 00:31:15 +0200 Subject: [PATCH 2/9] Add a container for the database --- db/Dockerfile | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 db/Dockerfile diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 0000000..8801b47 --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,3 @@ +FROM postgres:alpine + +# TODO: check if From 72a510393a0885354e3ada5be40d4e1fb1c7bb74 Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 00:31:59 +0200 Subject: [PATCH 3/9] Use gunicorn instead of default wsgi. * also temporarily stop using the alpine python for debug purposes --- Dockerfile | 6 +++--- requirements.txt | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7fc2373..74d77da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.6-alpine +FROM python:3 WORKDIR /app # Copy our current directory. @@ -14,5 +14,5 @@ EXPOSE 5000 # Run app. -CMD FLASK_APP=main.py flask run -# CMD gunicorn --bind 0.0.0.0:${PORT} application.app:app +#CMD FLASK_APP=main.py flask run +CMD gunicorn --bind 0.0.0.0:5000 main:app diff --git a/requirements.txt b/requirements.txt index 342690d..23d29e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ peewee==3.6.4 pyyaml>=3.1,<4 flask-cors==3.0.6 jsmin==2.2.2 +gunicorn>=19.9.0 From d24435a1ffe738832f2bb0d91cb06534ef27b2bb Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 00:32:45 +0200 Subject: [PATCH 4/9] Add a makefile to facilitate docker usage --- Makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..40c1004 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ + +build: + docker build db -t crowdcaptcha-db + docker build . -t crowdcaptcha-web + +run: + docker run -e POSTGRES_PASSWORD=torrepaswd\ + -e POSTGRES_USER=torre \ + -e POSTGRES_DB=crowdcaptcha \ + -h ccdb crowdcaptcha-db & + sleep 3 + docker run -p 4001:5000 crowdcaptcha-web & + +killall: + docker container ls \ + | grep crowdcaptcha | cut -c 1-5 \ + | xargs -I {container_name} \ + docker container kill {container_name} From 64f0e9694d1e7cfbc15eb6b666b0768aec1f8a51 Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 01:00:44 +0200 Subject: [PATCH 5/9] Appropiately connect docker containers. Also modified Makefile's killall command to remove stopped containers, otherwise the names are not freed.. --- Makefile | 9 +++++++-- model.py | 5 ++++- requirements.txt | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 40c1004..077a483 100644 --- a/Makefile +++ b/Makefile @@ -4,15 +4,20 @@ build: docker build . -t crowdcaptcha-web run: + docker network create ccnet docker run -e POSTGRES_PASSWORD=torrepaswd\ -e POSTGRES_USER=torre \ -e POSTGRES_DB=crowdcaptcha \ + --name ccdb \ + --net ccnet \ -h ccdb crowdcaptcha-db & - sleep 3 - docker run -p 4001:5000 crowdcaptcha-web & + sleep 10 + docker run -p 5000:5000 --name ccweb --net ccnet crowdcaptcha-web & killall: docker container ls \ | grep crowdcaptcha | cut -c 1-5 \ | xargs -I {container_name} \ docker container kill {container_name} + docker ps -a | grep crowd | cut -c 1-6 | xargs -I {imnm} docker rm {imnm} + docker network rm ccnet diff --git a/model.py b/model.py index 3d05a37..8cc1f75 100644 --- a/model.py +++ b/model.py @@ -9,7 +9,10 @@ dir_path = os.path.dirname(os.path.realpath(__file__)) db_yml = os.path.join(dir_path, "db.yml") db_config = yaml.load(open(db_yml)) -db = PostgresqlDatabase(db_config["database"], user=db_config["user"], password=db_config["password"]) # TODO: change to mysql. +db = PostgresqlDatabase(db_config["database"], + user=db_config["user"], + password=db_config["password"], + host="ccdb") # TODO: change to mysql. # Development db. #db = SqliteDatabase('crowd_captcha.db') diff --git a/requirements.txt b/requirements.txt index 23d29e6..a6021ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ pyyaml>=3.1,<4 flask-cors==3.0.6 jsmin==2.2.2 gunicorn>=19.9.0 +psycopg2>=2.7.5 From c679c4842ca3b2eb64a22044c2e5bbb10df5593d Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 04:46:16 +0200 Subject: [PATCH 6/9] Remove database details from Makefile; load 'em from yaml --- Makefile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 077a483..d848303 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,11 @@ build: docker build db -t crowdcaptcha-db docker build . -t crowdcaptcha-web - run: docker network create ccnet - docker run -e POSTGRES_PASSWORD=torrepaswd\ - -e POSTGRES_USER=torre \ - -e POSTGRES_DB=crowdcaptcha \ + docker run -e POSTGRES_PASSWORD=`cat db.yml | grep password | cut -c 11-`\ + -e POSTGRES_USER=`cat db.yml | grep user | cut -c 7-` \ + -e POSTGRES_DB=`cat db.yml | grep database | cut -c 11-` \ --name ccdb \ --net ccnet \ -h ccdb crowdcaptcha-db & From b850e0150c69cc15e5d1217d4c63ef027631ad40 Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 3 Sep 2018 04:50:04 +0200 Subject: [PATCH 7/9] Load db hostname from yaml otherwise it cannot be run without docker --- model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model.py b/model.py index 8cc1f75..e2bb80e 100644 --- a/model.py +++ b/model.py @@ -12,7 +12,7 @@ db = PostgresqlDatabase(db_config["database"], user=db_config["user"], password=db_config["password"], - host="ccdb") # TODO: change to mysql. + host=db_config["host"]) # TODO: change to mysql. # Development db. #db = SqliteDatabase('crowd_captcha.db') From b96a200747d4fc57793b4bbd2b5752ad9f8c6fc9 Mon Sep 17 00:00:00 2001 From: joaquintz Date: Mon, 10 Sep 2018 22:16:14 +0200 Subject: [PATCH 8/9] Makefile command to set up dev data. --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index d848303..289fee1 100644 --- a/Makefile +++ b/Makefile @@ -20,3 +20,6 @@ killall: docker container kill {container_name} docker ps -a | grep crowd | cut -c 1-6 | xargs -I {imnm} docker rm {imnm} docker network rm ccnet + +fill_dev_db: + docker exec -it ccweb python /app/dev_setup.py From 518690597510ade1a9d0cfd191ef6ddff10d9273 Mon Sep 17 00:00:00 2001 From: joaquintz Date: Wed, 12 Dec 2018 08:37:26 +0100 Subject: [PATCH 9/9] Reorder dockerfile to optimize build time. For each command in a dockerfile, docker mantains a diff-image cache that can be used for incremental builds. By reordering the instructions in the dockerfile we can first get all the python dependencies that are in the requirements.txt file and then copy the rest of the code. After this commit, one should expect not a full rebuild by just changing a line of code. However, adding a new package will force a full image rebuild. --- Dockerfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 74d77da..eb89131 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,15 @@ FROM python:3 WORKDIR /app -# Copy our current directory. -ADD . /app -# This will be shown on docker build -RUN python --version +# Copy only the requirements so we can have an image with full dependencies. +ADD requirements.txt /app RUN pip install --trusted-host pypi.python.org -r requirements.txt +# Now we can copy the rest of the code. +ADD . /app + + # This means that you can access the cointainer's port 80, not that our actual # machine will have the port 5000 accessible. EXPOSE 5000