[英]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. 创建了一个连接到PostgreSQL的简单应用程序,但是当我使用Docker对其进行容器化时,我似乎无法启动它,并且在运行容器时未显示任何端口。 What seems to be the problem?
似乎是什么问题?
The application works without the Docker but when I containerize it, that's where the problem arises. 该应用程序在没有Docker的情况下可以工作,但是当我对其进行容器化时,就会出现问题。
docker build
is successful and when I use docker run
docker build
成功,当我使用docker run
it gives me a container but when I check it using docker ps -a
no ports where shown 它给了我一个容器,但是当我使用
docker ps -a
检查它时,没有端口显示
This is what I did on docker terminal 这就是我在Docker终端上所做的
and this is my code when connecting to PostgreSQL db 这是我连接到PostgreSQL数据库时的代码
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 编辑:将我的Docker文件更改为此,现在我可以通过某种方式获取端口
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
这是屏幕截图
Any help would be much appreciated. 任何帮助将非常感激。 Thanks!
谢谢! PS: im a newbie
PS:我是新手
host.docker.internal
. host.docker.internal
。 More on that here . PS: im a newbie
PS:我是新手
Pro tip for you :p 专业提示给您:p
You might not want to use -d
on the container you're working, so you see it's logs right there. 您可能不想在正在使用的容器上使用
-d
,因此您会在此处看到它的日志。 Had you not used it, you'd have seen the process exit and not wondered why there's no port shown there :) 如果您不使用它,您将看到进程退出并且不知道为什么那里没有显示端口:)
UPDATE: 更新:
-d
: -d
情况下运行它: docker run -p 8080:80 --name test webapp1
This will show you the aspnet app crash. docker logs
when there's a lot to parse. docker logs
。 Anyway... host.docker.internal
as the host. host.docker.internal
作为主机来host.docker.internal
此操作。 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. host.docker.internal
when running within docker and localhost
otherwise. host.docker.internal
和localhost
运行时,应用程序使用host.docker.internal
方式。 My suggestion was to read it from config.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.