Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Springboot not running

I am new to docker, I have pre cooked a docker image(updated and installed Java and other dependancies) and stored it on my GitHub repo,
I have stored a simple hello world Spring Boot application on an AWS S3 bucket,

I want to my DockerFile -
1. Get the docker image from my GitHub repo
2. Do an update patch
3. Set my working to directory to /home/ubuntu
4. Download application from S3 bucket using wget(it's publicly accessible)
5. run the application inside the container

After which I will run the image.

Command to build -

docker build -t someTag .

Command to run -

docker run -p 9090:8090 someTag

My java application jar that will be downloded is - docker.jar
And the application runs on port 8080

I have the following Dockerfile -

FROM someRepoHere

WORKDIR /home/ubuntu

RUN apt-get update

RUN cd /home/ubuntu

VOLUME /home/ubuntu

RUN wget S3BucketLocationHere

#RUN nohup java -jar docker.jar &

# Expose the default port
EXPOSE 8080

#Old command - CMD nohup java -jar docker.jar &
CMD ["java", "-jar", "docker.jar"]

The DockerFile is able to successfully build the image but,
My application is unreachable, It did not run inside the container.
Locally if I wget my application and run the nohup command, the application responds successfully.

like image 645
Ani Avatar asked Nov 23 '25 19:11

Ani


1 Answers

Your command being run is what controls the existence of the container, when it exits/returns, the container exits and stops. Therefore you need to run your command in the foreground. When you are in an interactive shell in a container, that command is your shell. The command you've listed uses a shell, but that shell exits when it runs out of commands to process and nothing is running in the foreground:

CMD nohup java -jar docker.jar &

The string syntax will run the command with /bin/sh -c "nohup java ...".

A better option is to run with json syntax if you don't need a shell, and run your java app in the foreground, avoid the nohup and background syntax:

CMD ["java", "-jar", "docker.jar"]

A few more comments on the provided Dockerfile:

WORKDIR /home/ubuntu

RUN apt-get update

That only creates a cache inside your container that will become stale and result in cache misses if you try to use it in the future. This doesn't upgrade any packages if that's what you intended. That line should be removed.

RUN cd /home/ubuntu

This makes no filesystem changes, and will have no impact on the resulting image. The current shell state is lost after the RUN line exits, including the current directory and any variables you set. This line should be removed.

VOLUME /home/ubuntu

From this line forward, changes to /home/ubuntu will be lost. You'll only see anonymous volumes created as a result unless you specify a volume at runtime at the same location. You likely don't want the above volume line because it will break things like the next line.

RUN wget S3BucketLocationHere

This line has been obfuscated but I suspect you are outputting in /home/ubuntu because of the value of WORKDIR. Anything created here will be lost because of the VOLUME line above.

like image 188
BMitch Avatar answered Nov 26 '25 09:11

BMitch