I am runing a spring-boot app as a backend application within a docker container. I want to be able to exec in a container and start a shellMethod to execute some admin stuff. Is it possible start and stop a shell session within a container at runtime?
Asked
Active
Viewed 181 times
1 Answers
0
Spring Shell provides the interactive shell, and the non-interactive mode.
Assuming Spring Boot 3.0.0 and Spring Shell 3.0.0-M3 or newer:
If you have a command like this:
@ShellComponent
public class Cli {
@ShellMethod("Hello world")
public String hello(@ShellOption(defaultValue = "world") String arg1){
return "Hello " + arg1 + "!!";
}
}
and you create demo:0.0.1-SNAPSHOT
using the spring boot maven plugin, for example:
./mvnw -Pnative clean spring-boot:build-image -DskipTests
You can execute commands directly against that docker image, like so:
$ docker run -it demo:0.0.1-SNAPSHOT hello FishingIsLife
To get:
Hello FishingIsLife
So you don't need to start/stop a shell session, just use the non-interactive mode of Spring Shell.

DaShaun
- 3,722
- 2
- 27
- 29
-
Cool, but is also possible form a running system? Like you connect to another host. docker ps shows you the running container. You exec in the container or execute a command directly to e.g. start a data export – FishingIsLife Dec 21 '22 at 12:00
-
"docker attach" allows you to connect to a running container, this is what I think you are trying to get to. Additionally, I encourage you to take a look at Spring Cloud Task. I'm happy to help further if you are interested in sharing more details. – DaShaun Dec 21 '22 at 21:04