Blog

How to Set Up Multiple GravCMS Containers with Docker

First, create a Docker network. This enables container DNS, which allows containers to communicate with one another by name.

$ docker network create mynetwork

Start the Nginx proxy

$ docker run --name nginx-proxy --net mynetwork -p 80:80 -p 443:443 -v ~/certs:/etc/nginx/certs -v /etc/nginx/vhost.d -v /usr/share/nginx/html -v /var/run/docker.sock:/tmp/docker.sock:ro --label com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy -d --restart always jwilder/nginx-proxy

Start the Let’s Encrypt Nginx Proxy Companion.

$ docker run --name letsencrypt-nginx-proxy-companion --net mynetwork -v ~/certs:/etc/nginx/certs:rw -v /var/run/docker.sock:/var/run/docker.sock:ro --volumes-from nginx-proxy -d --restart always jrcs/letsencrypt-nginx-proxy-companion

Creating Directories for Data Storage

Before running our GravCMS instances, we need to prepare dedicated directories where each instance can store its data.

$ sudo mkdir -p /docker/data/grav{1,2,3,4,5}

For the Docker containers to function correctly, they need to have the appropriate permissions to access and modify data in these directories.

$ sudo chown -R 1000:1000 /docker/data/grav{1,2,3,4,5}

With our directories in place and correctly permissioned, we can now start spinning up some GravCMS Docker containers.

$ docker run -d \
    --name=grav1 \
    --network=mynetwork \
    -e PUID=1000 \
    -e PGID=1000 \
    -e TZ=Etc/UTC \
    -e VIRTUAL_HOST=sub1.mydomain.com \
    -e LETSENCRYPT_HOST=sub1.mydomain.com \
    -e LETSENCRYPT_EMAIL=your_email@example.com \
    -v /docker/data/grav1:/config \
    --restart unless-stopped \
    lscr.io/linuxserver/grav:latest

VIRTUAL_HOST, LETSENCRYPT_HOST, LETSENCRYPT_EMAIL: These environment variables instruct the nginx-proxy to route traffic for sub1.mydomain.com to our container and request an SSL certificate for that domain using the provided email from Let's Encrypt.

-v /docker/data/grav1:/config allows persistent storage and shared access to data between the host and the container.

You might have noticed that we didn't specify any port mapping (e.g., -p 8080:80). There is no need to bind our GravCMS containers to port 80 on the host. This is unnecessary when using a reverse proxy, which will handle the actual port 80 and 443 traffic and forward it to the correct container.

Want more insights?

Sign up for my newsletter.

I care about your data. Read my privacy policy.