3
votes

I have a dotnet core application created with Angular template that communicates with a postgresql database.

On my local machine, I run the following command on my terminal to run the database container:

docker run -p 5432:5432 --name accman-postgresql -e POSTGRES_PASSWORD=mypass -d -v 'accman-postgresql-volume:/var/lib/postgresql/data' postgres:10.4

And then by pressing F5 in VsCode, I see that my application works great.

To dockerise my application, I added this file to the root of my application.

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env

# install nodejs for angular, webpack middleware
RUN apt-get update  
RUN apt-get -f install  
RUN apt-get install -y wget  
RUN wget -qO- https://deb.nodesource.com/setup_11.x | bash -  
RUN apt-get install -y build-essential nodejs

WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Web.dll"]

Now I think I have to create a docker-compose file. Would you please help me on creating my docker-compose.yml file?

Thanks,

1

1 Answers

5
votes

I figured it out, here is my final version of docker-compose.yml file:

version: '3'
services:
  web:
    container_name: 'accman-web-app'
    image: 'accman-web'
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '8090:80'
    depends_on:
      - 'postgres'
    networks:
      - accman-network  

  postgres:
    ports:
      - '5432:5432'
    container_name: accman-postgresql
    environment:
      - POSTGRES_PASSWORD=mypass
    volumes:
      - 'accman-postgresql-volume:/var/lib/postgresql/data'
    image: 'postgres:10.4'
    networks:
      - accman-network

volumes:
  accman-postgresql-volume:

networks:
  accman-network:
    driver: bridge

You can use composerize to find out how you can add services to your docker-compose file.

Now you can run these following commands consecutively:

docker-compose build
docker-compose up

And voila!