8

I have a ubuntu machine which is a VM where I have installed docker in it. I am using this machine from my local windows machine and doing ssh , opening the terminal to the ubuntu machine.

Now , I am going to take a docker image which contains all the necessary softwares for eg: apache installed in it. Later I am going to deploy a sample appication(which is a web applicationP on to it and save it .

Now , I am in a confused mode as in how to check the deployed application if its running properly. i.e., what would be the address of the container which containds the deployed application. for eg:- If I type http://127.x.x.x which is the address of the ubuntu machine , I am just getting time out .

Can anyone tell me how to verify the deployed application . Also, the printing the output of the program on the console works seemlessly fine , as the output gets printed , only thing I have some doubts is regarding the web application.

4

5 回答 5

8

有一些方法可以检查您的应用程序是否正在运行。

远程 API

正如JimiDini所说,一种可能性是 Docker 远程 API。您可以使用它来查看所有正在运行的容器(这将是您的用例,对吗?),检查某个容器或启动和停止容器。API 是一个 REST-API,具有多种编程语言绑定(位于https://docs.docker.io/reference/api/remote_api_client_libraries/)。其中一些非常过时。要在另一台机器上使用 Docker 远程 API,我需要显式打开它:

docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d &

请注意,API 现在向全世界开放!在真实场景中,您需要以某种方式对其进行保护(例如,参见http://java.dzone.com/articles/securing-docker%E2%80%99s-remote-api中的示例)。

码头工人 PS

查看所有正在运行的容器docker ps在您的主机上运行。这将列出所有正在运行的容器。如果您没有看到您的应用程序,则它没有运行。它还向您显示您的应用程序正在公开的端口。您也可以通过远程 API 执行此操作。

日志

您还可以检查日志。您可以运行docker attach <container id>以附加到某个容器并查看其标准输出。你也可以运行 rundocker logs <container id>来接收 Docker 日志。我更喜欢将日志写入某个目录,例如将所有日志写入/var/log该文件夹并将其挂载到我的主机上。然后,您的所有日志最终都会出现在/home/ubuntu/docker-logs您的主机上。

docker run -p 80:8080 -v /home/ubuntu/docker-logs:/var/log:rw my/application

端口和IP一言以蔽之

每个容器都会有自己的 IP 地址。您可以通过远程 API 或主机上的 Docker 直接检查此 IP 地址。您还可以为容器指定某个主机名(通过将 传递--hostname="test42"run命令)。但是,您大多不需要那个。

要访问容器中的应用程序,需要打开容器中的端口并绑定到主机上的端口。

在您的 Dockerfile 中,您需要EXPOSE运行应用程序的端口:

FROM ubuntu
...
EXPOSE 8080
CMD run-my-app.sh

启动容器时,需要将此端口绑定到主机的端口:

docker run -p 80:8080 my/application

现在您可以在http://localhost:80或上访问您的应用程序http://127.0.0.1:80

如果您的应用程序没有响应,请通过键入docker ps或远程 API 检查容器是否正在运行。如果它没有运行,请检查日志以了解原因。

(注意:如果您在 VirtualBox 之类的设备中运行 Ubuntu VM 并尝试从 Windows 机器访问它,请确保您也在 VirtualBox 中打开了端口!)。

于 2014-06-01T09:35:16.613 回答
6

Docker 容器有一个单独的 IP 地址。默认情况下它是私有的(只能从主机访问)。

Docker 通过其 API 提供所有元数据(包括 IP 地址):

  1. https://docs.docker.io/reference/api/docker_remote_api_v1.10/#inspect-a-container
  2. https://docs.docker.io/reference/api/docker_remote_api_v1.10/#monitor-docker-s-events

您还可以查看一个名为docker-gen的小工具以获得灵感。它监控 docker-events 并使用模板在主机上创建配置文件。

于 2014-06-01T09:10:41.007 回答
3

要获取 docker 容器的 ip 地址,如果你知道它的 id(一个长的十六进制字符串)或者你命名它:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-id-or-name>
于 2014-12-02T13:50:30.923 回答
1

Docker 正在运行自己的网络,要获取有关它的信息,您可以运行以下命令:
docker network ls
docker network inspect <network name>
docker inspect <container id>
在输出中,您应该能够找到 IP。

但是,关于 Dockerfile 和 docker 命令,您还需要注意一些事项run

  • 当您EXPOSE在 Dockerfile 中使用端口时,容器中的服务无法从 Docker 外部访问,但可以从其他 Docker 容器内部访问
  • 当你暴露和使用docker run -p ...标志时,容器中的服务可以从任何地方访问,甚至在 Docker 之外

例如,如果您的 apache 在端口 8080 上运行,您应该在 Dockerfile 中公开它,然后您可以将其运行为: docker run -d -p 8080:8080 <image name>并且您应该能够从您的主机上的 HTTP://localhost:8080 访问它。

这是一个古老的问题/答案,但它可能对其他人有所帮助;)

于 2020-12-04T21:30:34.243 回答
0

从 2020 年开始工作

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

于 2020-03-06T08:05:09.407 回答