-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41062d8
commit c490716
Showing
25 changed files
with
726 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1 @@ | ||
{"assignment":"java-advanced-ru/multithreading-spring"} | ||
{"assignment":"java-advanced-ru/docker"} |
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 @@ | ||
.gradle/ | ||
build/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,22 @@ | ||
FROM eclipse-temurin:20-jdk | ||
|
||
WORKDIR ./app | ||
|
||
COPY ./app . | ||
|
||
# Gradle setup | ||
ARG GRADLE_VERSION=8.7 | ||
|
||
RUN apt-get update && apt-get install -yq make unzip | ||
RUN wget -q https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip \ | ||
&& unzip gradle-${GRADLE_VERSION}-bin.zip \ | ||
&& rm gradle-${GRADLE_VERSION}-bin.zip | ||
|
||
ENV GRADLE_HOME=/opt/gradle | ||
RUN mv gradle-${GRADLE_VERSION} ${GRADLE_HOME} | ||
|
||
ENV PATH=$PATH:$GRADLE_HOME/bin | ||
|
||
CMD ["gradle", "run"] | ||
|
||
EXPOSE 8080 |
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 @@ | ||
test: | ||
gradle test |
Large diffs are not rendered by default.
Oops, something went wrong.
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,85 @@ | ||
# Docker | ||
|
||
Docker — это программное обеспечение для автоматизации развёртывания и управления приложениями в средах с поддержкой контейнеризации. В этом домашнем задании мы познакомимся с Docker и упакуем простое приложение на Spring boot в контейнер. | ||
|
||
## Ссылки | ||
|
||
* [Установка Docker](https://docs.docker.com/engine/install/) | ||
* [Как и для чего использовать Docker](https://guides.hexlet.io/ru/docker/) | ||
* [Введение в Docker](https://www.youtube.com/watch?v=dfXuTTV6TVo) | ||
|
||
## Установка | ||
|
||
Для выполнения этого задания вам понадобится установленный Docker | ||
|
||
## Задачи | ||
|
||
* Установите Docker, следуя инструкции для своей операционной системы | ||
|
||
## app | ||
|
||
В директории *app* находится очень простое Spring boot приложение. Все, что оно делает – это выводит приветствие при запросе к корневой странице. Приложение стартует на порту 8080 | ||
|
||
## Задачи | ||
|
||
* Изучите код этого приложения, его мы будем упаковывать в контейнер. | ||
|
||
## Dockerfile | ||
|
||
* Запишите в *Dockerfile* команды для упаковки приложения в образ. | ||
|
||
При создании своего образа унаследуйтесть от образа `eclipse-temurin:20-jdk`. Внутри уже есть установленные jdk20, но нет Gradle. Так как для сборки приложения мы используем Gradle, нужно будет его установить вручную. Как это делается, можно посмотреть в [документации](https://gradle.org/install/#manually). Или просто добавьте код из примера ниже: | ||
|
||
```dockerfile | ||
ARG GRADLE_VERSION=8.2 | ||
|
||
RUN apt-get update && apt-get install -yq make unzip | ||
|
||
RUN wget -q https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip \ | ||
&& unzip gradle-${GRADLE_VERSION}-bin.zip \ | ||
&& rm gradle-${GRADLE_VERSION}-bin.zip | ||
|
||
ENV GRADLE_HOME=/opt/gradle | ||
|
||
RUN mv gradle-${GRADLE_VERSION} ${GRADLE_HOME} | ||
|
||
ENV PATH=$PATH:$GRADLE_HOME/bin | ||
``` | ||
|
||
При старте контейнера должно запускаться приложение. Приложение должно быть доступно снаружи контейнера на порту 3000. | ||
|
||
* Соберите образ при помощи команды: | ||
|
||
```bash | ||
docker build -t <image> . | ||
``` | ||
|
||
С помощью флага -t передается имя образа, включая имя аккаунта и тег. | ||
|
||
* После сборки образа вы сможете увидеть его в списке образов. Выполните команду `docker images` и найдите там свой образ. | ||
|
||
* Запустите контейнер при помощи команды: | ||
|
||
```bash | ||
docker run -it -p 3000:8080 <image> | ||
``` | ||
|
||
Так как приложение внутри контейнера запускается на порту 8080, а снаружи контейнера оно должно быть доступно на порту 3000, нам нужно сопоставить порт 8080 внутри контейнера с портом 3000 на вашем компьютере. Это делается при помощи флага `-p` | ||
|
||
* Откройте браузер и загрузите страницу *http://localhost:3000*. Убедитесь, что все работает. Если все сделано верно, вы увидите приветствие. | ||
|
||
* Остановите образ при помощи комбинации клавиш Ctrl + C. После этого приложение станет недоступно. | ||
|
||
* Мы также можем запустить образ и подключиться к нему при помощи команды: | ||
|
||
```bash | ||
docker run -it -p 3000:8080 <image> bash | ||
``` | ||
|
||
Запустится bash сессия внутри контейнера. Изучите файловую систему внутри контейнера. Протестируйте наше приложение, запустите тесты при помощи команды `gradle test`. Выйти из контейнера можно при помощи комбинации клавиш Ctrl + D | ||
|
||
## Подсказки | ||
|
||
* Изучите файлы с примерами в директории *Examples* | ||
|
||
* Не забывайте, что внутри контейнера теперь доступен gradle, который поможет вам собрать и запустить приложение |
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,26 @@ | ||
plugins { | ||
id 'org.springframework.boot' version '3.0.6' | ||
id 'io.spring.dependency-management' version '1.1.0' | ||
id 'application' | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
compileJava { | ||
options.release = 20 | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
application { | ||
mainClass = 'exercise.App' | ||
} |
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 @@ | ||
rootProject.name = 'simple-application' |
12 changes: 12 additions & 0 deletions
12
java-advanced-ru/docker/app/src/main/java/exercise/App.java
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,12 @@ | ||
package exercise; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class App { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(App.class, args); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
java-advanced-ru/docker/app/src/main/java/exercise/WelcomeController.java
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 @@ | ||
package exercise; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class WelcomeController { | ||
|
||
@GetMapping("/") | ||
public String root() { | ||
return "Great! We start Spring Boot application in Docker"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
java-advanced-ru/docker/app/src/test/java/exercise/AppTest.java
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 @@ | ||
package exercise; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
public class AppTest { | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@Test | ||
public void testRootPage() throws Exception { | ||
String body = this.restTemplate.getForObject( | ||
"http://localhost:" + port + "/", | ||
String.class | ||
); | ||
assertThat(body).contains("Great! We start Spring Boot application in Docker"); | ||
} | ||
} |
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,17 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
task checkSolution { | ||
File file = file('./Dockerfile') | ||
|
||
doLast { | ||
if (file.length() <= 1) { | ||
throw new Error("You need to add solution to Dockerfile") | ||
} | ||
} | ||
} | ||
|
||
test { | ||
dependsOn checkSolution | ||
} |
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,16 @@ | ||
# Указываем образ, от которого наследуемся | ||
FROM php:7.4-cli | ||
|
||
# Устанавливаем рабочую директорию | ||
WORKDIR /project | ||
|
||
# Копируем директорию из основной файловой системы внутрь образа | ||
COPY ./project . | ||
|
||
# Определяет действие, которое будет выполнено при старте контейнера (docker run) | ||
# Используется только в случае, если контейнер был запущен без указания команды | ||
# [команда, аргумент] | ||
CMD ["echo", "Hello Hexlet!"] | ||
|
||
# Сообщаем, что контейнер использует 8000 порт | ||
EXPOSE 8000 |
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,12 @@ | ||
# Используется для сборки образа | ||
# Флаг -t позволяет указать имя образа | ||
docker build . -t my_account_name/awesome-image | ||
# Показывает все собранные образы | ||
docker images | ||
# Запускает контейнер | ||
# Флаг -p используется для проброса портов: <порт внутри контейнера>:<порт доступный снаружи> | ||
docker run -it -p 80:8080 my_account_name/awesome-image | ||
# Отправляет образ на dockerhub | ||
docker push my_account_name/awesome-image | ||
# А так мы можем запустить образ и подключиться к нему с помощью bash | ||
docker run -it -p 80:8080 my_account_name/awesome-image bash |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
java-advanced-ru/docker/gradle/wrapper/gradle-wrapper.properties
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 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.