January 7, 2020 · Computers

Installing Nextcloud in Docker with Nginx on the host OS

Recently I switched from web hosting with NixiHost to a VPS with DigitalOcean.  I wanted to get some hands on experience using docker and and move forward with some projects I have been planning. I have relied on Nextcloud for cloud storage instead of Dropbox or Google Drive, and along with switching my hosting provider I had set up Nextcloud again. Previously I had installed Nextcloud though Softalicious on cPanel, but I no longer have cPanel or Softalicious.

I dove in head first, did very little research, and managed to cobble together something functional.

My current configuration has nginx running outside of docker on the host OS, and each virtual host points to a port that each docker container exposes as illustrated below.

I set up nginx and installed docker, then followed this guide by Alex Ellis (if you're reading this, thanks!) with some tweaking to get ghost installed. With those instructions I had a foundation to build off of. The nextcloud installation was a little more tricky than the ghost install, however, because it used docker-compose.

I used the guide from Docker Hub as a jumping off point. I made 2 changes to the docker-compose.yaml: adding a container name to each container using container_name and exposing nextcloud on a random port that could be reached by nginx.

version: '2'

volumes:
  nextcloud:
  db:

services:
  db:
    container_name: nextcloud_db
    image: mariadb
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    restart: always
    volumes:
      - db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    container_name: nextcloud
    image: nextcloud
    ports:
      #port 80 in the container is exposed to Host OS on port 25043
      - 127.0.0.1:25043:80
    links:
      - db
    volumes:
      - nextcloud:/var/www/html
    restart: always

After I had docker-compose.yaml finished I ran docker-compose up -d

Once nextcloud was running in docker I went on to configure nginx in the same fashion as my ghost install. Here is the content of the file in /etc/nginx/sites-available/

server {

       

        server_name nextcloud.drinking.beer;

        location / {
             proxy_pass  http://127.0.0.1:25043;
             proxy_set_header    X-Real-IP $remote_addr;
             proxy_set_header    Host      $http_host;
             proxy_set_header X-Forwarded-Proto https;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             #try_files $uri $uri/ =404;
         }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/drinking.beer/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/drinking.beer/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = nextcloud.drinking.beer) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;

        server_name nextcloud.drinking.beer;
    return 404; # managed by Certbot


}

After symlinking the file in sites-available to sites-enabled I restarted nginx and visited https://nextcloud.drinking.beer and was greeted by the login page, ready for the initial configuration.

It didn't work.

Two things were wrong. First, I was getting an error that Nextcloud was unable to connect to the mysql database. When installing Nextcloud via Docker your database address isn't localhost, it is db.

When installing Nextcloud via Docker your database address isn't localhost, it is db.

I was hung up on that for way too long. Maybe it's obvious to someone that knows docker better than I do, but it took me far longer than I would like to admit to come across that bit of information on a forum post. Hopefully this spares you some trouble

After getting the connection to the database figured out I was hanging up on login pages. I was able to enter my log in information and once I reached the loading screen I could refresh the page and I was in, but that is a hacky workaround I couldn't live with. This also made it impossible to connect the desktop client.

To remedy this I had to open a shell in the docker container and edit Nextcloud's config.php to add these lines:

  'overwrite.cli.url' => 'https://nextcloud.drinking.beer',
  'overwritehost' => 'nextcloud.drinking.beer',
  'overwriteprotocol' => 'https',

I had to install a text editor in that docker container. I am almost completely sure that is not the correct way to edit files in a container, but desperate times call for desperate measures.

After I added those lines and restarted the docker container, I was able to log in with no issues. I synced all of the files I had backed up locally and have had no issues so far.

I like my setup with nginx running on the host OS instead of in a docker container. It seems much less fussy to me.

To the Docker gurus: Is there an advantage to running nginx inside of a docker container? Where did I go wrong on this installation?