From 8d006354bf3946df991ba482677a7eb632d2f29f Mon Sep 17 00:00:00 2001 From: Giuliano Taffoni Date: Thu, 15 Nov 2018 17:19:45 +0100 Subject: [PATCH] add Docker exercises --- Docker/01esoclimi/Dockerfile | 35 ++++++++++++++++++++++++++++ "Docker/01esoclimi/Icon\r" | 0 Docker/02App/Dockerfile | 20 ++++++++++++++++ "Docker/02App/Icon\r" | 0 Docker/02App/main.py | 24 +++++++++++++++++++ Docker/02App/requirements.txt | 3 +++ "Docker/03services/Icon\r" | 0 Docker/03services/docker-compose.yml | 18 ++++++++++++++ Docker/04Volumes/Dockerfile | 17 ++++++++++++++ "Docker/04Volumes/Icon\r" | 0 Docker/04Volumes/main.py | 24 +++++++++++++++++++ Docker/04Volumes/requirements.txt | 3 +++ Docker/05compose/Dockerfile | 5 ++++ "Docker/05compose/Icon\r" | 0 Docker/05compose/docker-compose.yml | 8 +++++++ Docker/05compose/main.py | 30 ++++++++++++++++++++++++ Docker/05compose/requirements.txt | 2 ++ Docker/Examples | 25 ++++++++++++++++++++ Docker/pythonApp/Dockerfile | 20 ++++++++++++++++ "Docker/pythonApp/Icon\r" | 0 Docker/pythonApp/main.py | 30 ++++++++++++++++++++++++ Docker/pythonApp/requirements.txt | 2 ++ 22 files changed, 266 insertions(+) create mode 100644 Docker/01esoclimi/Dockerfile create mode 100644 "Docker/01esoclimi/Icon\r" create mode 100644 Docker/02App/Dockerfile create mode 100644 "Docker/02App/Icon\r" create mode 100644 Docker/02App/main.py create mode 100644 Docker/02App/requirements.txt create mode 100644 "Docker/03services/Icon\r" create mode 100644 Docker/03services/docker-compose.yml create mode 100644 Docker/04Volumes/Dockerfile create mode 100644 "Docker/04Volumes/Icon\r" create mode 100644 Docker/04Volumes/main.py create mode 100644 Docker/04Volumes/requirements.txt create mode 100644 Docker/05compose/Dockerfile create mode 100644 "Docker/05compose/Icon\r" create mode 100644 Docker/05compose/docker-compose.yml create mode 100644 Docker/05compose/main.py create mode 100644 Docker/05compose/requirements.txt create mode 100644 Docker/Examples create mode 100644 Docker/pythonApp/Dockerfile create mode 100644 "Docker/pythonApp/Icon\r" create mode 100644 Docker/pythonApp/main.py create mode 100644 Docker/pythonApp/requirements.txt diff --git a/Docker/01esoclimi/Dockerfile b/Docker/01esoclimi/Dockerfile new file mode 100644 index 0000000..1cd85e8 --- /dev/null +++ b/Docker/01esoclimi/Dockerfile @@ -0,0 +1,35 @@ +# I use CentOS latest +FROM centos:latest + +# I set the work directory +WORKDIR /home/mpi4py + +# I copy the content of the current dir into /home/mpi4py +COPY . /home/mpi4py + +# Install some sw +RUN yum install -y vim wget epel-release +RUN yum install -y gsl gsl-devel gcc gcc-gfortran make autoconf patch automake +RUN yum install -y mpich-3.2 mpich-3.2-devel mpich-3.2-autoload environment-modules +RUN yum install -y python-devel numpy python-matplotlib python2-pip + +# I set some env virables +ENV MPI_INCLUDE=/usr/include/mpich-3.2-x86_64 +ENV MPI_PYTHON_SITEARCH=/usr/lib64/python2.7/site-packages/mpich-3.2 +ENV MPI_LIB=/usr/lib64/mpich-3.2/lib +ENV MPI_BIN=/usr/lib64/mpich-3.2/bin +ENV MPI_COMPILER=mpich-3.2-x86_64 +ENV MPI_SYSCONFIG=/etc/mpich-3.2-x86_64 +ENV MPI_SUFFIX=_mpich-3.2 +ENV MPI_MAN=/usr/share/man/mpich-3.2 +ENV MPI_HOME=/usr/lib64/mpich-3.2 +ENV MPI_FORTRAN_MOD_DIR=/usr/lib64/gfortran/modules/mpich-3.2-x86_64 +ENV PATH="/usr/lib64/mpich-3.2/bin:${PATH}" + +# install some python modules +RUN pip install --upgrade pip +RUN pip install mpi4py +RUN pip install astropy +RUN pip install ipython + + diff --git "a/Docker/01esoclimi/Icon\r" "b/Docker/01esoclimi/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/02App/Dockerfile b/Docker/02App/Dockerfile new file mode 100644 index 0000000..2bf89e9 --- /dev/null +++ b/Docker/02App/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Python runtime as a parent image +FROM python:2.7-slim + +# Set the working directory to /app +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --trusted-host pypi.python.org -r requirements.txt + +# Make port 80 available to the world outside this container +EXPOSE 80 + +# Define environment variable +ENV NAME World + +# Run app.py when the container launches +CMD ["python", "main.py"] diff --git "a/Docker/02App/Icon\r" "b/Docker/02App/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/02App/main.py b/Docker/02App/main.py new file mode 100644 index 0000000..7f65252 --- /dev/null +++ b/Docker/02App/main.py @@ -0,0 +1,24 @@ +from flask import Flask +import os +import socket + +visits = 0 +app = Flask(__name__) + +@app.route("/") +def hello(): + try: + with open( "count.txt", "r" ) as f: + count = f.read() + except: + count = 0 + html = "

