apt-get fails in Docker with 'Could not open lock file ... Permission denied'
Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)Short answer
apt-get must write to /var/lib/apt, and a normal user cannot. In a Dockerfile this means a USER line above your RUN apt-get changed the user. Move the install above USER, or switch to root for that step and back after. In a running container, installing packages as another user fails the same way.
Fix it fast
1Find out which cause you have
Find out which user runs the failing step. A step as anything but root will fail.
RUN id && ls -ld /var/lib/apt/lists2Match what you see, then jump to its fix
What does the error look like?
Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)Permission denied when apt-get runs inside a container or a build step
Cause 1 of 2
A USER instruction above the install switched the build to a non-root user
1Check
USER sets the user for every later RUN, and for CMD and ENTRYPOINT, until the end of the stage. Check the order of lines.
grep -n -E '^(USER|RUN .*apt)' Dockerfile2Fix
Install packages before the USER line, or switch back to root for the install and drop privileges after it.
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
RUN useradd --create-home app
USER app3Verify
Rebuild; the apt step now runs.
docker build --progress=plain .Confirmed by docs.docker.com
Cause 2 of 2
The container is started as a different user and you install packages at run time
1Check
docker run -u (or --user) takes a name or UID and overrides who runs the process.
docker run --rm my-image id2Fix
Best: put the package in the image and rebuild it. For a one-off check, start a throwaway container as root.
docker run --rm -it --user root ubuntu:24.04 sh -c "apt-get update && apt-get install -y curl"3Verify
The install finishes without the lock error.
docker run --rm --user root ubuntu:24.04 idConfirmed by docs.docker.com
How do I stop it coming back?
- Put USER as late as possible in the Dockerfile, after all installs and chown steps.
- Install packages at build time; a container that installs at start is slower and not reproducible.
- Combine apt-get update and install in one RUN, and use --no-install-recommends to keep images small.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Docker build or container cannot create a directory: permission denied
PermissionError: [Errno 13] Permission denied: '/app/output.csv'
Once USER switches to a non-root account, every later RUN and the container itself run as that account.
3 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fix