dockerでgradleを使用したSpringBootによるWebAPIを実行したい
端末情報
OS:macOS Mojave
バージョン:10.14.3
端末:MacBook Pro (13-inch,2017,Four Thunderbolt 3 Ports)
プロセッサ:3.1 GHz Intel Core i5
メモリ:8 GB 2133 MHz LPDDR3
内容一覧
dockerfile作成
dockerビルド
docker実行
API実行
内容詳細
dockerfile作成
$ vi Dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
RUN mkdir /work
COPY . /work
WORKDIR /work
# RUN /work/gradlew build
RUN mv /work/build/libs/*.jar /work/app.jar
RUN ifconfig
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/work/app.jar"]
dockerビルド
$ docker build --no-cache -t springboot .
Sending build context to Docker daemon 43.11MB
Step 1/8 : FROM openjdk:8-jdk-alpine
---> 3675b9f543c5
Step 2/8 : VOLUME /tmp
---> Running in ec2e9267b1cb
---> 47446101c8cd
Removing intermediate container ec2e9267b1cb
Step 3/8 : RUN mkdir /work
---> Running in b4a522be9e8a
---> 9d219ab10698
Removing intermediate container b4a522be9e8a
Step 4/8 : COPY . /work
---> a840881d3b77
Step 5/8 : WORKDIR /work
---> 85e0c801f747
Removing intermediate container 2bf4e8147d75
Step 6/8 : RUN mv /work/build/libs/*.jar /work/app.jar
---> Running in 941ec78a41a5
---> 0485a98a2616
Removing intermediate container 941ec78a41a5
Step 7/8 : RUN ifconfig
---> Running in a706b468723a
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:270 (270.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
---> 2a44531d8388
Removing intermediate container a706b468723a
Step 8/8 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /work/app.jar
---> Running in 98fb5bc7dcd3
---> 58642affe690
Removing intermediate container 98fb5bc7dcd3
Successfully built 58642affe690
Successfully tagged springboot:latest
docker実行
$ docker run -p 8081:8080 springboot
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.1.RELEASE)
INFO SampleApplication - Starting SampleApplication on c82daa00868f with PID 1 (/work/app.jar started by root in /work)
INFO SampleApplication - No active profile set, falling back to default profiles: default
INFO TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
INFO Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
INFO StandardService - Starting service [Tomcat]
INFO StandardEngine - Starting Servlet Engine: Apache Tomcat/9.0.13
INFO AprLifecycleListener - The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-1.8-openjdk/jre/lib/amd64:/usr/lib/jvm/java-1.8-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
INFO [/] - Initializing Spring embedded WebApplicationContext
INFO ContextLoader - Root WebApplicationContext: initialization completed in 3562 ms
INFO ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
INFO Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
INFO NioSelectorPool - Using a shared selector for servlet write/read
INFO TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''
INFO SampleApplication - Started SampleApplication in 6.089 seconds (JVM running for 7.795)
INFO [/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO DispatcherServlet - Initializing Servlet 'dispatcherServlet'
INFO DispatcherServlet - Completed initialization in 46 ms
API実行
(別ターミナルにて実行)
$ curl localhost:8081/hello/world
{"value":"Hello sample"}$
$
==> なんとか実行確認まで進められました