DEV Community

DevOps Pass AI for DevOps Pass AI

Posted on • Edited on

1

Deploy app with Docker and SystemD in a few clicks

DevOps Pass AI

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.

You can do all following steps without even using console via DevOps Pass AI tool, which making DevOps simple as it should be (check video).

Docker'ize you app

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

For example - https://github.com/SimpleProgramming/simple-springboot-app

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

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"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run application with SystemD

Now time to deploy your app, generate SystemD service file in DevOps Pass 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
Enter fullscreen mode Exit fullscreen mode

Now you can deploy and start your app on server:

# copy your service
scp app_springboot.service user@host:/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

# Check app logs
journalctl -u app_springboot.service
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more