35

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
  • 465
  • 1
  • 4
  • 6

4 Answers4

26

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
  • 376
  • 3
  • 5
15

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

chovy
  • 251
  • 2
  • 4
9

Use this command:

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