<p>I've found sometimes I need <code>docker-compose build --no-cache</code> if I have a bad deploy.</p>

CC BY-SA 4.0
2022-04-15T05:29:55.077Z
I think this should be the answer as it actually rebuilds, despite cache, which is what led me to find this Q&A
dibs
CC BY-SA 4.0
2022-04-15T05:52:25.847Z
I agree. depends on why `docker build` is not a way to "fix" anything. That just assumes everything is working as normal.
chovy
CC BY-SA 4.0
2024-01-31T12:24:46.253Z
This was the only answer that worked for me.
strix25
CC BY-SA 4.0
2020-04-21T20:49:25.977Z
2020-04-21T20:49:25.977Z
chovy
<p>Use this command: </p>

<pre><code>docker-compose up --build
</code></pre>

CC BY-SA 4.0
2018-10-13T04:39:05.987Z
2019-11-26T05:11:08.413Z
2019-11-26T05:11:08.413Z
Parmatma
<p>Another way that worked for me was to remove all images and then recreate all images.</p>
<p>Step 1. This will remove all images</p>
<p><code>docker-compose down --rmi local/all</code></p>
<p><a href="https://docs.docker.com/compose/reference/down/" rel="nofollow noreferrer">https://docs.docker.com/compose/reference/down/</a></p>
<p>Step 2. This will create all images as part of up</p>
<p><code>docker-compose up --build</code></p>
<p><a href="https://docs.docker.com/compose/reference/up/" rel="nofollow noreferrer">https://docs.docker.com/compose/reference/up/</a></p>

CC BY-SA 4.0
2022-02-07T11:09:41.423Z
2022-02-07T11:09:41.423Z
DivineCoder
<p>Why not <code>docker-compose build</code> ? </p>

<p><a href="https://docs.docker.com/compose/reference/build/" rel="noreferrer">https://docs.docker.com/compose/reference/build/</a></p>

<p>You can also do <code>docker-compose up --build</code> to force a rebuild. </p>

CC BY-SA 4.0
2018-10-14T20:15:06.270Z
Also worth noting that even if you build every time, Docker's built in cache means the image will only be rebuilt if you change the Dockerfile or any files that you are COPYing.
user2640621
CC BY-SA 4.0
2021-09-25T06:59:17.013Z
This does NOT force a rebuild, at least not when no files have changed.
AntonOfTheWoods
CC BY-SA 4.0
2022-11-04T18:28:59.620Z
hint: you can use parallel option to make it faster `docker-compose build --no-cache --parallel`
Akhil
CC BY-SA 4.0
2024-04-17T13:38:02.893Z
This rebuilds ALL IMAGES  not just the ones where their Dockerfile has changed
Shayan
CC BY-SA 4.0
2018-10-12T17:32:48.557Z
2018-10-12T17:43:57.540Z
2018-10-12T17:43:57.540Z
janDro
<p>I am using docker-compose to start 2 containers using the following <strong>docker-compose.yaml</strong>.</p>

<pre><code>version: '2.4'

services:
  myservice1:
    container_name: mycontainername
    build: 
       context: .
       dockerfile: ./my-dockerfolder/Dockerfile
       args:       
       - MY_ARG=${MY_ARG}
    environment: 
       - MY_ARG=${MY_ARG}
    mem_limit: 3500M
    ports:
    - "9090:9090"
    extra_hosts:
    - "one.mydomain.net:127.0.0.1"
    - "two.mydomain.net:127.0.0.1"


  myservice2:
    container_name: myothercontainername
    build: 
       context: .
       dockerfile: ./other-dockerfolder/Dockerfile
       args: 
       - BUILD=${BUILD}
       - MY_ARG=${MY_ARG2}
    environment: 
       - MY_ARG2=${MY_ARG2}
    ports:
    - "2023:22"
</code></pre>

<p>My problem is that when I run <strong>docker-compose up</strong> again, it is using the same image to create the container, regardless of changes to the docker file.</p>

<p>How can I force it to take changes to the docker file into account?</p>

<p>Are other docker objects being reused that I need to be worried about?  I want the second time I run <strong>docker-compose up</strong> to be as clean an environment as the first time.</p>

<p>note: I do not want to delete images that I do not need to delete.  I have a slow connection to the docker repo, and removeing all the images would make my <strong>docker compose up</strong> take ~22 minutes. </p>

<docker><docker-compose>
How can I force Docker Compose to rebuild containers when dockerfile changes?
bnieland
# How can I force Docker Compose to rebuild containers when dockerfile changes?

(Asked by: username_0 on 2018-10-12)

I am using docker-compose to start 2 containers using the following docker-compose.yaml.
version: '2.4'

services:
  myservice1:
    container_name: mycontainername
    build: 
       context: .
       dockerfile: ./my-dockerfolder/Dockerfile
       args:       
       - MY_ARG=${MY_ARG}
    environment: 
       - MY_ARG=${MY_ARG}
    mem_limit: 3500M
    ports:
    - "9090:9090"
    extra_hosts:
    - "one.mydomain.net:127.0.0.1"
    - "two.mydomain.net:127.0.0.1"


  myservice2:
    container_name: myothercontainername
    build: 
       context: .
       dockerfile: ./other-dockerfolder/Dockerfile
       args: 
       - BUILD=${BUILD}
       - MY_ARG=${MY_ARG2}
    environment: 
       - MY_ARG2=${MY_ARG2}
    ports:
    - "2023:22"

My problem is that when I run docker-compose up again, it is using the same image to create the container, regardless of changes to the docker file.
How can I force it to take changes to the docker file into account?
Are other docker objects being reused that I need to be worried about?  I want the second time I run docker-compose up to be as clean an environment as the first time.
note: I do not want to delete images that I do not need to delete.  I have a slow connection to the docker repo, and removeing all the images would make my docker compose up take ~22 minutes.

**Tags:** docker, docker-compose


---

## 4 answers

### Answer by username_6 on 2018-10-12. Score: 26 | Accepted answer

Why not `docker-compose build` ?

<https://docs.docker.com/compose/reference/build/>

You can also do `docker-compose up --build` to force a rebuild.

> **Comments:**
> **username_7**: Also worth noting that even if you build every time, Docker's built in cache means the image will only be rebuilt if you change the Dockerfile or any files that you are COPYing.  
> **username_8**: This does NOT force a rebuild, at least not when no files have changed.  
> **username_9**: hint: you can use parallel option to make it faster `docker-compose build --no-cache --parallel`  
> **username_10**: This rebuilds ALL IMAGES  not just the ones where their Dockerfile has changed  

---

### Answer by username_1 on 2020-04-21. Score: 14

I've found sometimes I need `docker-compose build --no-cache` if I have a bad deploy.

> **Comments:**
> **username_2**: I think this should be the answer as it actually rebuilds, despite cache, which is what led me to find this Q&A  
> **username_1**: I agree. depends on why `docker build` is not a way to "fix" anything. That just assumes everything is working as normal.  
> **username_3**: This was the only answer that worked for me.  

---

### Answer by username_4 on 2018-10-13. Score: 9

Use this command: 
docker-compose up --build


---

### Answer by username_5 on 2022-02-07. Score: 0

Another way that worked for me was to remove all images and then recreate all images.

Step 1. This will remove all images

`docker-compose down --rmi local/all`

<https://docs.docker.com/compose/reference/down/>

Step 2. This will create all images as part of up

`docker-compose up --build`

<https://docs.docker.com/compose/reference/up/>


---

