Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

apt-get fails in Docker with 'Could not open lock file ... Permission denied'

ErrorCould 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.

  • Applies to Docker Engine 27 and 28, Debian and Ubuntu base images
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/lists
  2. 2Match what you see, then jump to its fix

    1. 1id in the failing RUN shows a uid other than 0A USER instruction above the install switched the build to a non-root userFix
    2. 2The error comes from docker run or compose, not from docker buildThe container is started as a different user and you install packages at run timeFix

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)' Dockerfile

2Fix

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 app

3Verify

Rebuild; the apt step now runs.

docker build --progress=plain .

Confirmed by docs.docker.com

Did this fix it?

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 id

2Fix

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 id

Confirmed by docs.docker.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.