Alpine Docker ERROR: Unable to lock database: Permission denied ERROR: Failed to open apk database: Permission denied

64,264

Solution 1

For those seeing this error using a Dockerfile (and coming here via a Google search): add the following line to your Dockerfile:

USER root

Solution 2

Hopefully this will help anyone who is not interested in creating a new container.

If you are trying to enter into your docker container like so:

docker exec -it <containername> /bin/sh

Instead, try this:

docker exec -it --user=root <containername> /bin/sh

Solution 3

The best fix is to place USER <youruser> AFTER the lines where your docker build is failing. In most cases it is safe to add the USER line directly above the command or entrypoint.

For example:

FROM python:3.8.0-alpine

RUN addgroup -S app && adduser -S -G app app

RUN apk add --no-cache libmaxminddb postgresql-dev gcc musl-dev

ADD . .

USER app

ENTRYPOINT ["scripts/entrypoint.sh"]

CMD ["scripts/gunicorn.sh"]

Solution 4

For those seeing this error when running through a Jenkins pipeline script (and coming hre via a Google search), use the following when starting your Docker image:

node('docker') {
  docker.image('golang:1.14rc1-alpine3.11').inside(' -u 0') {
    sh 'apk add curl'
    ...
  }
}

Solution 5

docker exec -it --user=root {containername} bash

with this I can able to execute apk-update

Share:
64,264
Vladimir_314159
Author by

Vladimir_314159

awkward knitting needles.

Updated on July 05, 2022

Comments

  • Vladimir_314159
    Vladimir_314159 almost 2 years

    So I have used the default docker for testcafe which on docker hub is testcafe/testcafe and I have to run a few testcafe scripts.

    However, I need the screenshot that fires on error, to be uploaded to somewhere where I can look at it later after the docker image is done running.

    I am using the Imgur program which uses bash so I re-did a few things to make it sh compatible and everything works except I need curl. I tried running

    apk add curl
    

    but I'm getting the error

    ERROR: Unable to lock database: Permission denied ERROR: Failed to open apk database: 
    

    Now I no this means that I do not have permission to do this but can I get around this is there some way to become root (this is in bitbucket pipeline).

    I do NOT really want to create my own docker.

    Also note all questions I have found relating to this are about installing while creating the docker, however, my question is how to do this after the docker is created. thx (a fine answer would be another way to save the screen shot, but preferably not with ssh).

  • kamal
    kamal almost 5 years
    If I do not want to use root. What should I add in this command RUN addgroup -S something && adduser -S -G something something to make it run apk add?
  • RoboBear
    RoboBear over 4 years
    Insecure, but it works. It was a permissions issue for the agent running the Dockerfile. For Reference -- I ran into this setting up a Dockerfile build for a Digital Ocean Jenkins CI server (I needed to APK ADD NodeJS and NVM, etc.).
  • LOLWTFasdasd asdad
    LOLWTFasdasd asdad over 4 years
    @kamal the way i fixed it is that I simply used the RUN addgroup -S something && adduser -S -G something something after the apk add comment. Probalby not fitting for every scenario though.
  • Thomas Göhringer
    Thomas Göhringer about 4 years
    When using a declarative pipeline add args '-u 0' inside docker { } before defining the image
  • Felipe Plazas
    Felipe Plazas over 3 years
    This indeed fixed the issue, just make sure to run 'USER nobody' at the end of your Dockerfile.
  • Karthikeyan
    Karthikeyan over 3 years
    It is working!! Thanks. Can you explain what exactly this args is doing?
  • kAmol
    kAmol about 3 years
    are their any consequences while using this container? or we need to modify entrypoiny/cmd in order to login as root always?
  • koppor
    koppor about 2 years
    Hint: Please check the Dockerfile for USER statements and use that USER statement after the command in place. In my case, the "upstream" image had USER node in it. I then used USER root, then my commands, then USER node to be closer to the original image again.