JupyterLab permissions in Docker Compose

I have a server setup in my home network that I prefer to run Docker Compose files on to start JupyterLab. I had an instance running that was working perfectly for the past couple of years, but something isn’t working correctly when making the switch to Docker Stacks and quay.io.

My docker-compose.yml file looks like this:

version: '3'
services:
  jupyterlab:
    container_name: jupyter-datascience
    image: quay.io/jupyter/datascience-notebook:latest
    restart: always
    volumes:
      - ./notebooks:/home/jovyan/work
    environment:
      - JUPYTER_TOKEN=token
    ports:
      - '8888:8888'

And when I run this simple compose file on my server I’m getting the following error, whenever I try to save a file into the “work” directory:

Permission denied: work/Untitled.ipynb

I know this has to do with directory permissions, but am unsure how to set those in a Docker Compose file to get this to work correctly.

Answering my own question here, but I had to change how permissions are handled in the Docker Compose file, rather than the command line. I wanted to document this here, because I couldn’t find this solution explicitly handled elsewhere.

You need to run the container as root, and specifically handle the user names and groups in the Compose file.

This is the solution that is working well for me:

version: '3'
services:
  jupyterlab:
    container_name: jupyter-datascience
    user: root
    image: quay.io/jupyter/datascience-notebook:latest
    restart: always
    volumes:
      - ./notebooks:/home/jupyternb
    environment:
      - JUPYTER_TOKEN=token
      - NB_USER=jupyternb
      - NB_UID=1000
      - NB_GID=100
      - CHOWN_HOME=yes
    working_dir: /home/jupyternb
    ports:
      - '8888:8888'