Play Framework HTTPS Hello World với Docker

 Dưới đây là cách cấu hình khung phát để phục vụ giao thông HTTPS với Docker cung cấp thời gian chạy của nó.
 Cài đặt Play Framework sử dụng brew install sbt, sau đó trong quá trình thiết lập ứng dụng mới, cho phép bạn chỉ định versions cho project.

 Tạo mới project với sbt new playframework/play-scala-seed.g8. Nó sẽ tạo ra một thư mục hello-worldvà một thư mục target.

Build project bằng cách nhập hello-worldvà sbt dist, file build target/universal/hello-world-1.0-SNAPSHOT.zip

 Extract đến  1 location, dùng svcđể triển khai:
set -x 
unzip -d svc target / universal / * - 1.0-SNAPSHOT.zip 
mv svc / * / * svc / 
rm svc / bin / * bat 
mv svc / bin / * svc / bin / start
set -x && unzip -d svc target / universal / * - 1.0-SNAPSHOT.zip && mv svc / * / * svc / & & rm svc / bin / * bat && mv svc / bin / * svc / bin / start
Build container Docker với Dockerfilegiống như thế này, có image base openjdk:8-jre, gồm có Play Framework và các thư viện phụ thuộc, bashvà JRE
FROM openjdk: 8-jre 
COPY svc / svc 
EXPOSE 9000 9443 
CMD /svc/bin/start -Dhttps.port=9443 -Dplay.crypto.secret=secret
Xây dựng từ cùng thư mục chứa Dockerfilevà svcvới docker build -t hello-world .
Run Docker container với ports nếu bạn định sử dụng một trình duyệt hoặc curlđể tương tác với nó. Bạn cũng có thể gọi curltừ bên trong container nếu nó được cài đặt ( openjdk:8-jrebó curl, nhưng những thứ khác có thể không).
docker run -it -p 9000: 9000-p 9443: 9443 --rm hello-world
 -ppublishes ports, -itcung cấp điều khiển TTY để bạn có thể CTRL Ccontainer và --rm xóa container ví dụ container (không phải image) khi exit (hai dấu nối, không phải là một dấu gạch nối). Bạn có thể thêm -dđể detach và run nó trong background.
docker psnên resemble, và bạn nhìn thấy các mappings của port:
Tương tác với container sử dụng curltừ localhost:
$ curl -I localhost:9000
HTTP/1.1 200 OK
Set-Cookie: PLAY_SESSION=935c3dbf94ba41f969fbf084de627bc695c2b326-csrfToken=fb0f50c61fa2e0512a83cb3fe07e6645c2ed8597-1497822065731-aebab6068130ca25cfdc16ec; Path=/; HTTPOnly
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'self'
X-Permitted-Cross-Domain-Policies: master-only
Content-Length: 439
Content-Type: text/html; charset=utf-8
Date: Sun, 18 Jun 2017 21:41:05 GMT
Hoặc trên trình duyệt:

Test thử HTTPS:
$ curl -Ikv https://localhost:9443/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate: localhost
> HEAD / HTTP/1.1
> Host: localhost:9443
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Set-Cookie: PLAY_SESSION=23aec23a5d9dabcf8568777821a8ecc4b45d2fd8-csrfToken=89bd94f48863a982efb861e500b5439bdfbca1dc-1497822198482-3fa6567fcee05ccb0084e73e; Path=/; HTTPOnly
Set-Cookie: PLAY_SESSION=23aec23a5d9dabcf8568777821a8ecc4b45d2fd8-csrfToken=89bd94f48863a982efb861e500b5439bdfbca1dc-1497822198482-3fa6567fcee05ccb0084e73e; Path=/; HTTPOnly
< X-Frame-Options: DENY
X-Frame-Options: DENY
< X-XSS-Protection: 1; mode=block
X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< Content-Security-Policy: default-src 'self'
Content-Security-Policy: default-src 'self'
< X-Permitted-Cross-Domain-Policies: master-only
X-Permitted-Cross-Domain-Policies: master-only
< Content-Length: 439
Content-Length: 439
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Date: Sun, 18 Jun 2017 21:43:18 GMT
Date: Sun, 18 Jun 2017 21:43:18 GMT
<
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
docker run --rm -it alpine:latest sh -c "apk --no-cache add openssl && echo newline | openssl s_client -connect 192.168.2.241:9443 -cipher ECDHE-RSA-AES256-GCM-SHA384 -curves secp521r1"
Và bạn sẽ thấy một dòng như:
Server Temp Key: ECDH, P-521, 521 bits
Server khác nhau (OpenJDK, HAProxy, v.v ...), Client (Chrome hoặc curl) và opensslkết hợp version mặc định cho các bộ và mật mã khác nhau. Kiểm tra một ít với Docker, như ubuntu:14.04hoặc centos:latest, ....!

Post a Comment

0 Comments