Linux ‐ Docker And Services - erikpa1/MyExperience GitHub Wiki

TODO:

  1. install docker - mac
  2. create docker image - golang server
  3. install docker on ubuntu
  4. pull image from cmd
  5. install image from ZIP
  6. Try docker on Raspberi PI

Install docker

sudo apt install docker.io
docker run -d -p 27017:27017 --name mongo mongo:8
docker update --restart unless-stopped mongo
docker start mongo

Minimal docker run example

Node JS express

sudo docker run --rm -p 3000:3000 node:22-alpine node -e "require('http').createServer((req,res)=>res.end('Hello World\n')).listen(3000)"

Install app service

Edit service file

sudo nano /etc/systemd/system/<SERVICE_NAME>.service
sudo micro /etc/systemd/system/<SERVICE_NAME>.service

[Unit]
Description=My Go application
After=network.target

[Service]
Type=simple
User=erik
WorkingDirectory=/home/erik/myapp
ExecStart=/home/erik/myapp/main
Restart=on-failure
RestartSec=5

# Environment variables — option A: inline
Environment="PORT=8080"
Environment="MONGO_URI=mongodb://localhost:27017"

# Option B: load from a file
EnvironmentFile=/home/erik/myapp/.env

[Install]
WantedBy=multi-user.target

Reset and start service

sudo systemctl daemon-reload
sudo systemctl enable --now <SERVICE_NAME>

Usefull commands

Status and restart

systemctl status <SERVICE_NAME>
journalctl -u <SERVICE_NAME> -f      # follow logs (stdout/stderr go here)

sudo systemctl daemon-reload
sudo systemctl restart <SERVICE_NAME>

journalctl -u <APPNAME> -n 200 --no-pager      # last 200 lines

Check service config

systemctl show myservice -p Environment
systemctl show myservice -p EnvironmentFiles
# or see the full unit definition:
systemctl cat myservice


Print env variable

echo "$MY_VAR"
printenv MY_VAR

Unzip

sudo apt get unzip
unzip "<FILE>.zip"

Executables

Problem with running executable

  1. Check executable type
file ./yourfile
  1. if error sudo: ./goproxy: command not found or permission denied
chmod +x ./<EXE_NAME>
sudo ./<EXE_NAME>

#This is for running 443
sudo setcap 'cap_net_bind_service=+ep' /path/to/your-binary
  1. Unzip permission

When you copy executable with ssh you have to add it separate permissions. But when unziping executable executable receives permissions of user.

⚠️ **GitHub.com Fallback** ⚠️