Dockerfile run screen command while keeping it attached

Viewed 24

I have a Dockerfile that I would like to run a script as its entrypoint. The script contains a single command with a shebang:

#!/bin/sh
screen -S mc bash -c "java -jar paper-1.18.2-387.jar"

My dockerfile looks as follows:

FROM openjdk:18-alpine
RUN apk --update add screen
WORKDIR /
COPY . ./

RUN apk add gcompat
ENV LD_PRELOAD=/lib/libgcompat.so.0

COPY run.sh /run.sh

ENTRYPOINT ["/bin/sh", "-c", "/run.sh"]

EXPOSE 25565

However, when I run the docker file the screen session that it should have attached is never executed. I have tried using the /bin/bash tag, but it tells me that it doesn't exist. I need this screen to be attached so that the docker image continues to run, and I am using screen so that I can have another session attach elsewhere.

How can I run this single command as my dockerfile entry point?

I am using WSL2 with Ubuntu 20.04.

Thanks!

1 Answers

I have solved this by modifying my script to look like so:

#!/bin/sh
screen -S myScreen sh -c "sh start.sh"

And adding a second script named start.sh that looks like

#!/bin/sh
java -jar paper-1.18.2-387.jar

And modifying my Dockerfile to look like:

FROM openjdk:18-alpine
RUN apk --update add screen
WORKDIR /
COPY . ./

RUN apk add gcompat
ENV LD_PRELOAD=/lib/libgcompat.so.0

COPY create-screen.sh /create-screen.sh
COPY start.sh /start.sh

CMD ["/create-screen.sh"]

EXPOSE 25565

Reconnecting to the screen involves running screen -x on the shell of the image. Since it is already attached by the CMD process, the window may look comically tiny because the headless docker process created it with a weird default size. This is an easy fix, you can look it up.

Related