New to Self-Hosting and Struggling with Monica CRM Docker Setup (503 Error)

Hello Docker community,

As someone new to self-hosting, I embarked on what I thought would be a relatively simple project: setting up Monica CRM using Docker. However, I’ve encountered an unexpected challenge and am seeking assistance from those with more experience.

Background:

This is my first foray into self-hosting, and my knowledge of Docker and Nginx is quite basic. I am facing a “503 Service Temporarily Unavailable” error when trying to access Monica CRM through the browser.

Setup:

The setup is based on Monica CRM using Docker Compose, with the files taken from the example folder in the official repository. The services included are app (Monica), db (MariaDB), redis, cron, queue, web, proxy (Nginx), and a Let’s Encrypt companion container. Nginx is set up as a reverse proxy to handle web requests. Issue:

I am consistently getting a “503 Service Temporarily Unavailable” error, even though the services seem to be up and running. The Monica app service (app) is internally using port 9000, but there are no external ports exposed in the docker-compose.yml. Troubleshooting Steps Taken:

Confirmed the status of all services using docker-compose ps. Reviewed the Nginx configuration, ensuring the upstream directive is correctly set to server app:9000. Checked Docker networking to ensure the Monica app and Nginx proxy can communicate internally. Double-checked the .env file for correct environment variables. Restarted all services after making any configuration changes.

Here are key snippets of my docker-compose.yml and Nginx configuration for context:

services:   
app:
    build: ./app
    image: monica-app
    ...   proxy:
    build: ./proxy
    ports:
      - 80:80
      - 443:443
    ...

Nginx configuration:

    upstream php-handler {
    server app:9000; }

I anticipated a smooth start to self-hosting with Monica CRM, but I’ve found myself stuck quite early in the process. Any tips, insights, or advice would be hugely appreciated. Thanks in advance for your help!

Please, review your post and fix indentation and possibly acidental copy-pastes. I don’t udnerstand your configuration, but the shared codes doesn’t help much, especially when I see “Copy code” in an nginx config and a yaml without line breaks wher it should have.

You got an HTTP status code so the server responded. The container logs should show you more.

docker compose logs proxy

Also don’t try to manually configure your proxy. Use nginx-proxy if you prefer Nginx or use Traefik.

The ngonx proxy supports fastcgi backends too. That should work with PHP

https://github.com/nginx-proxy/nginx-proxy/tree/main/docs#fastcgi-backends

You can’t connect to PHP FPM as you would to an HTTP server. That is a different protocol.

Hi thanks for your answer and your insights. I am really new to docker and networking in general so I appreciate you taking the time to respond.

Sorry about the indentation and the incomplete snippets.

Here is my ENV file

APP_ENV=production
APP_DEBUG=false

# The encryption key. Must be 32 characters long exactly.
# Use `echo -n 'base64:'; openssl rand -base64 32` to generate a random key.
#not the real key
APP_KEY=12345678901234567890123456789012

# The URL of your application.
APP_URL=http://localhost

# Set trusted proxy IP addresses.
# To trust all proxies that connect directly to your server, use a "*".
APP_TRUSTED_PROXIES=

# Database information
DB_CONNECTION=mysql
DB_HOST=db
DB_DATABASE=monica
DB_USERNAME=monica
DB_PASSWORD=secret

# Mail credentials used to send emails from the application.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.domain.com
MAIL_PORT=587
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
# Outgoing emails will be sent with these identity
MAIL_FROM_ADDRESS=email@example.com
MAIL_FROM_NAME="Monica instance"

LOG_CHANNEL=stderr

CACHE_DRIVER=redis
SESSION_DRIVER=database
QUEUE_DRIVER=redis
REDIS_HOST=redis

Here is my docker-compose.yml

# Run Monica with nginx proxy
#
# You might want to set these variables in you .env file:
#
#- APP_ENV=production
#- APP_URL with your domain (https scheme)
#- APP_KEY with a random string
#
#- DB_HOST=db
# See `db` container for these values:
#- DB_DATABASE=monica
#- DB_USERNAME=monica
#- DB_PASSWORD=secret
#
# To use redis:
#- REDIS_HOST=redis
#- CACHE_DRIVER=redis
#- QUEUE_CONNECTION=redis
#

version: "3.9"

services:
  app:
    build: ./app
    image: monica-app
    env_file: .env
    volumes:
      - data:/var/www/html/storage
    restart: always
    depends_on:
      - db
      - redis

  db:
    image: mariadb:11
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=true
      - MYSQL_DATABASE=monica
      - MYSQL_USER=monica
      - MYSQL_PASSWORD=secret
    volumes:
      - mysql:/var/lib/mysql
    restart: always

  redis:
    image: redis:alpine
    restart: always

  cron:
    build: ./app
    image: monica-app
    command: cron.sh
    env_file: .env
    restart: always
    volumes:
      - data:/var/www/html/storage
    depends_on:
      - db
      - redis

  queue:
    build: ./app
    image: monica-app
    command: queue.sh
    env_file: .env
    restart: always
    volumes:
      - data:/var/www/html/storage
    depends_on:
      - db
      - redis

  web:
    build: ./web
    image: monica-web
    restart: always
    environment:
      - VIRTUAL_HOST=
      - LETSENCRYPT_HOST=
      - LETSENCRYPT_EMAIL=
    volumes:
      - data:/var/www/html/storage:ro
    depends_on:
      - app
    networks:
      - proxy-tier
      - default

  proxy:
    build: ./proxy
    restart: always
    ports:
      - 80:80
      - 443:443
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
    volumes:
      - certs:/etc/nginx/certs:ro
      - vhost.d:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - proxy-tier

  letsencrypt-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    restart: always
    volumes:
      - certs:/etc/nginx/certs
      - vhost.d:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - proxy-tier
    depends_on:
      - proxy

volumes:
  data:
  mysql:
  certs:
  vhost.d:
  html:

networks:
  proxy-tier:

Here are a few lines from the proxy log that are generated when I try to access localhost through the browser.

proxy-1  | e[0me[0;33;1mnginx.1     | e[0mlocalhost 172.19.0.1 - - [03/Jan/2024:00:00:58 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "172.19.0.4:80"
proxy-1  | e[0;33;1mnginx.1     | e[0mlocalhost 172.19.0.1 - - [03/Jan/2024:00:00:58 +0000] "GET /favicon.ico HTTP/1.1" 200 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" "172.19.0.4:80"

Hope this additional information can help with the resolution.

If you don’t add the app service to the proxy-tier network, the proxy will not be able to access it.

2 Likes