Hello {name}!

" \ + "Hostname: {hostname}
" \ + "Visits: {visits}" + count = int(count) + with open( "count.txt", "w" ) as f: + f.write( str(count+1) ) + return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=count) + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=80) diff --git a/Docker/02App/requirements.txt b/Docker/02App/requirements.txt new file mode 100644 index 0000000..2a2ca3d --- /dev/null +++ b/Docker/02App/requirements.txt @@ -0,0 +1,3 @@ +Flask +gunicorn +Redis diff --git "a/Docker/03services/Icon\r" "b/Docker/03services/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/03services/docker-compose.yml b/Docker/03services/docker-compose.yml new file mode 100644 index 0000000..b31b996 --- /dev/null +++ b/Docker/03services/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3" +services: + web: + image: hello_odmec + deploy: + replicas: 5 + resources: + limits: + cpus: "0.1" + memory: 50M + restart_policy: + condition: on-failure + ports: + - "4000:80" + networks: + - webnet +networks: + webnet: diff --git a/Docker/04Volumes/Dockerfile b/Docker/04Volumes/Dockerfile new file mode 100644 index 0000000..be69106 --- /dev/null +++ b/Docker/04Volumes/Dockerfile @@ -0,0 +1,17 @@ +# Use an official Python runtime as a parent image +FROM hello_odmec + +# Set the working directory to /app +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Make port 80 available to the world outside this container +EXPOSE 80 + +# Define environment variable +ENV NAME World + +# Run app.py when the container launches +CMD ["python", "main.py"] diff --git "a/Docker/04Volumes/Icon\r" "b/Docker/04Volumes/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/04Volumes/main.py b/Docker/04Volumes/main.py new file mode 100644 index 0000000..ef756ac --- /dev/null +++ b/Docker/04Volumes/main.py @@ -0,0 +1,24 @@ +from flask import Flask +import os +import socket + +visits = 0 +app = Flask(__name__) + +@app.route("/") +def hello(): + try: + with open( "/data/count.txt", "r" ) as f: + count = f.read() + except: + count = 0 + html = "

Hello {name}!

