cli: After installing docker-ce 25.0.0 when building Dockerimage, the container won't run because of ulimit error
Description
Inside of my Dockerfile, which uses ubuntu:20.04, we install docker-ce. It is essential for us since we need to build AWS CDK code in a custom CodeBuild container.
Here’s the Dockerfile code (simplified):
FROM ubuntu:20.04
RUN apt update -y; \
apt upgrade -y; \
apt install software-properties-common -y; \
apt update -y; \
apt install wget -y; \
apt install curl -y; \
apt-get install ca-certificates gnupg lsb-release -y; \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg; \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null; \
apt-get update -y; \
apt install docker-ce -y; \
apt install unzip -y; \
service docker start; \
rm -rf /var/cache/apt;
ENTRYPOINT service docker start && /bin/bash
COPY install.py .
COPY remove.py .
The container was built successfully up until two days ago. On further investigation, I have found out that this is due to the Docker Engine upgrade to the 25.0.0 version.
If now we try docker run -t container, we get the following output (the same is printed when running service docker start while building the container:
service docker start /etc/init.d/docker: 62: ulimit: error setting limit (Invalid argument)
This is due to the line 62 in /etc/init.d/docker file which sets the ulimit hard limit:
ulimit -Hn 524288
Before the most recent 25.0.0 version release, it used to be the following line:
ulimit -n 1048576
When checking the /etc/security/limits.conf file inside of the Ubuntu image, I found out that the system hard limit is 100000.
If I remove the service docker start command from the Dockerfile (both in the RUN and ENTRYPOINT commands), the issue persists.
The only way I could make my image run is by hardcoding the previous version of docker-ce:
apt install docker-ce=5:24.0.7-1~ubuntu.20.04~focal -y
This has fixed the problem but is still a huge obstacle for us since we are now forced to use the older version of docker-ce and cannot get updates.
I hope this case will be helpful to anyone having the same problem as we did. I also hope a fix will be introduced so we could get the most recent updates on our image.
Reproduce
- Build an image using a Dockerfile and install the latest version of docker-ce.
- Try running the container
Expected behavior
We expected the container to work on 25.0.0 version of docker engine in the same way it did on 24.0.7
docker version
Not accessible since the container couldn't run.
The version being installed is 25.0.0
docker info
Not accessible since the container couldn't run.
Additional Info
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 11
- Comments: 15 (8 by maintainers)
Commits related to this issue
- Omit `ulimit` calls in docker-ce init.d script Context and fix are mentioned here https://github.com/docker/cli/issues/4807#issuecomment-1903950217 Looks like this was only impacting new image build... — committed to a-hilaly/ack-test-infra by a-hilaly 3 years ago
- Omit `ulimit` calls in docker-ce init.d script (#424) Context and fix are mentioned here https://github.com/docker/cli/issues/4807#issuecomment-1903950217 Looks like this was only impacting new im... — committed to aws-controllers-k8s/test-infra by a-hilaly 3 years ago
- docker in docker service fails to start https://github.com/docker/cli/issues/4807 As of 02/01/2024 a change in the "/etc/init.d/docker" script shipped with docker v25 is preventing the cpi image to ... — committed to cloudfoundry/bosh by nouseforaname 3 years ago
- fix: Normalize `RLIMIT_NOFILE` to sensible defaults During review, it was decided to remove `LimitNOFILE` from `docker.service` to rely on the systemd v240 implicit default of `1024:524288`. On suppo... — committed to moby/moby by polarathene 3 years ago
I think your build effectively happened to be “lucky” and managed to start because it didn’t have to adjust the
ulimits, and therefore JUST enough privileges to start the service (theulimitof the container used during build already had the correct values set (making theulimit -n 1048576a no-op, so no privileges required).Also trying what works and what doesn’t; (also see setrlimit(2), and ulimit
Keeping both the same also works;
Note that these ulimits are configured for the
dockerandcontainerdservices, so their values may differ between hosts, and even between systemd versions, which means that your build could fail depending on that.Given that you’re trying to run the docker engine in a container and ulimits can be set for the container at runtime (using the
--ulimitflag, see above), which will be inherited by processes inside the container, I think a good solution for your use-case would be to just disable theulimitin the init script;Here’s my Dockerfile with that applied;
With that change, the build succeeds succesfuly;
Yes - starting the docker service yields the error
checking the system’s
ulimit -Hnthen modifying the ulimit set in/etc/init.d/dockerfixes it, similar to your fix.Its weird I remember having the same issue years ago, then nothing, then I get it again today after installing on ubuntu 25.0.0
Thanks for reporting. It looks like this is an issue with code in the “engine” code, not the CLI itself, so probably the better location would be in https://github.com/moby/moby. Unfortunately, GitHub doesn’t allow transfering tickets between orgs, so I cannot move it there (but perhaps you could open a new ticket?)
This issue is related to https://github.com/moby/moby/commit/c8930105bc9fc3c1a8a90886c23535cc6c41e130, which is part of this PR;
That PR changed the default ulimits to follow systemd’s defaults (only raising the hard-limit, not the soft-limit).
When running the docker service as a systemd unit, this would be handled by systemd, but the sysvinit scripts are provided for non-standard setups where systemd is not used (likely in your container). I wondered if it was perhaps that ubuntu 20.04 didn’t provide the
H/S(hard / soft limits) option, in which case this could be a packaging issue (adjust the file depending on distro and distro-version; similar to https://github.com/docker/docker-ce-packaging/pull/968) but looks like both 20.04 and 22.04 do;But this is a docker service inside a container, and the container is part of the Dockerfile / ran during
docker build. Running the docker service requires a privileged (--privileged) container. Containers that are used duringdocker builddo not support--privileged(by design).It’s still possible to run the resulting image that was built as privileged, but in that case the container must be started with the
--privileged.Thanks! So, I think this is the issue indeed https://github.com/docker/cli/issues/4807#issuecomment-1903759890;
I tried reproducing the issue; I slightly simplified the Dockerfile, and reduced it to only the essential packages, and split the “install docker” step to a separate
RUN(to allow caching other steps if I had to change something);When building the Dockerfile on a docker 25.0 engine with BuildKit, the build works without errors;
When using the the legacy builder (BuildKit disabled through
DOCKER_BUILDKIT=0) however, it failsThe classic builder starts containers through containerd, whereas BuildKit starts containers through BuildKit (in which case
ulimit(LIMIT_NOFILE) of thedockerdservice are applied, not those fromcontainerd);When running the build on an older version of docker with the previous LIMIT_NOFILE, it fails with BuildKit as well. Here’s the same build on a docker 24.0.2 (Ubuntu 18.04) test-machine that I didn’t update yet;
It works with the previous docker-ce version, yes. We don’t get any errors there.