我想在 Google Cloud 上創建一個運行我的 docker 容器的虛擬機。我的影像在 Google 的 Artifact Registry 中,我可以將它部署到 VM,但它不允許網路請求,即使是本地的(我curl在本地從 SSH嘗試,但它不起作用)。容器是一個簡單的 Node.js 應用程式,它向任何請求回傳“Hi there”。
我認為問題在于在創建容器/運行映像時指定埠。在本地命令列上創建容器時,我只是使用-p運算子將埠發布到容器。但由于 Google 的 VM 從 Artifact Registry 獲取映像并在構建 VM 時自動創建容器,因此我無需運行-p標記。
我認為該解決方案可能與 Cloud Console 中的“命令”選項有關(見下文),但
編輯:日志
好吧,日志比我想象的更有趣。我是對的,最初創建了一個 docker 容器。

但是這個容器很快就被洗掉了,新的容器出現了,它的 id 不同,每隔一段時間就會重新啟動。

編輯2:
Dockerfile:
FROM node:latest
WORKDIR /untitled
COPY package.json .
RUN npm install
EXPOSE 3000
COPY . ./
CMD node server.js
我的專案名為“無題”。這是一個測驗影像,只是為了讓一切正常。
實際的 Node.js 代碼是這樣的:
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send("Hi there!")
})
app.listen(port, () => console.log("Listening On Port 3000"))
我的 Package.json 是
{
"name": "untitled",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.2"
}
}
I'm sure there's something wrong with what I'm doing but I doubt it's the source code--it doesn't exit when I run a local docker container.
uj5u.com熱心網友回復:
IIRC 使用gcloud compute create-with-container將容器系結到主機的網路。因此,容器公開的任何埠都應該在主機上的(相同)埠上可用。
你能確認你的容器正在虛擬機上運行嗎?SSH 進入然后docker container ls(就像你在本地一樣)。
您能否提供有關您的curl命令和回應的更多資訊。
您應該能夠sudo journalctl查看 VM 的啟動情況并確定它啟動容器時遇到的任何問題。您可能需要具體查看konlet-startup和docker單位,例如sudo journalctl --unit=konlet-startup,注意它是否啟動了一個容器(希望您的容器 [ID])。
另一件要嘗試的事情是ss虛擬機的(tcp 監聽)埠。其中之一應該與您的容器相匹配:
sudo ss --tcp --listening --processes
更新
PROJECT=...
ZONE=...
gcloud compute instances create-with-container test \
--container-image=gcr.io/kuar-demo/kuard-amd64:blue \
--image-family=cos-stable \
--image-project=cos-cloud \
--machine-type=f1-micro \
--zone=${ZONE} \
--project=${PROJECT}
gcloud compute ssh ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}
在虛擬機上:
docker container ls --format="{{.ID}}\t{{.Image}}"
521d11ace93b gcr.io/kuar-demo/kuard-amd64:blue
11ae194b354e gcr.io/stackdriver-agents/stackdriver-logging-agent:1.8.9
curl \
--silent \
--output /dev/null \
--write-out '%{response_code}' \
localhost:8080
200 # Success
# Matches `kuard` container
sudo journalctl --unit=konlet-startup
Starting a container with ID: 521d11ace93b...
# http-alt == 8080
sudo ss --tcp --listening --processes
Local Address:Port Process
*:http-alt users:(("kuard",pid=893,fd=3))
# Using pid from ss command
ps aux | grep 893
893 /kuard
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/397270.html
