Compose: --force-recreate should removes the old container

Created on 23 Dec 2016  路  9Comments  路  Source: docker/compose

I'm trying to recreate the containers after to rebuild an image (using docker-compose up -d --build --force-recreate) and I seem that it does not work properly. Now, I'm using docker-compose down && docker-compose up -d but would be cool if I can recreate them without the down command.

version: '2.1'

services:
  wordpress:
    build: .
    image: fbt-wpsite:${VERSION:-latest}
    volumes:
      - ./uploads:/var/www/html/wp-content/uploads
    depends_on:
      - mysql
    env_file: .env
    environment:
      - VIRTUAL_HOST=$DOMAIN
      - VIRTUAL_PORT=9000
    restart: unless-stopped

  mysql:
    image: mariadb
    volumes:
      - ./.mysql:/var/lib/mysql
    env_file: .env
    restart: unless-stopped

  server:
    image: jwilder/nginx-proxy
    ports:
      - ${PORT:-80}:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    restart: unless-stopped

Dockerfile:

FROM wordpress:4-php7.0-apache

COPY ./plugins /var/www/html/wp-content/plugins
COPY ./theme /var/www/html/wp-content/themes/fbt

Most helpful comment

@shin-

Nop, I need to run down as well.

This is my bash script (it works):

cd www \
        && git pull origin qa \
        && docker-compose build \
        && docker-compose down \
        && docker-compose up -d

but using

cd www \
        && git pull origin qa \
        && docker-compose build \
        && docker-compose up -d --force-recreate

doesn't work.

All 9 comments

Hi @brunocascio,

What about it seems to "not work properly"? What challenges are you encountering?

Hi @shin-

The container is "recreated" with the old image instead of the new buit image. Is it okay?

Oh, I see! Do you get the results you expect if you do docker-compose build && docker-compose up -d --force-recreate instead?

@shin-

Nop, I need to run down as well.

This is my bash script (it works):

cd www \
        && git pull origin qa \
        && docker-compose build \
        && docker-compose down \
        && docker-compose up -d

but using

cd www \
        && git pull origin qa \
        && docker-compose build \
        && docker-compose up -d --force-recreate

doesn't work.

I'm currently having the same issue.

docker-compose up -d --force-recreate recreates the container from the image which it was run with originally, and not with the updated one (pulled just a minute ago). Only a docker-compose down actually forces the creation of container from the new image.

Image building is not involved at all in my case.

I tried to create a reproducing scenario in smaller scale, but strangely it worked. Must be an edge case somewhere...

So, my issue is the following:
Right after docker-compose pull fetched a newer image I am getting different results if I run the same command on a new container (docker run) and in a just recreated container (docker-compose up -d --force-recreate and then docker exec).

I can run docker-compose up -d --force-recreate all day, but will only have container created out of the more recent image if I explicitly remove that container first.

@shin-
Could you please confirm that this is an error case?

Turns out this was due to the volume definition in Dockerfile, as discussed in #2127. After removing the VOLUME clause the --force-recreate started working again.

@Umkus I'm using Docker version 18.06.1-ce, build e68fc7a and I can use the following command to recreate anonymous volumes:

docker-compose up -d --build --force-recreate --renew-anon-volumes db

It seems the flag --renew-anon-volumes was recently added

Sounds like this was fixed via the addition of that new flag? Closing on that basis, please reopen if I got the wrong end of the stick.

Was this page helpful?
0 / 5 - 0 ratings