在 Docker 上向节点服务器 运行 发送 GET 方法请求

Sending a GET method request to a node server running on Docker

我目前正在 运行 使用 Docker 连接节点服务器,它将与已上传的智能合约进行交互。这是我的 Docker 文件:

FROM node:7
WORKDIR /app
COPY package.json /app
RUN npm install
RUN npm install -g npm
RUN ls
COPY . /app
CMD node gameserver.js
EXPOSE 8081
ARG walletPrivate
ENV walletPrivate = $private
ARG walletPublic
ENV walletPublic = $public

我正在定义我将在 运行 时间传递的变量。这是服务器代码:

const express = require('express');
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const MissionTrackerJson = require('./contracts/MissionTracker.json');

const app = express();
const port = process.env.PORT || 5001;
const providerUrl = 'https://rinkeby.infura.io/N9Txfkh1TNZhoeKXV6Xm';

const gamePublicKey = process.env.public;
const gamePrivateKey = process.env.private;
const production = true;

let contractAddress = null;
let contract = null;

let web3 = new Web3(new Web3.providers.HttpProvider(providerUrl));
if (typeof web3 === 'undefined') throw 'No web3 detected. Is Metamask/Mist being used?';
console.log("Using web3 version: " + Web3.version);

let contractDataPromise = MissionTrackerJson;
let networkIdPromise = web3.eth.net.getId(); // resolves on the current network id

Promise.all([contractDataPromise, networkIdPromise])
.then(results => {
    let contractData = results[0];
    let networkId = results[1];

    // Make sure the contract is deployed on the connected network
    if (!(networkId in contractData.networks)) {
        throw new Error("Contract not found in selected Ethereum network on MetaMask.");
    }

    contractAddress = contractData.networks[networkId].address;
    contract = new web3.eth.Contract(contractData.abi, contractAddress);
    app.listen(port, () => console.log(`Site server on port ${port}`));
})
.catch(console.error);

if (production) {
    app.use('/', express.static(`${__dirname}/client/build`));
}

app.get('/api/complete_checkpoint/:reviewer/:checkpoint', (req, res) => {
    let reviewerId = req.params.reviewer;
    let checkpointId = req.params.checkpoint;
    let encodedABI = contract.methods.setCheckpointComplete(reviewerId, checkpointId).encodeABI();

    web3.eth.getTransactionCount(gamePublicKey, 'pending')
    .then(nonce => {
        let rawTx = {
            from: gamePublicKey,
            to: contractAddress,
            gas: 2000000,
            data: encodedABI,
            gasPrice: '100',
            nonce,
        };

        let tx = new Tx(rawTx);
        tx.sign(gamePrivateKey);

        let serializedTx = tx.serialize();

        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .on('receipt', console.log)
        .catch(console.error);
    })
});

app.get('/api/add_checkpoint/:checkpoint_name', (req, res) => {
    console.log("hello");
    let checkpointName = decodeURIComponent(req.params.checkpoint_name);
    let encodedABI = contract.methods.addGameCheckpoint(checkpointName).encodeABI();

    web3.eth.getTransactionCount(gamePublicKey, 'pending')
    .then(nonce => {
        let rawTx = {
            from: gamePublicKey,
            to: contractAddress,
            gas: 2000000,
            data: encodedABI,
            gasPrice: '100',
            nonce,
        };

        let tx = new Tx(rawTx);
        tx.sign(gamePrivateKey);

        let serializedTx = tx.serialize();

        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .on('receipt', console.log)
        .catch(console.error);
    })
});
console.log("end");

要调用合约,我需要使用 HTTP GET 方法 ping 服务器。当我 运行 以下命令时,我发现我的 Docker 服务器的 IP 地址是 172.17.0.2:8081:

docker run -t -i --env private=0x12345 --env public=0x11111 -p 8081:8081 game_server

我正在制作我的外向端口 8081

如何将 HTTP GET 方法发送到我的服务器?我应该寻找其他地址吗?

非常感谢您的帮助!

我觉得你的 Dockerfile:

有点乱

FROM node:7 WORKDIR /app COPY package.json /app <== this is copying package.json to /app/app...should just be COPY package.json . RUN npm install RUN npm install -g npm <== unnecessary as npm bundles with node RUN ls <== unnecessary COPY . /app <== again, you're copying to /app/app CMD node gameserver.js <== this should go as the last line EXPOSE 8081 <== this should be the second to last line ARG walletPrivate ENV walletPrivate = $private ARG walletPublic ENV walletPublic = $public

当您 运行 您的容器时,您是否看到它确实在工作?既然你有 /app/app 事情在进行,如果 CMD node gameserver.js 命令真的找到了你的服务器文件,我会感到惊讶。

此外,您的 gameserver.js 文件需要一个您未提供的 PORT 环境变量。由于未提供,您的节点服务器正在侦听端口 5001,而不是 8081...因此,公开 8081 对您没有帮助。不要从容器中公开 8081,而是将 -p "8081:5001" 添加到 运行 命令中。或者,提供 8081 作为环境变量。

希望对您有所帮助。