From d0286953a9cd95567b24fe819633a7c6bb4123e8 Mon Sep 17 00:00:00 2001 From: cerofrais Date: Thu, 23 Jul 2020 18:40:26 +0530 Subject: [PATCH 1/2] sample docker setup --- sample_docker/.dockerignore | 1 + sample_docker/Dockerfile | 19 +++++++++++ sample_docker/Readme.md | 58 ++++++++++++++++++++++++++++++++++ sample_docker/app.py | 15 +++++++++ sample_docker/requirements.txt | 1 + 5 files changed, 94 insertions(+) create mode 100644 sample_docker/.dockerignore create mode 100644 sample_docker/Dockerfile create mode 100644 sample_docker/Readme.md create mode 100644 sample_docker/app.py create mode 100644 sample_docker/requirements.txt diff --git a/sample_docker/.dockerignore b/sample_docker/.dockerignore new file mode 100644 index 0000000..07f43b8 --- /dev/null +++ b/sample_docker/.dockerignore @@ -0,0 +1 @@ +data/* \ No newline at end of file diff --git a/sample_docker/Dockerfile b/sample_docker/Dockerfile new file mode 100644 index 0000000..4f7c1aa --- /dev/null +++ b/sample_docker/Dockerfile @@ -0,0 +1,19 @@ +# base image is python3.6 installed debian, can be changed to any other os +FROM python:3.6-stretch + +# copying all the files in current path to app folder which can be found in the container also +COPY . /app +# changing dir +WORKDIR /app/ + +# commands that need to be run in terminal of the image that is being built +RUN apt-get update -y +RUN apt-get install python3-pip python3-dev -y +RUN apt-get install build-essential pkg-config -y + +RUN python3 -m pip install requirements.txt + +# exposing port so that can be accessed from out side container(from host system) +EXPOSE 1111 +# command +CMD ["sh","-c","python3 app.py"] diff --git a/sample_docker/Readme.md b/sample_docker/Readme.md new file mode 100644 index 0000000..4dee1ee --- /dev/null +++ b/sample_docker/Readme.md @@ -0,0 +1,58 @@ +# Basic guide to have a dockerzie a flask app + +Flask : https://flask-doc.readthedocs.io/en/latest/ + +Docker: https://docs.docker.com/ + +Fastapi: https://fastapi.tiangolo.com/ + +## CMD to build docker image : +1. Any build arguments, tags needed should be added here. +2. By default . points to Dockerfile and u can give filename by -f argument. + + ```$ sudo docker build -t imagename .``` + +## CMD to run docker image +1. Any environment variables needed to run the docker container goes here with -e argument +2. port mapping is hostport:exposeddockerport +3. -d is for detach, if you need the the logs you can do -it + + ```$ sudo docker run -d -p 1111:1111 --name containername imagename``` + +## To See current running images and containers +1. Images +```$ sudo docker images``` +2. Containers +```$ sudo docker ps -a``` +3. Stats of a running container +```$ sudo docker stats containername``` + + +## CMD to ssh into a running docker container +1. Container should be running + + ```$ sudo docker exec -it containername /bin/bash``` + + + +## Clean up +1. To delete all unused(not attached to any container) images. + + ```$ sudo docker system prune -a``` +2. To stop a running container. + + ```$ sudo docker stop containername``` +3. To delete an image., -f can be used to force + + ```$ sudo docker rmi image name``` + + +### Things to check : +1. If the port being used in the flask app and the port exposed in Dockerfile are same. +2. As we do in git to ignore files we add the path to `.gitignore`, same can be done with `.dockerignore` + +### Other things to checkout: +1. Attaching volumes to a container +2. Adding in newrelic for logging. +3. instead of docker `run` one can try docker `compose`,`start`,`stop` + diff --git a/sample_docker/app.py b/sample_docker/app.py new file mode 100644 index 0000000..8f5eae0 --- /dev/null +++ b/sample_docker/app.py @@ -0,0 +1,15 @@ +from flask import Flask, request, jsonify + +app = Flask(__name__) + +@app.route("/", methods=['GET,POST']) +def index(): + print(request.headers) + print(request.json) + print(request.body) + + return jsonify("success"),200 + +if __name__=="__main__": + app.run(debug=True, host='0.0.0.0', port=1111) + diff --git a/sample_docker/requirements.txt b/sample_docker/requirements.txt new file mode 100644 index 0000000..e80548c --- /dev/null +++ b/sample_docker/requirements.txt @@ -0,0 +1 @@ +flask== \ No newline at end of file From 7f2d856931129987b9fda3895124cce5b42b6c68 Mon Sep 17 00:00:00 2001 From: cerofrais Date: Thu, 23 Jul 2020 18:42:04 +0530 Subject: [PATCH 2/2] added flask version --- sample_docker/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample_docker/requirements.txt b/sample_docker/requirements.txt index e80548c..8c4b3e4 100644 --- a/sample_docker/requirements.txt +++ b/sample_docker/requirements.txt @@ -1 +1 @@ -flask== \ No newline at end of file +flask==1.0.1 \ No newline at end of file