From d45fcb79c1f5973f660ee8ca1acf06410924aee7 Mon Sep 17 00:00:00 2001 From: acidmikk Date: Tue, 21 Nov 2023 18:06:13 +0400 Subject: [PATCH 1/2] add dockerfile and paths --- basharin_sevastyan_lab_2/README.md | 0 basharin_sevastyan_lab_2/app1/Dockerfile | 5 +++++ basharin_sevastyan_lab_2/app1/main.py | 6 ++++++ .../app1/var/data/data.txt | 0 basharin_sevastyan_lab_2/app2/Dockerfile | 5 +++++ basharin_sevastyan_lab_2/app2/main.py | 6 ++++++ .../app2/var/data/data.txt | 0 basharin_sevastyan_lab_2/docker-compose.yaml | 19 +++++++++++++++++++ 8 files changed, 41 insertions(+) create mode 100644 basharin_sevastyan_lab_2/README.md create mode 100644 basharin_sevastyan_lab_2/app1/Dockerfile create mode 100644 basharin_sevastyan_lab_2/app1/main.py create mode 100644 basharin_sevastyan_lab_2/app1/var/data/data.txt create mode 100644 basharin_sevastyan_lab_2/app2/Dockerfile create mode 100644 basharin_sevastyan_lab_2/app2/main.py create mode 100644 basharin_sevastyan_lab_2/app2/var/data/data.txt create mode 100644 basharin_sevastyan_lab_2/docker-compose.yaml diff --git a/basharin_sevastyan_lab_2/README.md b/basharin_sevastyan_lab_2/README.md new file mode 100644 index 0000000..e69de29 diff --git a/basharin_sevastyan_lab_2/app1/Dockerfile b/basharin_sevastyan_lab_2/app1/Dockerfile new file mode 100644 index 0000000..ca02ee7 --- /dev/null +++ b/basharin_sevastyan_lab_2/app1/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.11 +COPY . . +COPY var/data /var/data +COPY var/result /var/result +CMD ["python", "app.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app1/main.py b/basharin_sevastyan_lab_2/app1/main.py new file mode 100644 index 0000000..cd9ac48 --- /dev/null +++ b/basharin_sevastyan_lab_2/app1/main.py @@ -0,0 +1,6 @@ +def main(): + pass + + +if __name__ == "__main__": + main() diff --git a/basharin_sevastyan_lab_2/app1/var/data/data.txt b/basharin_sevastyan_lab_2/app1/var/data/data.txt new file mode 100644 index 0000000..e69de29 diff --git a/basharin_sevastyan_lab_2/app2/Dockerfile b/basharin_sevastyan_lab_2/app2/Dockerfile new file mode 100644 index 0000000..ca02ee7 --- /dev/null +++ b/basharin_sevastyan_lab_2/app2/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.11 +COPY . . +COPY var/data /var/data +COPY var/result /var/result +CMD ["python", "app.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app2/main.py b/basharin_sevastyan_lab_2/app2/main.py new file mode 100644 index 0000000..cd9ac48 --- /dev/null +++ b/basharin_sevastyan_lab_2/app2/main.py @@ -0,0 +1,6 @@ +def main(): + pass + + +if __name__ == "__main__": + main() diff --git a/basharin_sevastyan_lab_2/app2/var/data/data.txt b/basharin_sevastyan_lab_2/app2/var/data/data.txt new file mode 100644 index 0000000..e69de29 diff --git a/basharin_sevastyan_lab_2/docker-compose.yaml b/basharin_sevastyan_lab_2/docker-compose.yaml new file mode 100644 index 0000000..4de2fdc --- /dev/null +++ b/basharin_sevastyan_lab_2/docker-compose.yaml @@ -0,0 +1,19 @@ +version: "3" +services: + worker-1: + build: + context: /app1 + dockerfile: Dockerfile + volumes: + - ./data:/var/data + - ./result:/var/result + + worker-2: + depends_on: + - worker-1 + build: + context: /app2 + dockerfile: Dockerfile + volumes: + - ./result:/var/data + - ./result:/var/result \ No newline at end of file -- 2.25.1 From 89c01268ca596d0c74d0fea00c84a22eca4e4f83 Mon Sep 17 00:00:00 2001 From: acidmikk Date: Tue, 21 Nov 2023 21:21:04 +0400 Subject: [PATCH 2/2] is ready --- basharin_sevastyan_lab_2/README.md | 70 +++++++++++++++++++ basharin_sevastyan_lab_2/app1/Dockerfile | 2 +- basharin_sevastyan_lab_2/app1/main.py | 10 ++- .../app1/var/data/max_data.txt | 0 basharin_sevastyan_lab_2/app2/Dockerfile | 2 +- basharin_sevastyan_lab_2/app2/main.py | 9 ++- .../app2/var/data/data.txt | 10 +++ basharin_sevastyan_lab_2/docker-compose.yaml | 10 +-- 8 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 basharin_sevastyan_lab_2/app1/var/data/max_data.txt diff --git a/basharin_sevastyan_lab_2/README.md b/basharin_sevastyan_lab_2/README.md index e69de29..a85382d 100644 --- a/basharin_sevastyan_lab_2/README.md +++ b/basharin_sevastyan_lab_2/README.md @@ -0,0 +1,70 @@ +## Лабораторная работа 1. Вариант 5. + +#### Вариант первой программы: +Ищет в каталоге `/var/data` файл с самым коротким названием и перекладывает его в `/var/result/data.txt`. +#### Вариант второй программы: +Сохраняет произведение первого и последнего числа из файла `/var/data/data.txt` в `/var/result/result.txt`. +### Как запустить: +В директории с файлом `docker-compose` в консоли/терминале ввести команду `docker-compose up` + +#### Файл сборки docker-compose.yaml +```dockerfile +version: "3" +services: + worker-1: + build: + context: /app1 + dockerfile: Dockerfile + + worker-2: + build: + context: /app2 + dockerfile: Dockerfile +``` + +#### Dockerfile каждой программы +```dockerfile +FROM python:3.11 +COPY . . +COPY var/data /var/data +COPY var/result /var/result +CMD ["python", "main.py"] +``` + +#### Код первой программы +```python +import os + + +def main(): + data_dir = 'var/data' + result_file = '/var/result/data.txt' + min_name = min(os.listdir(data_dir)) # Ищем и записываем самое короткое название (включая расширение) + with open(result_file, 'w') as result: + result.write(min_name) + print(f'В файл {result_file} было записано {min_name}') + + +if __name__ == "__main__": + main() +``` + +#### Код второй программы +```python +def main(): + data_path = 'var/data/data.txt' + result_path = 'var/result/result.txt' + data = [] + with open(data_path, "r") as data_file: + data = list(map(lambda x: int(x), data_file.read().split())) + with open(result_path, 'w') as result: + result.write(f'{data[0] * data[-1]}') + print(f'В файл {result_path} было записано {data[0] * data[-1]}') + + +if __name__ == "__main__": + main() +``` + +### Видео +https://youtu.be/D7HKUqotLGA \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app1/Dockerfile b/basharin_sevastyan_lab_2/app1/Dockerfile index ca02ee7..3adf2cd 100644 --- a/basharin_sevastyan_lab_2/app1/Dockerfile +++ b/basharin_sevastyan_lab_2/app1/Dockerfile @@ -2,4 +2,4 @@ FROM python:3.11 COPY . . COPY var/data /var/data COPY var/result /var/result -CMD ["python", "app.py"] \ No newline at end of file +CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app1/main.py b/basharin_sevastyan_lab_2/app1/main.py index cd9ac48..56613c1 100644 --- a/basharin_sevastyan_lab_2/app1/main.py +++ b/basharin_sevastyan_lab_2/app1/main.py @@ -1,5 +1,13 @@ +import os + + def main(): - pass + data_dir = 'var/data' + result_file = '/var/result/data.txt' + min_name = min(os.listdir(data_dir)) # Ищем и записываем самое короткое название (включая расширение) + with open(result_file, 'w') as result: + result.write(min_name) + print(f'В файл {result_file} было записано {min_name}') if __name__ == "__main__": diff --git a/basharin_sevastyan_lab_2/app1/var/data/max_data.txt b/basharin_sevastyan_lab_2/app1/var/data/max_data.txt new file mode 100644 index 0000000..e69de29 diff --git a/basharin_sevastyan_lab_2/app2/Dockerfile b/basharin_sevastyan_lab_2/app2/Dockerfile index ca02ee7..3adf2cd 100644 --- a/basharin_sevastyan_lab_2/app2/Dockerfile +++ b/basharin_sevastyan_lab_2/app2/Dockerfile @@ -2,4 +2,4 @@ FROM python:3.11 COPY . . COPY var/data /var/data COPY var/result /var/result -CMD ["python", "app.py"] \ No newline at end of file +CMD ["python", "main.py"] \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/app2/main.py b/basharin_sevastyan_lab_2/app2/main.py index cd9ac48..27e5242 100644 --- a/basharin_sevastyan_lab_2/app2/main.py +++ b/basharin_sevastyan_lab_2/app2/main.py @@ -1,5 +1,12 @@ def main(): - pass + data_path = 'var/data/data.txt' + result_path = 'var/result/result.txt' + data = [] + with open(data_path, "r") as data_file: + data = list(map(lambda x: int(x), data_file.read().split())) + with open(result_path, 'w') as result: + result.write(f'{data[0] * data[-1]}') + print(f'В файл {result_path} было записано {data[0] * data[-1]}') if __name__ == "__main__": diff --git a/basharin_sevastyan_lab_2/app2/var/data/data.txt b/basharin_sevastyan_lab_2/app2/var/data/data.txt index e69de29..96c54d7 100644 --- a/basharin_sevastyan_lab_2/app2/var/data/data.txt +++ b/basharin_sevastyan_lab_2/app2/var/data/data.txt @@ -0,0 +1,10 @@ +30 +36 +20 +37 +39 +68 +22 +16 +5 +21 \ No newline at end of file diff --git a/basharin_sevastyan_lab_2/docker-compose.yaml b/basharin_sevastyan_lab_2/docker-compose.yaml index 4de2fdc..315ded8 100644 --- a/basharin_sevastyan_lab_2/docker-compose.yaml +++ b/basharin_sevastyan_lab_2/docker-compose.yaml @@ -4,16 +4,8 @@ services: build: context: /app1 dockerfile: Dockerfile - volumes: - - ./data:/var/data - - ./result:/var/result worker-2: - depends_on: - - worker-1 build: context: /app2 - dockerfile: Dockerfile - volumes: - - ./result:/var/data - - ./result:/var/result \ No newline at end of file + dockerfile: Dockerfile \ No newline at end of file -- 2.25.1