首页 > 解决方案 > 在 arm64 docker 容器中运行 c++ 应用程序时出现错误“getsockopt level=1 optname=20 not yet supported”

问题描述

我使用实现 HTTPS POST 请求的 Poco 库创建了以下 c++ 程序。

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <Poco/File.h>
#include <iostream>
#include <string>
#include <stdio.h>

using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::URI;
using Poco::File;

using namespace std;
using namespace Poco;
using namespace Poco::Net;


int main()

{
    try
    {
    URI uri = URI("https://example.com/login");
    string path(uri.getPathAndQuery());
    if (path.empty())
        path = "/";
    Context::Ptr ctx = new Context(Context::TLSV1_2_CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    HTTPSClientSession session(uri.getHost(), uri.getPort(), ctx);
    HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
    std::string body("this is a random request body\r\n0\r\n");
    request.setContentLength((int) body.length());
    session.sendRequest(request) << body;
    HTTPResponse response;
    istream &is = session.receiveResponse(response);
    string responseStr;
    StreamCopier::copyToString(is, responseStr);
    cout << "response is " << responseStr << endl;
    }
    catch (Poco::Exception& exc)
    {
        cout << "Poco Exception occured during https request." << endl;
        cout << "message: " <<  exc.message() << endl;
    }

}

当我为我的 Ubuntu 20.04 amd64 架构构建它时,它工作正常并且响应是正确的

response is <?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>404 - Not Found</title>
    </head>
    <body>
        <h1>404 - Not Found</h1>
        <script type="text/javascript" src="//wpc.75674.betacdn.net/0075674/www/ec_tpm_bcon.js"></script>
    </body>
</html>

之后,我为 arm64 arch 构建了它,并尝试根据我在 Dockerfile 之后构建的映像在 docker 容器中运行它:

FROM arm64v8/ubuntu:20.04
WORKDIR /opt/myapp
ENV DEBIAN_FRONTEND=noninteractive
COPY myapp .
RUN apt-get clean
RUN apt-get update
RUN apt-get install libssl-dev nano iputils-ping curl -y 
# cleanup
RUN apt-get -qy autoremove
RUN echo "/opt/myapp/libs" >> /etc/ld.so.conf  && \
    ldconfig
EXPOSE 2
EXPOSE 8022
EXPOSE 443
EXPOSE 80
CMD ["bash"]

之后我使用以下命令运行容器

docker run -v /usr/bin/qemu-aarch64-static:/usr/bin/qemu-aarch64-static -i -t --platform arm64 myapp_image

程序运行到session.sendRequest(request) << body; 终端给出以下错误的行:

getsockopt level=1 optname=20 not yet supported

捕获的异常是:

Net Exception: Operation not supported 

另一方面,当我在容器内给出命令时:

curl -d '{json}' -H 'Content-Type: application/json' https://example.com/login

我得到与 amd64 arch case 相同的打印输出:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>404 - Not Found</title>
    </head>
    <body>
        <h1>404 - Not Found</h1>
        <script type="text/javascript" src="//wpc.75674.betacdn.net/0075674/www/ec_tpm_bcon.js"></script>
    </body>
</html>

我使用的 gcc/g++ 和 aarch64-linux-gnu-g++/aarch64-linux-gnu-gcc 编译器都使用多架构功能构建相同版本 9.3.0 的位置。

有谁知道我可能做错了什么?

标签: c++linuxdockerarm64poco-libraries

解决方案


推荐阅读