A. Hartman
A. Hartman

Reputation: 83

Docker-Compose build fails [Permission Denied] with postgresql unless it uses a cached Dockerfile

Thanks for taking the time to read this post!

I'm currently building an application that makes use of Python, Flask, Docker, Docker-compose and Postgres on Ubuntu 18.04 LTS. Previously, docker-compose build would succeed and everything was working according to plan. However, now that I switched to another Dockerfile, and I'm not using the cached version of the previous Dockerfile anymore, the build breaks with the following error:

PermissionError: [Errno 13] Permission denied: '/usr/lib/python3.7/__pycache__/__future__.cpython-37.pyc.140409285875056'
dpkg: error processing package python3.7-minimal (--configure):
 installed python3.7-minimal package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 python3.7-minimal

I have tried the following (Stackoverflow) posts:

but alas, to no avail.

I'm using the following files:

Dockerfile.prod

# pull official base image
FROM python:3.9.0-slim-buster

# set working directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV FLASK_ENV production
ENV APP_SETTINGS src.config.ProductionConfig

# install system dependencies
RUN apt-get update \
  && apt-get -y install netcat gcc postgresql\
  && apt-get clean

# add and install requirements
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# add app
COPY . .

# add and run as non-root user
RUN adduser --disabled-password myuser
USER myuser

# run gunicorn
CMD gunicorn --bind 0.0.0.0:$PORT manage:app

docker-compose.yml

version: '3.8'

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile.prod
    entrypoint: ['/usr/src/app/entrypoint.sh']
    volumes:
      - .:/user/src/app
    ports:
      - 5004:5000
    environment:
      - FLASK_ENV=development
      - APP_SETTINGS=src.config.DevelopmentConfig
      - DATABASE_URL=postgresql://postgres:postgres@api-db:5432/api_dev
      - DATABASE_TEST_URL=postgresql://postgres:postgres@api-db:5432/api_test
    depends_on:
      - api-db

  api-db:
    build:
      context: ./src/db
      dockerfile: Dockerfile
    expose:
      - 5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

./src/db/Dockerfile

FROM postgres:13-alpine

ADD create.sql /docker-entrypoint-initdb.d

Thanks once again for taking the time to read through this question, and if any additional information is required (e.g. a minimum viable Flask program to reproduce) please let me know!

Upvotes: 5

Views: 1464

Answers (1)

basquiatraphaeu
basquiatraphaeu

Reputation: 687

Make sure docker belongs to your user groups:

$ groups
$ foo adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

If that's not the case, try to create a docker group:

$ sudo groupadd docker 

You might receive the message: groupadd: group 'docker' already exists

Cool, now add your user to the docker group:

$ sudo usermod -aG docker $USER

Check if docker belongs to your user group:

$ groups
$ docker adm cdrom sudo dip plugdev lpadmin lxd sambashare foo

You most likely need to logout and login back again.

Even if after that docker is not listed among the groups your user belongs to try:

$ newgrp docker

Upvotes: 0

Related Questions