gpt4 book ai didi

node.js - 如何使用 google/node-runtime 镜像在 Docker 中运行 Ghost?

转载 作者:搜寻专家 更新时间:2023-11-01 00:43:35 24 4
gpt4 key购买 nike

我对 Docker、Ghost 和 Node 真的很陌生,所以请原谅我的无知。

我正在尝试基于 google/nodejs-runtime 图像为 Ghost 设置 Docker 图像/容器,但是当我通过 Docker 运行时无法连接到服务器。

一些细节:我在 OS X 上,所以我使用的是 boot2docker。我将 Ghost 作为 npm 模块运行,配置为使用端口 8080,因为这是 google/nodejs-runtime 所期望的。当我使用 npm start 时,此配置在 Docker 之外运行良好。我还在端口 8080 上尝试了一个简单的“Hello, World”Express 应用程序,它在 Docker 中运行。

我的目录结构是这样的:

  • 我的应用程序
    • 内容/
    • docker 文件
    • ghost_config.js
    • package.json
    • server.js

package.json

{
"name": "my_app",
"private": true,
"dependencies": {
"ghost": "0.5.2",
"express": "3.x"
}
}

Dockerfile

FROM google/nodejs-runtime

ghost_config.js

我将所有出现的端口 2368 更改为 8080。

server.js

// This Ghost server works with npm start, but not with Docker
var ghost = require('ghost');
var path = require('path');

ghost({
config: path.join(__dirname, 'ghost_config.js')
}).then(function (ghostServer) {
ghostServer.start();
});

// This "Hello World" app works in Docker
// var express = require('express');
// var app = express();

// app.get('/', function(req, res) {
// res.send('Hello World');
// });

// var server = app.listen(8080, function() {
// console.log('Listening on port %d', server.address().port);
// });

我使用 docker build -t my_app . 构建我的 Docker 镜像,然后使用 docker run -p 8080 my_app 运行它,将其打印到控制台:

> my_app@ start /app
> node server.js

Migrations: Up to date at version 003
Ghost is running in development...
Listening on 127.0.0.1:8080
Url configured as: http://localhost:8080
Ctrl+C to shut down

docker ps 输出:

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                     NAMES
4f4c7027f62f my_app:latest "/nodejs/bin/npm sta 23 hours ago Up About a minute 0.0.0.0:49165->8080/tcp pensive_lovelace

boot2docker ip 输出:

The VM's Host only interface IP address is: 192.168.59.103

所以我将我的浏览器指向:192.168.59.103:49165 并没有得到任何结果,Docker 日志中没有输出。就像我上面说的,在同一个 server.js 中运行“Hello World”应用程序工作正常。

在我看来一切都是正确的。我看到的唯一奇怪的事情是在 docker build 期间 sqlite3 在 npm install 中失败:

[sqlite3] Command failed: 
module.js:356
Module._extensions[extension](this, filename);
^
Error: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.14' not found

...

node-pre-gyp ERR! Testing pre-built binary failed, attempting to source compile

但源代码编译似乎成功了。

我希望我只是在这里做了一些愚蠢的事情。

最佳答案

在您的 ghost 配置中,将相关服务器主机更改为 0.0.0.0 而不是 127.0.0.1:

server: {
host: '0.0.0.0',
...
}

PS:SQLite 错误。试试这个 Dockerfile:

FROM phusion/baseimage:latest

# Set correct environment variables.
ENV HOME /root

# Regenerate SSH host keys. baseimage-docker does not contain any, so you
# have to do that yourself. You may also comment out this instruction; the
# init system will auto-generate one during boot.
RUN /etc/my_init.d/00_regen_ssh_host_keys.sh

# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]

# ...put your own build instructions here...
# Install Node.js and npm
ENV DEBIAN_FRONTEND noninteractive
RUN curl -sL https://deb.nodesource.com/setup | sudo bash -
RUN apt-get install -y nodejs

# Copy Project Files
RUN mkdir /root/webapp
WORKDIR /root/webapp

COPY app /root/webapp/app
COPY package.json /root/webapp/
RUN npm install

# Add runit service for Node.js app
RUN mkdir /etc/service/webapp
ADD deploy/runit/webapp.sh /etc/service/webapp/run
RUN chmod +x /etc/service/webapp/run

# Add syslog-ng Logentries config file
ADD deploy/syslog-ng/logentries.conf /etc/syslog-ng/conf.d/logentries.conf

# Expose Ghost port
EXPOSE 2368

# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

请注意,我使用 phusion/baseimage 而不是 google/nodejs-runtime 并安装了 node.js 和 npm:

ENV DEBIAN_FRONTEND noninteractive
RUN curl -sL https://deb.nodesource.com/setup | sudo bash -
RUN apt-get install -y nodejs

关于node.js - 如何使用 google/node-runtime 镜像在 Docker 中运行 Ghost?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com