Permissions problem to remove previous build files and/or writing new ones

Hello, I'm facing an issue which I don't seem to find any answers.

I'm using a private runner on a CentOS 7 VPS with docker engine and docker-compose already installed. When running as root, all stages pass, but the next time I commit and push something and the CI is triggered, the build fails because it cannot delete previous build files. If I run as a simple user, the build stage passes ok and the testing one fails because it cannot create the .tox folder. What puzzles me is that both cases work fine on my workstation (Fedora 25).

Here's my .gitlab-ci.yml:

stages:
  - build
  - test

variables:
  DOCKER_DRIVER: overlay
  DJANGO_TEST_IMAGE: registry.gitlab.com/grupoirona/turnateme:djangodev-$CI_BUILD_REF_NAME

before_script:
  - docker login -u mbaragiola -p $CI_BUILD_TOKEN registry.gitlab.com

build-images:
  stage: build
  script:
    - docker-compose -f dev.yml build
  tags:
    - linux
    - docker
    - docker-compose

test-images:
  stage: test
  script:
    - docker-compose -f dev.yml run django tox
  tags:
    - linux
    - docker
    - docker-compose
  • Case 1: Running as root inside the Django docker image.
FROM python:3.5

ENV PYTHONUNBUFFERED 1

# Requirements have to be pulled and installed here, otherwise caching won't work
COPY ./requirements /requirements
RUN pip install -r /requirements/development.pip


COPY ./compose/django-dev/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./compose/django-dev/start-dev.sh /start-dev.sh
RUN sed -i 's/\r//' /start-dev.sh
RUN chmod +x /start-dev.sh

WORKDIR /app

ENTRYPOINT ["/entrypoint.sh"]
  • Case 2: Running as user 'django' inside the Django docker image.
FROM python:3.5

ENV PYTHONUNBUFFERED 1

# Requirements have to be pulled and installed here, otherwise caching won't work
COPY ./requirements /requirements
RUN pip install -r /requirements/development.pip


COPY ./compose/django-dev/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./compose/django-dev/start-dev.sh /start-dev.sh
RUN sed -i 's/\r//' /start-dev.sh
RUN chmod +x /start-dev.sh

RUN useradd django
WORKDIR /app
RUN chown -R django:django .
RUN chmod +rwx .
USER django

ENTRYPOINT ["/entrypoint.sh"]

And here's my config.toml:

concurrent = 1
check_interval = 0

[[runners]]
  name = "centos-512mb-nyc1-01"
  url = "https://gitlab.com/ci"
  token = "tokengoeshere"
  executor = "shell"
  [runners.cache]
    Insecure = false

I've tried using docker-in-docker approach, but Alpine Linux turned out to be inconvenient when docker-compose and also installing Python3 and Django dependencies that needed GCC and many other libraries. Since this VPS is private and so far only used for this project, running on shell seems alright.

Here's the error output on case 1 (running as root after an already successful pipeline):

Running with gitlab-ci-multi-runner 1.11.1 (a67a225)
  on centos-512mb-nyc1-01 (fcdcff7a)
Using Shell executor...
Running on centos-512mb-nyc1-01...
rm: cannot remove ‘/home/gitlab-runner/builds/fcdcff7a/0/grupoirona/turnateme/turnateme/__pycache__/__init__.cpython-35.pyc’: Permission denied
rm: cannot remove ‘/home/gitlab-runner/builds/fcdcff7a/0/grupoirona/turnateme/turnateme/__pycache__/urls.cpython-35.pyc’: Permission denied
rm: cannot remove ‘/home/gitlab-runner/builds/fcdcff7a/0/grupoirona/turnateme/turnateme/__pycache__/api_urls.cpython-35.pyc’: Permission denied
rm: cannot remove ‘/home/gitlab-runner/builds/fcdcff7a/0/grupoirona/turnateme/turnateme/settings/__pycache__/__init__.cpython-35.pyc’: Permission denied
....
....
....
goes on and on for every single file

Here's the error output on case 2 (running as user):

Running with gitlab-ci-multi-runner 1.11.1 (a67a225)
  on centos-512mb-nyc1-01 (fcdcff7a)
Using Shell executor...
Running on centos-512mb-nyc1-01...
Cloning repository...
Cloning into '/home/gitlab-runner/builds/fcdcff7a/0/grupoirona/turnateme'...
Checking out 6e4cada5 as master...
Skipping Git submodules setup
$ docker login -u mbaragiola -p $CI_BUILD_TOKEN registry.gitlab.com
Login Succeeded
$ docker-compose -f dev.yml run django tox
Postgres is up - continuing...
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/py/_error.py", line 65, in checked_call
    return func(*args, **kwargs)
PermissionError: [Errno 13] Permission denied: '/app/.tox'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/tox", line 11, in <module>
    sys.exit(cmdline())
  File "/usr/local/lib/python3.5/site-packages/tox/session.py", line 39, in main
    retcode = Session(config).runcommand()
  File "/usr/local/lib/python3.5/site-packages/tox/session.py", line 337, in __init__
    config.logdir.ensure(dir=1)
  File "/usr/local/lib/python3.5/site-packages/py/_path/local.py", line 524, in ensure
    return p._ensuredirs()
  File "/usr/local/lib/python3.5/site-packages/py/_path/local.py", line 506, in _ensuredirs
    parent._ensuredirs()
  File "/usr/local/lib/python3.5/site-packages/py/_path/local.py", line 509, in _ensuredirs
    self.mkdir()
  File "/usr/local/lib/python3.5/site-packages/py/_path/local.py", line 459, in mkdir
    py.error.checked_call(os.mkdir, fspath(p))
  File "/usr/local/lib/python3.5/site-packages/py/_error.py", line 85, in checked_call
    raise cls("%s%r" % (func.__name__, args))
py.error.EACCES: [Permission denied]: mkdir('/app/.tox',)
ERROR: Job failed: exit status 1

If there's anything else you need, just ask. Thanks in advance!