I have two entities. My asp.net application and the database from where it takes it's data.
My Dockerfile is following:
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY bin/Debug/net6.0/ ./
COPY /DBinit.sql ./
COPY /entrypoint.sh ./
WORKDIR ./
ENTRYPOINT ["dotnet", "Server.dll"]
EXPOSE 80/tcp
RUN chmod x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
My entrypoint.sh is following:
#!/bin/bash
set -e
run_cmd="dotnet run --server.urls http://*:80"
until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done
>&2 echo "SQL Server is up - executing command"
$run_cmd
sleep 30s
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Your_password123 -d master -i /DBinit.sql
And my Docker Compose is:
version: "3.9"
services:
web:
build: .
ports:
- "8000:80"
depends_on:
- db
db:
image: "mcr.microsoft.com/mssql/server"
environment:
SA_PASSWORD: "Your_password123"
ACCEPT_EULA: "Y"
My application is running correctly. Both server_db and server_web are up. My issue, is that the db itself is empty and my DBinit.sql script is not getting executed.
From where I can see. My problem is that this DBinit.sql file is not getting copied inside the server_db. If I will open up the ls -la of the / in server_web. This init script is there (but it shouldn't even be as part of my web app)
While if I will do the same inside server_db. There is no sql script in it. And I am getting really confused with all of these Docker entities.
Could someone point me to a solution ?
CodePudding user response:
You cannot use /opt/mssql-tools/bin/sqlcmd
from your asp.net container.
I recommend you to do the next:
- Create separate
Dockerfile
for your DB (name itDockerfile.db
) and remove related from your app'sDockerfile
:
FROM mcr.microsoft.com/mssql/server
COPY /DBinit.sql /
COPY /db_entrypoint.sh /
WORKDIR /
# you may chmod db_entrypoint.sh on your host system so you will not need this line at all
RUN chmod x /db_entrypoint.sh
ENTRYPOINT /db_entrypoint.sh & /opt/mssql/bin/sqlservr
- Move DB-related stuff to another entrypoint (let's name it
db_entrypoint.sh
):
#!/bin/bash
sleep 30s
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -d master -i /DBinit.sql
(note that I've replaced Your_password123
with env variable)
- Prepare your
docker-compose.yml
:
version: "3.9"
services:
web:
build: .
ports:
- "8000:80"
depends_on:
- db
db:
build:
context: .
dockerfile: Dockerfile.db
environment:
SA_PASSWORD: "Your_password123"
ACCEPT_EULA: "Y"
That's all. Check it out — now you may be able to init your DB successfully