top of page
Search

Deploy app with Docker and SystemD in a few clicks

Pretty often developers facing situation, when you have your code locally and have to deploy it somehow on server. Sometimes you even have no pipeline ¯\_(ツ)_/¯.

Lets deploy our app with DevOps Pass, Docker and SystemD.


Video how to do it with DOP:



Docker`ize you app

Lets take some random Java Spring Boot app and Dockerize it.


You can generate Dockerfile for Java Spring Boot (you can generate it in DOP):

FROM eclipse-temurin:17-jdk-focal as build

WORKDIR /build

COPY .mvn/ ./.mvn
COPY mvnw pom.xml  ./
RUN ./mvnw dependency:go-offline

COPY . .
RUN ./mvnw package -DskipTests

FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
COPY --from=build /build/target/*.jar run.jar
ENTRYPOINT ["java", "-jar", "/app/run.jar"]

Build docker image with some tag:

# Build in app folder
docker build -t my-app:1.0.0 .

# Push to your registry or DockerHub https://hub.docker.com/signup
docker push my-app:1.0.0

Run application with SystemD

Now time to deploy your app, generate SystemD service file in DOP or use one below:

# app_springboot.service 
[Unit]
Description=Sample springboot app
DefaultDependencies=no
After=network.target

[Service]
Type=simple
User=root
Group=root

Environment=PATH=$PATH:$HOME/.local/bin:$HOME/bin

WorkingDirectory=/tmp

ExecStart=/usr/bin/docker run --rm --name %n my-app:1.0.0

# Command to run before and after start, add "-" if command failures should be ignored, like "ExecStartPre=-docker pull nginx:latest"
# More details - https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html
# ExecStartPre=
# ExecStartPost=

ExecStop=/usr/bin/docker stop --time 10 %n

# Command to run before and after stop, add "-" if command failures should be ignored, like "ExecStopPost=-rm -Rf /var/log/app_logs/*"
# More details - https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html
# ExecStopPre=
# ExecStopPost=

# Reload command
# More details - https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html
# ExecReload=kill -HUP $MAINPID

TimeoutStartSec=0
RemainAfterExit=yes
Restart=always

[Install]
WantedBy=default.target

  • copy your service to "cp app_springboot.service /etc/systemd/system/"

  • reload systemd "systemctl daemon-reload"

  • enable service "systemctl enable app_springboot.service"

  • start your app "systemctl start app_springboot.service"

  • check your app status "systemctl status app_springboot.service"


Enjoy!

61 views0 comments

Kommentare


bottom of page