27

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.

4 Answers 4

20

Why not docker-compose build ?

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

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

2
  • 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. Oct 14, 2018 at 20:15
  • 1
    This does NOT force a rebuild, at least not when no files have changed. Sep 25, 2021 at 6:59
10

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

2
  • 2
    I think this should be the answer as it actually rebuilds, despite cache, which is what led me to find this Q&A
    – dibs
    Apr 15 at 5:29
  • 1
    I agree. depends on why docker build is not a way to "fix" anything. That just assumes everything is working as normal.
    – chovy
    Apr 15 at 5:52
8

Use this command:

docker-compose up --build
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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.