Search code examples
dockerdocker-composedockerfiledevopsdocker-machine

restoring database from dockerfile


I'm trying to restore database from dockerfile, I'm dockerizing api and database, when I run docker-compose up --build I got error ERROR

how can I fix this, I'm sending dockerfile and docker-compose.yaml below, If anyone has some idea how to load database please tell me, any help will be welcome. Kind regards and thank you all

DOCKERFILE

 FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    WORKDIR /app
    EXPOSE 5192
    ENV ASPNETCORE_URLS=http://+:5192
    
    
    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
    WORKDIR /src
    COPY . .
    
    FROM build AS publish
    RUN dotnet publish "e-Res/e-Res.csproj" -c Release -o /app
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app .
    COPY ./e-Res/Uploads/Images ./Uploads/Images
    
    COPY ["e-Res/ERes.bak", "var/opt/mssql/data"]
    RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Starting database restore" && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'QWElkj132!' -Q "RESTORE FILELISTONLY FROM DISK='/var/opt/mssql/data/ERes.bak';"
    
    

DOCKER-COMPOSE.YAML

version: '3'
services:
  #mssql docker
  eres-sql:
    image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
    restart: unless-stopped
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=QWElkj132!
      - MSSQL_PID=Developer
    ports:
      - 1401:1433
    expose:
      - 1433
    networks:
      - eresnet2022

  eres-api:
    restart: unless-stopped
    build:
      context: .
    environment:
      - ConnectionStrings:DefaultConnection=Server=eres-sql,1433;Database=eres;User=sa;Password=QWElkj132!;ConnectRetryCount=0
      - ASPNETCORE_ENVIRONMENT=Development
    ports:
      - 5192:5192
    networks:
      - eresnet2022
    links:
      - eres-sql
    depends_on:
      - eres-sql

networks:
  eresnet2022:
    driver: bridge
    ENTRYPOINT ["dotnet", "e-Res.dll"]

Solution

  • You cannot create the Db when building the image but you have to wait until the container has started.

    You need to create another SQL Server container with a custom startup, or execute the restore command after the container has started

    FROM mcr.microsoft.com/mssql/server:2017-latest-ubuntu
    # You can also pass them from docker-compose file
    EXPOSE 1433
    ENV ACCEPT_EULA y
    ENV SA_PASSWORD QWElkj132!
    # 
    COPY ["e-Res/ERes.bak", "var/opt/mssql/data"]
    # Init scripts
    COPY ./init.sql .
    COPY ./entrypoint.sh .
    
    CMD /bin/bash ./entrypoint.sh
    

    On the init.sql you have the Db restore command

    RESTORE FILELISTONLY FROM DISK='/var/opt/mssql/data/ERes.bak';
    

    On the entrypoint.sh you have the boot point

    #!/bin/bash
    
    # Start the init.sql script (you can also put your command here and remove the init.sql file)
    # -l 50 means wait 50 seconds before the timeout (you can adjust this value) this is the time necessary to SQL server to startup (see below)
    /opt/mssql-tools/bin/sqlcmd -S localhost -l 50 -U SA -P "QWElkj132!" -i init.sql &
    
    # Start SQL server
    /opt/mssql/bin/sqlservr
    

    Remove the last two rows from the Dockerfile

    COPY ["e-Res/ERes.bak", "var/opt/mssql/data"]
    RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Starting database restore" && /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'QWElkj132!' -Q "RESTORE FILELISTONLY FROM DISK='/var/opt/mssql/data/ERes.bak';"