Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Docker container won't start

Created a simple application that connects to PostgreSQL but when I containerized it using Docker i cant seem to start it and no port/s were shown when i ran the container. What seems to be the problem?

The application works without the Docker but when I containerize it, that's where the problem arises.

docker build is successful and when I use docker run

it gives me a container but when I check it using docker ps -a no ports where shown

This is what I did on docker terminal

and this is my code when connecting to PostgreSQL db

class Program
{
    static void Main(string[] args)
    {
        using (var connection = new NpgsqlConnection("Host=localhost;Username=postgres;Password=password;Database=user"))
        {
            connection.Open();
            connection.Execute("Insert into customer (name) values ('Mikolasola');");
            var value = connection.Query<string>("Select name from customer;");
            Console.WriteLine(value.First());
        }

        Console.Read();
    }
}

here's my docker file

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

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

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

Edit: Changed my Docker file to this and somehow I can get the port now

FROM microsoft/dotnet:2-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -r linux-x64 -o out

FROM microsoft/dotnet:2-runtime-deps
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["./webapplication1"]

But I can't run it. Here's the screenshot

enter image description here

Any help would be much appreciated. Thanks! PS: im a newbie

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  1. If you're using Docker for Mac (or Docker for Windows), to access anything on the host, you need to use host.docker.internal. More on that here.
  2. Read the connection string from config and environment variables. That will allow you to override it when running the container.

PS: im a newbie

Pro tip for you :p

You might not want to use -d on the container you're working, so you see it's logs right there. Had you not used it, you'd have seen the process exit and not wondered why there's no port shown there :)


UPDATE:

  1. Use the Dockerfile from the original question (before the edit).
  2. I suggested you run it without -d:
    docker run -p 8080:80 --name test webapp1
    
    This will show you the aspnet app crash. You'll see what's happening. In this case since I know what's happening I can help, but in general you want to know what's happening. You do that by not running it in a detached state for small things like this, and using docker logs when there's a lot to parse. Anyway...
  3. ... you want the container to access the database on the host, not on its localhost. You do that by using host.docker.internal as the host. Eg:
    Host=host.docker.internal;Username=postgres;Password=password;Database=user
    
    This only works if you're using Docker for Windows or Docker for Mac. Doesn't work on Docker Toolbox or on linux AFAIK.
  4. Now you need to figure out a way your application to use host.docker.internal when running within docker and localhost otherwise. My suggestion was to read it from config.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!