Skip to content
← All posts

Port 3000 was already taken — fixing a stuck GitLab CI deploy

· 1 min read

A deploy that had worked for weeks suddenly started failing. The pipeline built the app fine, then died when it tried to restart it: port 3000 was already in use.

What was going on

The pipeline runs as the gitlab-runner user and manages the app with pm2. But at some point someone (me) had started the same app by hand as root, with root's own pm2. So there were two pm2s on the server:

  • root's pm2, quietly holding port 3000
  • gitlab-runner's pm2, trying to start a fresh copy and failing

Neither could see the other's processes, so pm2 list as one user looked perfectly normal.

The fix

  1. Find what's holding the port: sudo ss -ltnp | grep 3000
  2. Stop and remove root's copy: sudo pm2 delete all && sudo pm2 kill
  3. Re-run the pipeline so gitlab-runner's pm2 owns the app.
  4. Make gitlab-runner's pm2 start on boot, so nobody is tempted to start it as root again:
sudo env PATH=$PATH pm2 startup systemd -u gitlab-runner --hp /home/gitlab-runner
sudo -u gitlab-runner pm2 save

I also added a small alias so checking the app always uses the right user:

alias apppm2='sudo -u gitlab-runner PM2_HOME=/home/gitlab-runner/.pm2 pm2'

What I took from it

Production processes should belong to exactly one user, and it shouldn't be root. If there's an easy way to do the wrong thing, like running pm2 as root "just once", sooner or later it will happen. So make the right way the easy way.