31

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.

bnieland
  • 425
  • 1
  • 4
  • 6

4 Answers4

24

Why not docker-compose build ?

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

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

janDro
  • 356
  • 2
  • 5
  • 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 Oct 14 '18 at 20:15
  • 1
    This does NOT force a rebuild, at least not when no files have changed. – AntonOfTheWoods Sep 25 '21 at 06:59
  • hint: you can use parallel option to make it faster docker-compose build --no-cache --parallel – Akhil Nov 04 '22 at 18:28
12

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

chovy
  • 221
  • 2
  • 4
  • 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 '22 at 05: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 '22 at 05:52
  • 1
    This was the only answer that worked for me. – strix25 Jan 31 '24 at 12:24
8

Use this command:

docker-compose up --build
Parmatma
  • 81
  • 2
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/