-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[+] C++ libboost-filesystem1.81.0 [+] Docker C++ для сборки и тестирования [+] Как создавать ключи в Git [+] MySQL Создание таблиц [+] Пример на Python для запуска внутри контейнера [+] Пример компиляции и запуска внутри контейнера [+] Docker опорный конспект + Образ с компиляторами [+] MacOS scripts for Docker [+] Запуск Tetris из Docker
- Loading branch information
Denis Stepulenok
committed
Dec 2, 2023
1 parent
98ec08d
commit 5a335ad
Showing
33 changed files
with
631 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
**Что такое Docker и как он может быть полезен для QA?** | ||
|
||
Установка и настройка Docker | ||
============================ | ||
|
||
**Установка Docker в Windows** | ||
|
||
https://docs.docker.com/desktop/install/windows-install/ | ||
|
||
Проверка, что Docker работает: | ||
|
||
```sh | ||
docker --version | ||
``` | ||
|
||
У меня в Windows выводится: | ||
|
||
``` | ||
Docker version 24.0.6, build ed223bc | ||
``` | ||
|
||
Проверка что Docker работает: | ||
|
||
```sh | ||
docker run hello-world | ||
``` | ||
|
||
**Установка Docker в Linux и MacOS** | ||
https://docs.docker.com/desktop/install/mac-install/ | ||
|
||
```sh | ||
brew install docker docker-compose docker-machine | ||
brew install --cask virtualbox | ||
``` | ||
|
||
```sh | ||
docker-machine create --driver virtualbox default | ||
``` | ||
|
||
https://hub.docker.com/repositories/stden | ||
|
||
Авторизуйтесь в Docker Hub через командную строку: | ||
|
||
```sh | ||
docker login | ||
``` | ||
|
||
Основы Dockerfile: создание образа для тестовой среды. | ||
Интеграция Docker с Python. | ||
|
||
```Dockerfile | ||
# Используем официальный образ Python как родительский образ | ||
FROM python:latest | ||
# Установим рабочий каталог в контейнере | ||
WORKDIR /app | ||
# Скопируем файл с зависимостями в рабочий каталог | ||
COPY requirements.txt ./ | ||
# Установим все необходимые библиотеки из файла requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
# Установим команду tree внутрь контейнера | ||
RUN apt-get update && apt-get install -y tree | ||
# Копируем файл скрипта в рабочую директорию | ||
COPY my-test.py /app | ||
# Скопируем содержимое локального каталога в рабочий каталог контейнера | ||
COPY . . | ||
# Команда, которая будет выполнена при запуске контейнера | ||
CMD ["python", "./my-test.py"] | ||
``` | ||
|
||
Docker Compose | ||
============== | ||
|
||
Чтобы собрать среду с Python и MySQL для запуска модульных тестов, вам потребуется использовать Docker Compose. | ||
Docker Compose позволяет определить и запустить много-контейнерные приложения Docker с помощью простого файла YAML. Вот | ||
как это можно сделать: | ||
|
||
```sh | ||
docker-compose up --build | ||
``` | ||
|
||
```sh | ||
docker-compose exec app python -m unittest discover -s tests | ||
``` | ||
|
||
Тома и хранение данных: сохранение результатов тестов. | ||
|
||
Запуск баз данных, таких как MySQL и PostgreSQL, в Docker можно выполнить с использованием официальных образов из | ||
Docker Hub. | ||
|
||
```sh | ||
# Запустить контейнер MySQL | ||
export DATA=/Users/x/Desktop/mysql-data | ||
docker run -d mysql:latest --name mysql-x -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin12345 $DATA:/var/lib/mysql | ||
|
||
# Параметры: | ||
# --name mysql-x - Имя, которое вы хотите присвоить вашему контейнеру | ||
# -e MYSQL_ROOT_PASSWORD=... - Установить переменную окружения MYSQL_ROOT_PASSWORD, | ||
# которая определяет пароль пользователя root в MySQL | ||
# -d - Запустить контейнер в фоновом режиме | ||
# mysql:tag - Имя образа и его тег; tag может быть, например, 'latest' | ||
# -p 3306:3306 - Чтобы подключаться к MySQL c хост машины | ||
# Для сохранения данных, которые будут сохраняться после перезапуска контейнера, вы должны примонтировать том | ||
``` | ||
|
||
```sh | ||
docker stop mysql-x | ||
docker rm -f mysql-x | ||
``` | ||
|
||
PostgreSQL | ||
---------- | ||
|
||
```sh | ||
# Запустить контейнер PostgreSQL | ||
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres:latest | ||
|
||
# Параметры: | ||
# --name some-postgres - Имя, которое вы хотите присвоить вашему контейнеру | ||
# -e POSTGRES_PASSWORD=... - Установить переменную окружения POSTGRES_PASSWORD, | ||
# которая определяет пароль для пользователя postgres | ||
# -d - Запустить контейнер в фоновом режиме | ||
# postgres:tag - Имя образа и его тег; tag может быть, например, 'latest' | ||
``` | ||
|
||
Для сохранения базы данных на хост-машине, вы можете использовать тома: | ||
|
||
```sh | ||
docker run --name some-postgres -v /my/own/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=mysecretpassword -d postgres:tag | ||
``` | ||
|
||
Уборка Docker: | ||
-------------- | ||
|
||
```sh | ||
docker container prune -f | ||
docker volume prune -f | ||
docker network prune -f | ||
``` | ||
|
||
Уборка всего мусора: | ||
|
||
```sh | ||
docker system prune -f | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hw_pascal | ||
hw_pascal.o | ||
hw_cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Официальный образ Python как базовый | ||
FROM python:latest | ||
|
||
# LaTeX, компилятор C++ и FreePascal | ||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends \ | ||
texlive \ | ||
texlive-latex-extra \ | ||
texlive-fonts-recommended \ | ||
g++ \ | ||
fp-compiler \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Дополнительные Python библиотеки если необходимо | ||
RUN pip install --no-cache-dir numpy pandas | ||
# Установим команду tree внутрь контейнера | ||
RUN apt-get update && apt-get install -y tree | ||
# Папка для нашего кода | ||
WORKDIR /x | ||
# Копируем наш код в контейнер | ||
COPY . /x | ||
RUN chmod +x /x/test.sh | ||
# Команда по умолчанию, когда контейнер запускается | ||
CMD /x/test.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <iostream> | ||
|
||
int main() { | ||
std::cout << "== Программа на C++ ==" << std::endl; | ||
for(int i = 2; i <= 7; ++i) | ||
std::cout << "Квадрат числа " << i << " равен " << i * i << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var i: Integer; | ||
begin | ||
WriteLn('Hello, World!'); | ||
Writeln('Привет всем!'); | ||
for i := 2 to 7 do | ||
WriteLn('Квадрат числа ', i, ' равен ', i * i); | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env python3 | ||
print("== Программа на Python ==") | ||
for i in range(2, 8): | ||
print(f"Квадрат числа {i} равен {i * i}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
brew install fpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
echo --== Free Pascal ==-- | ||
fpc hw_pascal.pas | ||
./hw_pascal | ||
|
||
echo --== GNU C++ ==-- | ||
g++ -o hw_cpp hw_cpp.cpp | ||
./hw_cpp | ||
|
||
echo --== Python ==-- | ||
./hw_python.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export USER=stden | ||
export IMAGE=compilers | ||
export TAG=latest | ||
echo ===== Build container ===== | ||
docker build -t $IMAGE . | ||
echo ===== Tag container ===== | ||
docker tag $IMAGE:$TAG $USER/$IMAGE:$TAG | ||
echo ===== Push to https://hub.docker.com/repository/docker/stden/compilers ===== | ||
docker push $USER/$IMAGE:$TAG | ||
echo ===== Show files tree in container ===== | ||
docker run --rm $IMAGE tree /x | ||
echo ===== Run container ===== | ||
docker run $IMAGE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Официальный образ GNU C++ как базовый | ||
FROM gcc:latest | ||
|
||
# Компилятор C++ | ||
RUN apt-get update && \ | ||
apt-get install -y git cmake g++ libboost-filesystem1.81.0 gcovr lcov valgrind tree | ||
# Не работает последняя версия libboost-filesystem1.83.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
set USER=stden | ||
set IMAGE=cpp | ||
set TAG=latest | ||
@echo ===== Build container ===== | ||
docker build -t %IMAGE% . | ||
@echo ===== Tag container ===== | ||
docker tag %IMAGE%:%TAG% %USER%/%IMAGE%:%TAG% | ||
@echo ===== Push to https://hub.docker.com/repository/docker/stden/cpp ===== | ||
docker push %USER%/%IMAGE%:%TAG% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export USER=stden | ||
export IMAGE=cpp | ||
export TAG=latest | ||
echo ===== Build container ===== | ||
docker build -t $IMAGE . | ||
echo ===== Tag container ===== | ||
docker tag $IMAGE:$TAG $USER/$IMAGE:$TAG | ||
echo ===== Push to https://hub.docker.com/repository/docker/stden/compilers ===== | ||
docker push $USER/$IMAGE:$TAG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Пример конфигурации для Python и MySQL | ||
version: '3.8' | ||
services: | ||
db: | ||
image: mysql:latest # Используем готовый образ | ||
environment: | ||
MYSQL_DATABASE: 'my_db' | ||
MYSQL_USER: 'user' | ||
MYSQL_PASSWORD: 'password' | ||
MYSQL_ROOT_PASSWORD: 'password' | ||
ports: | ||
- "3306:3306" | ||
volumes: | ||
- my-db:/var/lib/mysql | ||
- ./my-python-app/my-db.sql:/my-db.sql | ||
command: --init-file=/my-db.sql | ||
|
||
app: | ||
build: my-python-app # Собрать контейнер из папки | ||
environment: | ||
- DB_HOST=db | ||
- DB_USER=user | ||
- DB_PASSWORD=password | ||
- DB_NAME=my_db | ||
depends_on: | ||
- db | ||
|
||
volumes: | ||
my-db: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
echo ===== Run "Hello world" from Docker ===== | ||
docker run hello-world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export USER=stden | ||
export IMAGE=java-run-swing | ||
export TAG=latest | ||
echo ===== Build container ===== | ||
docker build -t $IMAGE . | ||
echo ===== Tag container ===== | ||
docker tag $IMAGE:$TAG $USER/$IMAGE:$TAG | ||
echo ===== Push to https://hub.docker.com/repository/docker/stden/java-run-swing ===== | ||
docker push $USER/$IMAGE:$TAG | ||
echo ===== Show files in container --rm - remove container ===== | ||
docker run --rm $IMAGE ls /app | ||
echo ===== Show files tree in container ===== | ||
docker run $IMAGE tree /app | ||
echo ===== Run container ===== | ||
docker run $IMAGE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM fermiumlabs/latex-docker:latest | ||
|
||
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - | ||
RUN apt-get update -qq && apt-get install -y -qq sshpass | ||
# Компилятор C++ | ||
RUN apt-get update && \ | ||
apt-get install -y git cmake g++ libboost-filesystem1.81.0 gcovr lcov valgrind tree | ||
# Не работает последняя версия libboost-filesystem1.83.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export USER=stden | ||
export IMAGE=latex | ||
export TAG=latest | ||
echo ===== Build container ===== | ||
docker build -t $IMAGE . | ||
echo ===== Tag container ===== | ||
docker tag $IMAGE:$TAG $USER/$IMAGE:$TAG | ||
echo ===== Push to https://hub.docker.com/repository/docker/stden/latex ===== | ||
docker push $USER/$IMAGE:$TAG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
# Используем официальный образ Python как родительский образ | ||
FROM python:latest | ||
# Установим рабочий каталог в контейнере | ||
WORKDIR /app | ||
# Установим команду tree внутрь контейнера | ||
RUN apt-get update && apt-get install -y tree | ||
# Установим рабочий каталог ВНУТРИ контейнера | ||
WORKDIR /x | ||
# Скопируем файл с зависимостями в рабочий каталог | ||
COPY requirements.txt ./ | ||
# Установим все необходимые библиотеки из файла requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
# Установим команду tree внутрь контейнера | ||
RUN apt-get update && apt-get install -y tree | ||
# Копируем файл скрипта в рабочую директорию | ||
COPY my-test.py /app | ||
COPY my-test.py ./ | ||
# Скопируем содержимое локального каталога в рабочий каталог контейнера | ||
# Скопируем все файлы из текущего каталога (где мы собираем контейнер) в WORKDIR внутри | ||
COPY . . | ||
# Команда, которая будет выполнена при запуске контейнера | ||
CMD ["python", "./my-test.py"] | ||
#CMD ["python", "./my-test.py"] | ||
#CMD python ./my-test.py | ||
CMD ./my-test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Создаём базу данных | ||
CREATE DATABASE IF NOT EXISTS my_db; | ||
# create schema my_db; | ||
|
||
# Подключаемся к этой базе данных | ||
use my_db; | ||
|
||
# Создаём таблицы | ||
create table user | ||
( | ||
id int auto_increment primary key, | ||
login varchar(1024) charset utf8mb3 not null, | ||
password_hash varchar(1024) charset utf8mb3 not null | ||
); | ||
|
||
# Добавляем записи в БД | ||
INSERT INTO user (login, password_hash) | ||
VALUE ('admin', md5('admin')); | ||
INSERT INTO user (login, password_hash) | ||
VALUE ('user', md5('user')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import mysql.connector | ||
|
||
mydb = mysql.connector.connect( | ||
host="localhost", | ||
user="root", | ||
password="password", | ||
database="my_db" | ||
) | ||
|
||
cur = mydb.cursor() | ||
cur.execute("SELECT * FROM user") | ||
r = cur.fetchall() | ||
for row in r: | ||
print(row) |
Oops, something went wrong.