简体   繁体   English

Docker容器无法启动

[英]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:我是新手

  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 . 如果要使用Mac的Docker(或Windows的Docker),要访问主机上的任何内容,则需要使用host.docker.internal More on that here . 这里更多。
  2. Read the connection string from config and environment variables. 从config和环境变量中读取连接字符串。 That will allow you to override it when running the container. 这将允许您在运行容器时覆盖它。

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: 更新:

  1. Use the Dockerfile from the original question (before the edit). 使用原始问题中的Dockerfile(在编辑之前)。
  2. I suggested you run it without -d : 我建议您在不使用-d情况下运行它:
     docker run -p 8080:80 --name test webapp1 
    This will show you the aspnet app crash. 这将向您显示aspnet应用程序崩溃。 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. 您可以通过以下方式来做到这一点:在诸如此类的小事中,不要以分离状态运行它,并在需要解析的地方使用docker logs 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. 您可以使用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. 仅当您使用Windows的Docker或Mac的Docker时,此方法才有效。 Doesn't work on Docker Toolbox or on linux AFAIK. 在Docker Toolbox或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. 现在,您需要找出在host.docker.internallocalhost运行时,应用程序使用host.docker.internal方式。 My suggestion was to read it from config. 我的建议是从配置中读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM