3

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

tad00
  • 51
  • 1
  • 7
  • 1
    `"Host=localhost;` - presumably your Postgres instance is also running within the same Docker container (i.e. what the container sees as "localhost")? – ProgrammingLlama Jun 07 '19 at 14:53
  • @John so do i need to stop postgresql from running in the services? or do i need to set an ip address for the container? – tad00 Jun 07 '19 at 15:01
  • Show your dockerfile, are you using docker-compose? Where is the actual database you are connecting to, the host, another container, or the container with your program? – zero298 Jun 07 '19 at 15:05
  • I mean that `localhost` in the container = this container, not the physical machine. – ProgrammingLlama Jun 07 '19 at 15:06
  • @zero298 nope. I'm connecting to the host. Just edited my post and added the dockerfile – tad00 Jun 07 '19 at 15:09
  • @John oh okay but how do i run it separetly? Im really sorry if i dont get it right away. Im kinda confused with whats going on – tad00 Jun 07 '19 at 15:15
  • Are you running Postgres in docker? – ProgrammingLlama Jun 07 '19 at 15:17
  • @John no sir. My goal is to containerize my c# application and get it to connect to local database so my application is now running via docker but it connects to the database outside the docker container – tad00 Jun 07 '19 at 15:20
  • Connecting to the host db is non-trivial. You can try switching to host networking for your scenario. See [this question](https://stackoverflow.com/q/22944631/596285) for options others have used. There are no ports for a stopped container, but there are logs visible with `docker logs $container_id`. – BMitch Jun 07 '19 at 16:53
  • Do you use docker for mac, windows or linux? Did the suggestion from @gldraphael work for you? – Edward Jun 10 '19 at 06:13
  • @TaoZhou I used docker for windows. Please check my recent edits. Thanks! – tad00 Jun 10 '19 at 12:14

1 Answers1

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.
galdin
  • 12,411
  • 7
  • 56
  • 71
  • Hey sorry for the late reply. I was able to show the port when I changed the docker file but I can't run since it gives an error: `Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"./webapplication1\": stat ./webapplication1: no such file or directory": unknown.` – tad00 Jun 10 '19 at 12:12
  • @tad00 I've updated my answer... are you using Docker toolbox by any chance? – galdin Jun 10 '19 at 17:57
  • ok im gonna try this. Yep i used docker toolbox and not docker for windows since its the only that is free. – tad00 Jun 10 '19 at 19:13
  • @tad00 docker for windows is free, but you need windows 10 pro. – galdin Jun 10 '19 at 19:31
  • oh okay but my OS rn is windows 10 home. Anyways, I tried it with `docker run -p 8080:80 --name test webapplication1` and it only outputs this message: `Did you mean to run dotnet SDK commands? Please install dotnet SDK from: https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409` – tad00 Jun 11 '19 at 10:43
  • @tad00 are you positive that the entry point is dotnet appname.dll? If you use dotnet run it'll need the SDK, but using the DLL directly doesn't need the sdk, just the runtime. – galdin Jun 11 '19 at 11:06