Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker does not create a new container when using docker-compose build

I've set up two windows container for ASP.NET and MSSQL server. On the first docker-compose build everything works as expected. Then after I've made some changes to the custom dockerfile and run docker-compose build again it uses the old container again, not making any changes.

I assumed that when i did a build it created a new container. Am i misunderstanding how docker works?

This is the docker-compose.yml

version: '3'
services:
  db:
    image: microsoft/mssql-server-windows-developer

    environment:
      sa_password: "Password1234!"
      ACCEPT_EULA: "Y"

    ports:
      - "8003:1433"
    build:
      context: .
      dockerfile: mssql.dockerfile

  web:
    build:
      context: .
      dockerfile: web.dockerfile
    image: mcr.microsoft.com/dotnet/framework/aspnet:4.8
    #volumes:
    #  - .:C:/inetpub/wwwroot
    ports: 
      - "8080:80"
      - "8081:431"

This is the mssql.dockerfile

# escape=`

FROM microsoft/mssql-server-windows-developer

#set shell
SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

#make temp folder
RUN mkdir C:\temp

#copy script to temp folder
COPY DownloadDatabase.ps1 C:\temp
COPY RestoreDatabase.ps1 C:\temp

#run script to retrieve production database
WORKDIR C:\temp

RUN .\DownloadDatabase.ps1 -sourcefile <url> -destinationfile <target>

CMD .\RestoreDatabase.ps1

It is very easy to tell if the image has been re-used because the mkdir C:\temp errors out saying the directory already exists.

EDIT: I've already tried all the options on docker compose. no-cache, force-rm

like image 766
LlamaNL Avatar asked Nov 06 '22 17:11

LlamaNL


1 Answers

docker-compose build 

Only builds images but does not start containers.

That's why your changes in dockerfile are not applied. You have rebuilded the image but not the container. It's the reason why the container previoulsy launched is based on the older version of the image.

docker-compose up 

From Docker documentation :

If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes). To prevent Compose from picking up changes, use the --no-recreate flag.

In order to make shure that both of your image and container are rebuilded you have to add this flags :

docker-compose up --force-recreate --build

That way your containers are based on the correct image version.

Explanation on flags from Docker documentation :

--build                   Build images before starting containers.

--force-recreate          Recreate containers even if their configuration
                          and image haven't changed.

If you want to do this for a specific service just add the service name at the end of command line :

docker-compose up --force-recreate --build serviceName

Another flag useful if you want a clear output is the -d flag :

 -d, --detach              Detached mode: Run containers in the background,
                           print new container names. Incompatible with
like image 129
Yann Avatar answered Nov 14 '22 23:11

Yann