" \ + "Hostname: {hostname}
" \ + "Visits: {visits}" + count = int(count) + with open( "/data/count.txt", "w" ) as f: + f.write( str(count+1) ) + return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=count) + +if __name__ == "__main__": + app.run(host='0.0.0.0', port=80) diff --git a/Docker/04Volumes/requirements.txt b/Docker/04Volumes/requirements.txt new file mode 100644 index 0000000..2a2ca3d --- /dev/null +++ b/Docker/04Volumes/requirements.txt @@ -0,0 +1,3 @@ +Flask +gunicorn +Redis diff --git a/Docker/05compose/Dockerfile b/Docker/05compose/Dockerfile new file mode 100644 index 0000000..eda942d --- /dev/null +++ b/Docker/05compose/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.4-alpine +ADD . /code +WORKDIR /code +RUN pip install -r requirements.txt +CMD ["python", "main.py"] diff --git "a/Docker/05compose/Icon\r" "b/Docker/05compose/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/05compose/docker-compose.yml b/Docker/05compose/docker-compose.yml new file mode 100644 index 0000000..899cf44 --- /dev/null +++ b/Docker/05compose/docker-compose.yml @@ -0,0 +1,8 @@ +version: '3' +services: + web: + build: . + ports: + - "5000:5000" + redis: + image: "redis:alpine" diff --git a/Docker/05compose/main.py b/Docker/05compose/main.py new file mode 100644 index 0000000..cce338f --- /dev/null +++ b/Docker/05compose/main.py @@ -0,0 +1,30 @@ +import time + +import redis +from flask import Flask + + +app = Flask(__name__) +cache = redis.Redis(host='redis', port=6379) + + +def get_hit_count(): + retries = 5 + while True: + try: + return cache.incr('hits') + except redis.exceptions.ConnectionError as exc: + if retries == 0: + raise exc + retries -= 1 + time.sleep(0.5) + + +@app.route('/') +def hello(): + count = get_hit_count() + return 'Hello World! I have been seen {} times.\n'.format(count) + +if __name__ == "__main__": + app.run(host="0.0.0.0", debug=True) + diff --git a/Docker/05compose/requirements.txt b/Docker/05compose/requirements.txt new file mode 100644 index 0000000..1a5dc97 --- /dev/null +++ b/Docker/05compose/requirements.txt @@ -0,0 +1,2 @@ +flask +redis diff --git a/Docker/Examples b/Docker/Examples new file mode 100644 index 0000000..74b1d35 --- /dev/null +++ b/Docker/Examples @@ -0,0 +1,25 @@ +docker run --rm -ti ubuntu:latest bash + +docker run -ti ubuntu:latest bash +--mount type=bind,source="$(pwd)"/target,target=/app + +docker ps -a + +docker attach 9ef4536973c7 +docker start 9ef4536973c7 +docker ps -a +docker attach 9ef4536973c7 +docker rm 9ef4536973c7 +docker ps -a + +export ODMEC="ENV_DATA" +docker run --name ubuntu.test -ti -v $PWD:/data -e ODMEC ubuntu bash + + +docker run -ti -v /data2 ubuntu:latest bash +root@bbdf070861eb:/# cd /data2/ +root@bbdf070861eb:/data2# + + + + diff --git a/Docker/pythonApp/Dockerfile b/Docker/pythonApp/Dockerfile new file mode 100644 index 0000000..2bf89e9 --- /dev/null +++ b/Docker/pythonApp/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Python runtime as a parent image +FROM python:2.7-slim + +# Set the working directory to /app +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --trusted-host pypi.python.org -r requirements.txt + +# Make port 80 available to the world outside this container +EXPOSE 80 + +# Define environment variable +ENV NAME World + +# Run app.py when the container launches +CMD ["python", "main.py"] diff --git "a/Docker/pythonApp/Icon\r" "b/Docker/pythonApp/Icon\r" new file mode 100644 index 0000000..e69de29 diff --git a/Docker/pythonApp/main.py b/Docker/pythonApp/main.py new file mode 100644 index 0000000..e4734fd --- /dev/null +++ b/Docker/pythonApp/main.py @@ -0,0 +1,30 @@ +# [START gae_flex_quickstart] +import logging + +from flask import Flask + + +app = Flask(__name__) + + +@app.route('/') +def hello(): + """Return a friendly HTTP greeting.""" + return 'Hello World!' + + +@app.errorhandler(500) +def server_error(e): + logging.exception('An error occurred during a request.') + return """ + An internal error occurred:
{}
+ See logs for full stacktrace. + """.format(e), 500 + + +if __name__ == '__main__': + # This is used when running locally. Gunicorn is used to run the + # application on Google App Engine. See entrypoint in app.yaml. + app.run(host='127.0.0.1', port=80, debug=True) +# [END gae_flex_quickstart] + diff --git a/Docker/pythonApp/requirements.txt b/Docker/pythonApp/requirements.txt new file mode 100644 index 0000000..a34d076 --- /dev/null +++ b/Docker/pythonApp/requirements.txt @@ -0,0 +1,2 @@ +Flask==1.0.2 +gunicorn==19.9.0 -- GitLab