# Build docker image fails randomly with "Error response from daemon: container is marked for removal and cannot be started"

**URL:** <https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429>\
**Category:** Build Environment\
**Tags:** docker\
**Created:** [March 16, 2021, 5:22am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429 "2021-03-16T05:22:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sashabaranova](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/sashabaranova/32/26938_2.png) [@sashabaranova](https://discuss.circleci.com/u/sashabaranova)\
**Post date:** [March 16, 2021, 5:22am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429/1 "2021-03-16T05:22:06Z")

</div>

Hi all!

We’re experiencing random failures of our docker image builds. The error we get is “Error response from daemon: container is marked for removal and cannot be started”. The error happens randomly: sometimes the build is successful and sometimes it fails without anything being changed from our side. We started to get this error on 15 March and we did not have it before (the environment, build scripts and images were not changed for at least 1 month).

Any thoughts on what could be the problem?

 ![image](https://global.discourse-cdn.com/circleci/original/3X/b/0/b0f55d0fae5eb582e917ccf7ac126cb9b93cd480.png)

---

<div class="post-metadata">

**Author:** ![amdprophet](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/amdprophet/32/25103_2.png) [@amdprophet](https://discuss.circleci.com/u/amdprophet)\
**Post date:** [March 17, 2021, 1:12am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429/2 "2021-03-17T01:12:38Z")

</div>

We’re seeing the the same issue when using the buildx multiarch image. I first started seeing it on March 10.

---

<div class="post-metadata">

**Author:** ![amdprophet](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/amdprophet/32/25103_2.png) [@amdprophet](https://discuss.circleci.com/u/amdprophet)\
**Post date:** [March 17, 2021, 1:34am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429/3 "2021-03-17T01:34:12Z")

</div>

Upgrading the machine executor we were using from `ubuntu-2004:202010-01` to `ubuntu-2004:202101-01` along with upgrading from buildx 0.4.1 to 0.5.1 seems to have fixed it.

---

<div class="post-metadata">

**Author:** ![amdprophet](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/amdprophet/32/25103_2.png) [@amdprophet](https://discuss.circleci.com/u/amdprophet)\
**Post date:** [March 18, 2021, 6:39am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429/4 "2021-03-18T06:39:16Z")

</div>

I was incorrect; the issue has started reoccurring.

**EDIT:**  
After some additional investigation, it seems that this was happening due to the multiarch container being cached across builds. I also noticed that we weren’t logging into Docker. To resolve this I added the following to the beginning of each build:

```
echo $PASSWORD | docker login --username $USERNAME --password-stdin
docker rm -f buildx_buildkit_docker-multiarch0

```

The name of your multiarch builder differs from mine; make sure to take that into account.

We haven’t seen the issue occur since making these changes.

---

<div class="post-metadata">

**Author:** ![sashabaranova](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/sashabaranova/32/26938_2.png) [@sashabaranova](https://discuss.circleci.com/u/sashabaranova)\
**Post date:** [March 26, 2021, 5:14am UTC](https://discuss.circleci.com/t/build-docker-image-fails-randomly-with-error-response-from-daemon-container-is-marked-for-removal-and-cannot-be-started/39429/5 "2021-03-26T05:14:41Z")

</div>

Thanks for your help!

We have handled it in a similar way by adding:  
`docker container prune -f`  
It has solved the issue for us.

Upon advice from support, we’ll also add a debug step that uploads the [buildkit logs](https://github.com/docker/buildx/issues/331#issuecomment-663606169) as [artifacts in CircleCI](https://circleci.com/docs/2.0/artifacts/) to check further.
