Hosting with Docker

I’m having trouble hosting FarmOS in a docker container. The examples I can find use a volume for the sites directory

    volumes:
      - './sites:/opt/drupal/web/sites'

If I do that I get an error on installation, because my sites directory is empty.

Should I be pre-populating the sites directory before starting the container, or is there a different workaround that I’m missing?

1 Like

@gmagnusson Are you using the official farmOS image? And if so, are you using 1.x or 2.x? And is it the production image or the development image?

We have a special docker-entrypoint.sh script that helps to populate the sites directory for you the first time. This works a bit differently in 1.x vs 2.x and prod vs dev images. Happy to point you to the relevant code to help you understand.

If you are overriding the image (or especially the entrypoint script), then it might be breaking this logic.

Here is the 2.x entrypoint script: farmOS/docker-entrypoint.sh at 2.x · farmOS/farmOS · GitHub

Notice this line:

# If the sites directory is empty, populate it from pre-built files.
if [ -d /opt/drupal/web/sites ] && ! [ "$(ls -A /opt/drupal/web/sites/)" ]; then
  echo "farmOS sites directory not detected. Copying from pre-built files in the Docker image."
  cp -rp /var/farmOS/web/sites/. /opt/drupal/web/sites
fi
1 Like

This is just helper logic to fix the exact issue you’re describing. Alternatively, if you don’t want to use this logic, you can simply install farmOS using a standard Drupal process, which would be to manually copy default.settings.php to settings.php and make it writable by www-data.

Thank you, that clarifies some things.

Here is the full docker-compose.yml

version: '3'
services:
  db:
    image: mariadb:latest
    volumes:
      - './db:/var/lib/mysql'
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: farm
      MYSQL_DATABASE: farm
      MYSQL_USER: farm
      MYSQL_PASSWORD: farm

  www:
    depends_on:
      - db
    image: farmos/farmos:2.x
    volumes:
      - './sites:/opt/drupal/web/sites'
    ports:
      - '80:80'
    environment:
      XDEBUG_CONFIG: remote_host=172.17.0.1

I can see that sites is populated with some content, but /sites/default/settings.php is not in there

After I run docker-compose up this is what I find in sites:

sites/
├── README.txt
├── default
│   ├── default.services.yml
│   ├── default.settings.php
│   └── files
├── development.services.yml
├── example.settings.local.php
└── example.sites.php
1 Like

Ah OK then it may be a permissions issue… During installation, Drupal will attempt to copy default.settings.php to settings.php itself and then write database details etc to it. But if that directory is not writeable by www-data then it won’t be able to. This would not only affect initial installation, but also uploading files later when you’re using farmOS.

Can you check whether or not that directory is writable by www-data?

Oh wait I’m also noticing some inconsistencies in your docker-compose.yml (unrelated to this issue perhaps, but worth asking about)… it looks like you’ve got a mix of farmOS 1.x vs 2.x and development vs production templates…

PostgreSQL is the recommended database for farmOS 2.x (MariaDB will also work, but I assume you go that from the 1.x docker-compose.development.yml template).

Also, I see XDEBUG_CONFIG environment variable being used. This will only have an affect if you are running the 1.x-dev or 2.x-dev images, so it’s harmless… but it also looks like you copied that from the 1.x template. Those variables are different in 2.x.

1.x templates

Development: farmOS/docker-compose.development.yml at 7.x-1.x · farmOS/farmOS · GitHub
Production: farmOS/docker-compose.production.yml at 7.x-1.x · farmOS/farmOS · GitHub

2.x templates

Development: farmOS/docker-compose.development.yml at 2.x · farmOS/farmOS · GitHub
Production: farmOS/docker-compose.production.yml at 2.x · farmOS/farmOS · GitHub

1 Like

@gmagnusson Did you figure this out? Was it a permissions issue?

What operating system are you running Docker on?

@raheel seems to be having a similar (but different) issue, which may also be permissions related: Apache/2.4.38 (Debian) Server at localhost Port 80

I’m wondering if maybe both of you are running on Windows? I’m not very familiar with how Linux permissions (which is what is expected within the Docker container) are treated by a Windows host system.

I encountered this issue when i used the production.yml file and when i used the development.yml i encounter the other issue i have opened up after the modules started to get installed.

@mstenta I did get past this issue but the solution is unconfirmed.

I reverted back to the recommended docker-compose.development.yml for 2.x. I still saw this error on multiple attempts to install and fiddle with permissions. Eventually I got past it simply by refreshing the page. Seems like settings.php was not copied until I rendered the install page a second time.

I was running this on MacOS.
The other error I mentioned here The website encountered an unexpected error. Please try again later - #2 by gmagnusson was running on AWS Fargate

1 Like

Did you faced this issue?


i am trying to connect to a postgres DB i made on my local machine i am trying to connect to that but its giving me that error, if you faced it how did you overcame it? Do i have to enable the local host and port in firewall, or through some settings in postgresql?

1 Like

I saw something similar. I was launching both FarmOS and Postgres from docker-compose. Initially I set the DB host as localhost and got this error. When I set the host to db which is the service name in docker-compose it worked fine.

If you’re running Postgres outside of docker, maybe there is some trick required to make sure the docker network has access to it. I don’t know exactly what steps you might need.

1 Like

@raheel See my answer here: Postgres DB configuration issue - #5 by mstenta