2

I am newbie to the scala world, I try to run this project to understand the Scala Rest Play workflow : https://developer.lightbend.com/guides/play-rest-api/index.html

I am able to run this project successfully using the sbt run command

/scala/play-scala-rest-api-example$ sbt run 
[info] Loading settings for project play-scala-rest-api-example-build from plugins.sbt ...
[info] Loading project definition from /home/scala/play-scala-rest-api-example/project
[info] Loading settings for project root from build.sbt ...
[info] Loading settings for project docs from build.sbt ...
[info] Set current project to play-scala-rest-api-example (in build file:/home/dominic/scala/play-scala-rest-api-example/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Enter to stop and go back to the console...)

I try to put this project inside docker

FROM ubuntu:latest
MAINTAINER group
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y  software-properties-common && \
    add-apt-repository ppa:webupd8team/java -y && \
    apt-get update && \
    echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections && \
    apt-get install -y oracle-java8-installer && \
    apt-get clean
RUN echo "deb https://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
RUN apt-get update
RUN apt-get install -y sbt=1.2.8
COPY ./ ./
WORKDIR ./play-scala-rest-api-example
CMD ["sbt","run"]

It is build successfully as docker image

But when I run this docker image, It is opening the port : 9000 (as we run without docker) and immediately the port is closing like below

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0.0.0.0:9000

(Server started, use Enter to stop and go back to the console...)

[info] p.c.s.AkkaHttpServer - Stopping server...

[success] Total time: 614 s, completed Feb 5, 2019 5:11:56 AM
[INFO] [02/05/2019 05:11:56.196] [Thread-2] [CoordinatedShutdown(akka://sbt-web)] Starting coordinated shutdown from JVM shutdown hook

My query is why it is shutting down when I run in docker? How to run this forever?

8
  • Can you reproduce this here labs.play-with-docker.com
    – Akash
    Commented Feb 5, 2019 at 5:38
  • What is the exact command you use to run your image? Are you running the container interactively (docker run -it -p 9000:9000 <your other options> <your-image>) ?
    – francoisr
    Commented Feb 5, 2019 at 7:12
  • @francoisr no I am just running docker run playtestimage
    – Harry
    Commented Feb 5, 2019 at 7:13
  • 1
    You need to use the -it option, otherwise your standard input will not be connected to your container, and then the server will immediately stop after it has started.
    – francoisr
    Commented Feb 5, 2019 at 7:16
  • 1
    I'm going to write a more complete answer.
    – francoisr
    Commented Feb 5, 2019 at 7:47

1 Answer 1

3

You are running your container without the -it options (which allow you to connect to its standard input like you're in a terminal), yet your program expects input when it starts ("press enter..."). Your program probably waits for input on stdin and probably reads an EOF (end of file) when it starts, causing it to terminate, which in turns terminates your container.

If you want to run your container in the background, it seems to me that you have two options:

1) Run your container using docker run -it -p 9000:9000 <your_other_options> <your_image> and then put it to the background using CTRL+P then CTRL+Q. You will see your container is still running in docker ps. To reattach to it, you can simply use docker attach <your_container>. Of course, this approach will not be applicable if you want to run your container on, say, a unit-test server where you won't want to manually do the CTRL+P/Q thing.

2) Modify your server so that it can be run completely in the background, without user input. In this case, the way to terminate your program will be to send it a SIGINT signal. This is what CTRL+C usually does, and also what docker stop <your_container> will do for you. You will probably want to properly handle this signal in your Scala code, so that you can perform some clean up instead of abruptly crashing. This can be done using a shutdown hook. Shutdown hooks come from the JVM and are not Scala specific. You should take care of manually stoping any thread / subprocess within your shutdown hook.

Second method is best IMO, but is also more involved and probably overkill if the first method works for you.

1
  • @user:1602853 If this answer solved your problem, please mark it as the answer so that this question appears as solved on the platform.
    – francoisr
    Commented Mar 1, 2019 at 10:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.