0

I'm deploying the project with Asp.net Core, PostgreSql and Docker in Windows 10 (no PostgreSql installed). So I have to run sql script to update data before the application launches (for registering a singleton dependency injection).

The content of my Dockerfile as following:

# TODO use official docker image
FROM microsoft/dotnet:1.1.0-sdk-projectjson

# Install .NET CLI dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
        autoconf \
        automake \
        bzip2 \
        file \
        g++ \
        gcc \
        imagemagick \
        libbz2-dev \
        libc6-dev \
        libcurl4-openssl-dev \
        libdb-dev \
        libevent-dev \
        libffi-dev \
        libgdbm-dev \
        libgeoip-dev \
        libglib2.0-dev \
        libjpeg-dev \
        libkrb5-dev \
        liblzma-dev \
        libmagickcore-dev \
        libmagickwand-dev \
        libmysqlclient-dev \
        libncurses-dev \
        libpng-dev \
        libpq-dev \
        libreadline-dev \
        libsqlite3-dev \
        libssl-dev \
        libtool \
        libwebp-dev \
        libxml2-dev \
        libxslt-dev \
        libyaml-dev \
        make \
        patch \
        xz-utils \
        zlib1g-dev \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV ASPNETCORE_URLS="http://*:5000"
ENV ASPNETCORE_ENVIRONMENT="Development"

# Copy files to app directory
COPY . /app

# Set working directory
WORKDIR /app

# Restore NuGet packages
RUN ["dotnet", "restore"]

# Build app
RUN ["dotnet", "build"]

#dotnet ef migrations add InitialCreate
RUN ["dotnet", "ef", "migrations", "add", "InitialCreate"]
# Open up port
EXPOSE 5000

CMD chmod +x ./docker-start.sh
CMD bash ./docker-start.sh

And here is the content of docker-start.sh:

#!/bin/bash

set -e

# How to apply migrations
dotnet ef database update

# I would like to run sql file at here"
psql -h postgres --username postgres -d POSTGRES_USER -a -f /app/static.sql    

# Start web app
echo "Starting web app"
dotnet run

How can I do that? Thanks advanced.

4
  • Does your script running command failed? What is the question? Commented Feb 16, 2017 at 5:44
  • You might find it easier to run your migrations in code. See my answer stackoverflow.com/a/38283080/5782634 Commented Feb 16, 2017 at 5:48
  • yes @evgenyl I cant not run the script. Commented Feb 16, 2017 at 6:11
  • @Brad, I have to run the script before the app starting so i cant use the code for this. Commented Feb 16, 2017 at 6:16

1 Answer 1

0

I have just found a solution for this. I missed postgresql-client. We will be need to install postgresql-client as using psql to run the sql script from Dockerfile.

So Dockerfile should be changed:

# TODO use official docker image
FROM microsoft/dotnet:1.1.0-sdk-projectjson

# Install .NET CLI dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
        autoconf \
        automake \
        bzip2 \
        file \
        g++ \
        gcc \
        imagemagick \
        libbz2-dev \
        libc6-dev \
        libcurl4-openssl-dev \
        libdb-dev \
        libevent-dev \
        libffi-dev \
        libgdbm-dev \
        libgeoip-dev \
        libglib2.0-dev \
        libjpeg-dev \
        libkrb5-dev \
        liblzma-dev \
        libmagickcore-dev \
        libmagickwand-dev \
        libmysqlclient-dev \
        libncurses-dev \
        libpng-dev \
        libpq-dev \
        libreadline-dev \
        libsqlite3-dev \
        libssl-dev \
        libtool \
        libwebp-dev \
        libxml2-dev \
        libxslt-dev \
        libyaml-dev \
        make \
        patch \
        xz-utils \
        zlib1g-dev \
        postgresql-client \
    && rm -rf /var/lib/apt/lists/*

# Install netcat so that we can ping the database server until it
RUN apt-get update -qq \
    && apt-get install -y netcat \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV ASPNETCORE_URLS="http://*:5000"
ENV ASPNETCORE_ENVIRONMENT="Development"
ENV DB_HOSTNAME="posgres"

# Copy files to app directory
COPY . /app

# Set working directory
WORKDIR /app

# Restore NuGet packages
RUN ["dotnet", "restore"]

# Build app
RUN ["dotnet", "build"]

#dotnet ef migrations add InitialCreate
RUN ["dotnet", "ef", "migrations", "add", "InitialCreate"]
# Open up port
EXPOSE 5000

CMD chmod +x ./docker-start.sh
CMD bash ./docker-start.sh

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.