How to get docker-compose to always re-create containers from fresh images?

后端 未结 7 2101
误落风尘
误落风尘 2020-11-29 15:13

My docker images are built on a Jenkins CI server and are pushed to our private Docker Registry. My goal is to provision environments with docker-compose which always start

相关标签:
7条回答
  • 2020-11-29 15:35

    docker-compose up --force-recreate is one option, but if you're using it for CI, I would start the build with docker-compose rm -f to stop and remove the containers and volumes (then follow it with pull and up).

    This is what I use:

    docker-compose rm -f
    docker-compose pull
    docker-compose up --build -d
    # Run some tests
    ./tests
    docker-compose stop -t 1
    

    The reason containers are recreated is to preserve any data volumes that might be used (and it also happens to make up a lot faster).

    If you're doing CI you don't want that, so just removing everything should get you want you want.

    Update: use up --build which was added in docker-compose 1.7

    0 讨论(0)
  • You can pass --force-recreate to docker compose up, which should use fresh containers.

    I think the reasoning behind reusing containers is to preserve any changes during development. Note that Compose does something similar with volumes, which will also persist between container recreation (a recreated container will attach to its predecessor's volumes). This can be helpful, for example, if you have a Redis container used as a cache and you don't want to lose the cache each time you make a small change. At other times it's just confusing.

    I don't believe there is any way you can force this from the Compose file.

    Arguably it does clash with immutable infrastructure principles. The counter-argument is probably that you don't use Compose in production (yet). Also, I'm not sure I agree that immutable infra is the basic idea of Docker, although it's certainly a good use case/selling point.

    0 讨论(0)
  • 2020-11-29 15:46
    docker-compose up --build
    

    OR

    docker-compose build --no-cache
    
    0 讨论(0)
  • 2020-11-29 15:52

    The only solution that worked for me was this command :

    docker-compose build --no-cache
    

    This will automatically pull fresh image from repo and won't use the cache version that is prebuild with any parameters you've been using before.

    0 讨论(0)
  • 2020-11-29 15:52

    I claimed 3.5gb space in ubuntu AWS through this.

    clean docker

    docker stop $(docker ps -qa) && docker system prune -af --volumes

    build again

    docker build .

    docker-compose build

    docker-compose up

    0 讨论(0)
  • 2020-11-29 15:53

    By current official documentation there is a short cut that stops and removes containers, networks, volumes, and images created by up, if they are already stopped or partially removed and so on, then it will do the trick too:

    docker-compose down
    

    Then if you have new changes on your images or Dockerfiles use:

    docker-compose build --no-cache
    

    Finally:docker-compose up

    In one command: docker-compose down && docker-compose build --no-cache && docker-compose up

    0 讨论(0)
提交回复
热议问题