From 05cc45af275c51f499b8203da1d6b20ab95ef833 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 19:41:41 +0300 Subject: [PATCH 01/12] Practice 1 Dockerfile with python --- Dockerfile | 12 ++++++++++++ requirements.txt | 1 + 2 files changed, 13 insertions(+) create mode 100644 Dockerfile create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..b7d9b34b8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:22.04 +RUN apt-get update -y && +RUN apt-get upgrade -y && +RUN apt-get install -y python3 +COPY requirements.txt requirements.txt +RUN `pip install -r requirements.txt` +RUN useradd -ms /bin/bash myuser +USER myuser +COPY app /app +WORKDIR /app +EXPOSE 5000 +CMD ["python3", "app.py"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..0f800fccf --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flask==3.0.0 \ No newline at end of file From c20043421950e9e1c87920673ca23971e44a5453 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 19:47:00 +0300 Subject: [PATCH 02/12] Add missing escape for the pipes --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index b7d9b34b8..d29100469 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM ubuntu:22.04 -RUN apt-get update -y && -RUN apt-get upgrade -y && +RUN apt-get update -y && \ +RUN apt-get upgrade -y && \ RUN apt-get install -y python3 COPY requirements.txt requirements.txt RUN `pip install -r requirements.txt` From 5ba9f55ef90d32f7d963bac1fc081727bbdbcb57 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 19:48:57 +0300 Subject: [PATCH 03/12] Additional changes to RUN command --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d29100469..a077783e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM ubuntu:22.04 RUN apt-get update -y && \ -RUN apt-get upgrade -y && \ -RUN apt-get install -y python3 + apt-get upgrade -y && \ + apt-get install -y python3 COPY requirements.txt requirements.txt RUN `pip install -r requirements.txt` RUN useradd -ms /bin/bash myuser From dc712576cdce12f13e5ba015d893e4aa31ea55bd Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 19:55:20 +0300 Subject: [PATCH 04/12] Add python3-pip --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a077783e8..619b50497 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ FROM ubuntu:22.04 RUN apt-get update -y && \ apt-get upgrade -y && \ - apt-get install -y python3 + apt-get install -y python3 && \ + apt-get install python3-pip COPY requirements.txt requirements.txt RUN `pip install -r requirements.txt` RUN useradd -ms /bin/bash myuser From 69d4ef65a358c28598d91e15139bb8559a998732 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 20:01:16 +0300 Subject: [PATCH 05/12] Add -y --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 619b50497..e1aeb7eae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM ubuntu:22.04 RUN apt-get update -y && \ apt-get upgrade -y && \ apt-get install -y python3 && \ - apt-get install python3-pip + apt-get install -y python3-pip COPY requirements.txt requirements.txt RUN `pip install -r requirements.txt` RUN useradd -ms /bin/bash myuser From 01f66ac53c5777c8acf0eb28489247918a479068 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 20:07:42 +0300 Subject: [PATCH 06/12] Refactor again --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e1aeb7eae..dc9a6cba4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,9 @@ RUN apt-get update -y && \ apt-get install -y python3 && \ apt-get install -y python3-pip COPY requirements.txt requirements.txt -RUN `pip install -r requirements.txt` RUN useradd -ms /bin/bash myuser USER myuser +RUN `pip install -r requirements.txt` COPY app /app WORKDIR /app EXPOSE 5000 From e934bddb41af233ae733b40e23595419ee1a0f2c Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 20:10:09 +0300 Subject: [PATCH 07/12] Refactor 2 --- Dockerfile | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index dc9a6cba4..bc1eb138c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,14 @@ FROM ubuntu:22.04 -RUN apt-get update -y && \ - apt-get upgrade -y && \ - apt-get install -y python3 && \ - apt-get install -y python3-pip -COPY requirements.txt requirements.txt -RUN useradd -ms /bin/bash myuser -USER myuser -RUN `pip install -r requirements.txt` -COPY app /app +RUN apt-get update && \ + apt-get install -y && \ + apt-get install python3 -y && \ + apt-get install pip -y && \ + groupadd -g 1234 notroot && \ + useradd -m -u 1234 -g notroot notroot +USER notroot WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY app . EXPOSE 5000 CMD ["python3", "app.py"] \ No newline at end of file From de09d4a7512b123546d5af5caf7e7a2214102483 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 20:13:47 +0300 Subject: [PATCH 08/12] change port inside app.py to 5000 from 3000 --- app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index 67e0180c0..1c2c83d55 100644 --- a/app/app.py +++ b/app/app.py @@ -11,4 +11,4 @@ def hello_world(): if __name__ == "__main__": - app.run(port=os.environ.get("PORT", 3000), host="0.0.0.0") + app.run(port=os.environ.get("PORT", 5000), host="0.0.0.0") From 6340897fc906d08b10295c6e21b211e0fee1f589 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Wed, 23 Oct 2024 20:43:50 +0300 Subject: [PATCH 09/12] Refactorring --- Dockerfile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index bc1eb138c..8985d30b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ FROM ubuntu:22.04 RUN apt-get update && \ - apt-get install -y && \ - apt-get install python3 -y && \ - apt-get install pip -y && \ - groupadd -g 1234 notroot && \ - useradd -m -u 1234 -g notroot notroot + apt-get install -y \ + python3 -y \ + python3-pip -y && \ + groupadd -g 1234 notroot && \ + useradd -m -u 1234 -g notroot notroot USER notroot WORKDIR /app COPY requirements.txt . From ccf2ffed00d08a31cfbf3d1d55929102dd70e6d7 Mon Sep 17 00:00:00 2001 From: Iliyan Vutov Date: Mon, 28 Oct 2024 18:01:20 +0200 Subject: [PATCH 10/12] Ansible homework --- M1-3-Ansible/README.md | 36 ++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 2 files changed, 40 insertions(+) create mode 100644 M1-3-Ansible/README.md diff --git a/M1-3-Ansible/README.md b/M1-3-Ansible/README.md new file mode 100644 index 000000000..e44faf9c3 --- /dev/null +++ b/M1-3-Ansible/README.md @@ -0,0 +1,36 @@ +# M1-3-1 Configuration Management + +## Ansible Task + +Create an Ansible playbook that build, push and then run the Docker image for the Python +application. Let your playbook has the following variables: + +* `image_name` - contains the name of your image without the tag, i.e. `vutoff/python-app` +* `image_tag` - contains the tag you tagged your image with, i.e. `v0.2` +* `listen_port` - contains the listening port you're binding your app to. + +Make sure that you set environment variable `PORT` when you define your container +in the Ansible playbook that takes its value from `listen_port` variable. + +Use Ansible modules. Do not shell out. + +### Requirements + +* Make sure you have Python installed. Any version above 3.8 would suffice. +* The `requirements.txt` file in this directory contains the required Ansible version. Run + +```sh +pip install -r requirements.txt +``` + +* Make sure that Docker is running on your local machine. + +### Mind the following + +* If you're running Docker Desktop or Rancher Desktop, mind the location of the `docker.sock` file. The location of the socket file is + * Docker Desktop - `${HOME}/.docker/run/docker.sock` + * Rancher DEsktop - ${HOME}/.rd/run/docker.sock + +* If you're using one of the above, when you write your Ansible playbook you +must specify the path to the docker socket with the parameter `docker_host`, +i.e. `docker_host: "unix://{{ ansible_env.HOME }}/.rd/docker.sock"`. diff --git a/requirements.txt b/requirements.txt index b5ba78cca..38a10fb15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,7 @@ +ansible==10.3.0 +ansible-compat==24.9.1 +ansible-core==2.17.5 +ansible-lint==24.9.2 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" From e27a761b73f927707d18273201ed337e27a2f380 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Mon, 4 Nov 2024 12:13:33 +0200 Subject: [PATCH 11/12] Ansible homework --- M1-3-Ansible/docker-playbook.yml | 77 ++++++++++++++++++++++++++++++++ M1-3-Ansible/requirements.txt | 12 +++++ requirements.txt | 15 ------- 3 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 M1-3-Ansible/docker-playbook.yml create mode 100644 M1-3-Ansible/requirements.txt diff --git a/M1-3-Ansible/docker-playbook.yml b/M1-3-Ansible/docker-playbook.yml new file mode 100644 index 000000000..395dd38d8 --- /dev/null +++ b/M1-3-Ansible/docker-playbook.yml @@ -0,0 +1,77 @@ +--- +- name: Build, push and run Docker container + hosts: localhost + vars: + image_name: "ghristov/practice1" + image_tag: "e934bdd" + listen_port: 8080 + full_image_name: "{{ image_name }}:{{ image_tag }}" + dockerfile_path: "../" # Path to the directory containing Dockerfile + + tasks: + - name: Ensure Docker Python SDK is installed + pip: + name: docker + state: present + become: true + + - name: Check if Dockerfile exists in parent directory + stat: + path: "{{ dockerfile_path }}/Dockerfile" + register: dockerfile_check + + - name: Fail if Dockerfile is missing + fail: + msg: "Dockerfile not found in the parent directory" + when: not dockerfile_check.stat.exists + + - name: Build Docker image + community.docker.docker_image: + name: "{{ image_name }}" + tag: "{{ image_tag }}" + source: build + build: + path: "{{ dockerfile_path }}" + pull: yes + force_source: yes + state: present + register: build_result + + - name: Log into Docker registry + community.docker.docker_login: + username: "{{ docker_username }}" + password: "{{ docker_password }}" + when: docker_username is defined and docker_password is defined + register: login_result + + - name: Push Docker image to registry + community.docker.docker_image: + name: "{{ full_image_name }}" + push: yes + source: local + when: + - docker_username is defined + - docker_password is defined + - build_result is succeeded + register: push_result + + - name: Remove any existing container + community.docker.docker_container: + name: python-app + state: absent + force_kill: yes + ignore_errors: yes + + - name: Run Docker container + community.docker.docker_container: + name: python-app + image: "{{ full_image_name }}" + state: started + recreate: yes + pull: false + ports: + - "{{ listen_port }}:{{ listen_port }}" + env: + PORT: "{{ listen_port | string }}" + restart_policy: unless-stopped + when: build_result is succeeded \ No newline at end of file diff --git a/M1-3-Ansible/requirements.txt b/M1-3-Ansible/requirements.txt new file mode 100644 index 000000000..a1a99e359 --- /dev/null +++ b/M1-3-Ansible/requirements.txt @@ -0,0 +1,12 @@ +ansible==10.3.0 +ansible-compat==24.9.1 +ansible-core==2.17.5 +ansible-lint==24.9.2 +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" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e6267b2e8..5ea80b143 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,17 +1,2 @@ -<<<<<<< HEAD -ansible==10.3.0 -ansible-compat==24.9.1 -ansible-core==2.17.5 -ansible-lint==24.9.2 -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" -======= flask==3.0.0 ->>>>>>> practice1 From bb98e1888d6dab0344359991488a287a3d8e5de7 Mon Sep 17 00:00:00 2001 From: Georgi Hristov Date: Mon, 4 Nov 2024 20:36:26 +0200 Subject: [PATCH 12/12] Optimization --- M1-3-Ansible/docker-playbook.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/M1-3-Ansible/docker-playbook.yml b/M1-3-Ansible/docker-playbook.yml index 395dd38d8..0819458d9 100644 --- a/M1-3-Ansible/docker-playbook.yml +++ b/M1-3-Ansible/docker-playbook.yml @@ -1,4 +1,3 @@ ---- - name: Build, push and run Docker container hosts: localhost vars: @@ -6,23 +5,23 @@ image_tag: "e934bdd" listen_port: 8080 full_image_name: "{{ image_name }}:{{ image_tag }}" - dockerfile_path: "../" # Path to the directory containing Dockerfile + dockerfile_path: "../" tasks: - - name: Ensure Docker Python SDK is installed + - name: Is Docker Python SDK installed pip: name: docker state: present become: true - - name: Check if Dockerfile exists in parent directory + - name: Is Dockerfile exists in parent dir stat: path: "{{ dockerfile_path }}/Dockerfile" register: dockerfile_check - name: Fail if Dockerfile is missing fail: - msg: "Dockerfile not found in the parent directory" + msg: "Dockerfile not found" when: not dockerfile_check.stat.exists - name: Build Docker image @@ -55,16 +54,16 @@ - build_result is succeeded register: push_result - - name: Remove any existing container + - name: If existing container then remove community.docker.docker_container: - name: python-app + name: ansible-test state: absent force_kill: yes ignore_errors: yes - name: Run Docker container community.docker.docker_container: - name: python-app + name: ansible-test image: "{{ full_image_name }}" state: started recreate: yes