diff --git a/.python-version b/.python-version new file mode 100644 index 000000000..b6d8b7612 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.11.8 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..40833b198 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM ubuntu:22.04 +WORKDIR /app +COPY --chown=user1 requirements.txt ./ + +# no need to have " / " + +RUN useradd user1 && \ + apt-get update && \ + apt-get upgrade -y && \ + apt-get install -y \ + python3 \ + python3-pip + + +RUN pip3 install --no-cache-dir -r requirements.txt + +COPY --chown=user1 app/ /app + +# EXPOSE 5000 +CMD ["python3" , "-m" ,"flask" , "run" , "--host=0.0.0.0" ] diff --git a/ansDockerPy.yml b/ansDockerPy.yml new file mode 100644 index 000000000..16dd86e92 --- /dev/null +++ b/ansDockerPy.yml @@ -0,0 +1,41 @@ +--- +- name: Build Docker container with my Python app + hosts: localhost + + vars: + image_name: "pythonapp" + image_tag: "v1" + listen_port: 5000 + #dockerhub_uname: "dxs01" + vars_files: + - dockerhubcreds.yml + + tasks: + - name: build docker image + docker_image: + name: "{{ image_name }}" + tag: "{{ image_tag }}" + source: build + build: + path: "." + state: present + + - name: Dockerhub login + shell: docker login -u "{{ docker_uname }}" -p "{{ docker_passwd }}" + + - name: push docker image + docker_image: + name: "{{ image_name }}" + repository: "{{ docker_uname }}/{{ image_name }}" + tag: "{{ image_tag }}" + push: yes + source: local + state: present + - name: run docker image + docker_container: + detach: yes + name: old_excercise_python_app + image: "{{ image_name }}:{{ image_tag }}" + state: started + published_ports: "{{ listen_port }}:{{ listen_port }}" + diff --git a/app/app.py b/app/app.py new file mode 100644 index 000000000..67e0180c0 --- /dev/null +++ b/app/app.py @@ -0,0 +1,14 @@ +import os + +from flask import Flask + +app = Flask(__name__) + + +@app.route("/") +def hello_world(): + return "Hello, World!" + + +if __name__ == "__main__": + app.run(port=os.environ.get("PORT", 3000), host="0.0.0.0") diff --git a/dockerhubcreds.yml b/dockerhubcreds.yml new file mode 100644 index 000000000..68841fc2e --- /dev/null +++ b/dockerhubcreds.yml @@ -0,0 +1,8 @@ +$ANSIBLE_VAULT;1.1;AES256 +33376436373438386531636233366236333332346439623931336436306631643937376261313933 +6535366263356533393061306437656439366235623839310a356466653536373062633162613034 +38323135383836336563653933653061393566666135336263303865636333376461316562363062 +3234633630633165340a626430383130333236346264643864623531303534333133303335383937 +66336263626661356461396232363261343636613830373532333537363862333463366561346539 +30383730316363353836396637616632393935316138303961343562636430316639333966666363 +316537663164396336373566313661363437 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..b5ba78cca --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +blinker==1.6.3 ; python_version >= "3.10" and python_version < "4.0" +click==8.1.7 ; python_version >= "3.10" and python_version < "4.0" +colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows" +flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" +itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" +werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" + diff --git a/test.yml b/test.yml new file mode 100644 index 000000000..ce810eaa4 --- /dev/null +++ b/test.yml @@ -0,0 +1,37 @@ +--- +- name: Build, Push and Run Docker Image for Python Application + hosts: localhost + vars: + image_name: "dxs01/python-app" # Change this to your Docker Hub username and app name + image_tag: "v0.2" + listen_port: 5000 + docker_socket: "unix://{{ ansible_env.HOME }}/.docker/run/docker.sock" # Change path if using Rancher Desktop + + tasks: + - name: Ensure Docker is installed + ansible.builtin.command: docker --version + register: docker_installed + changed_when: false + + - name: Build Docker Image + community.docker.docker_image: + name: "{{ image_name }}" + tag: "{{ image_tag }}" + path: "{{ playbook_dir }}" + state: present + + - name: Push Docker Image to Docker Hub + community.docker.docker_image: + name: "{{ image_name }}" + tag: "{{ image_tag }}" + push: true + + - name: Run Docker Container + community.docker.docker_container: + name: python_app_container + image: "{{ image_name }}:{{ image_tag }}" + state: started + published_ports: + - "{{ listen_port }}:{{ listen_port }}" + env: + PORT: "{{ listen_port }}"