I am trying to establish a websocket connection with a server written in C++ with websocketpp. I can successfully establish a connection with my server when it is running on my computer, but I am unable to connect with the same server when it is running in a Docker container on my local machine.
I spent a while looking up this issue online, and it seemed that the main pain points were to listen to 0.0.0.0
instead of 127.0.0.1
, and to use the -p
config instead of EXPOSE
in the Dockerfile. I think what I have now follows those two pieces of advice, but I am still unable to connect to the endpoint. I was pretty lost, so I figured I would ask here to see if anyone else ran into a similar issue.
My client looks like this (just from within the browser):
let ws = new WebSocket('ws://localhost:3001');
My server looks like this:
websocketpp::server<websocketpp::config::asio> server_;
server_.clear_access_channels(
websocketpp::log::alevel::frame_header | websocketpp::log::alevel::frame_payload
);
server_.set_reuse_addr(true);
server_.init_asio();
server_.listen(port_); // port_ is set to 3001
server_.start_accept();
server_.run();
and my Dockerfile looks like the following:
FROM gcc:latest
RUN set -ex; \
apt-get -y update; \
apt-get -y upgrade; \
apt-get install -y cmake build-essential libboost-dev \
libboost-system-dev libboost-thread-dev librange-v3-dev \
libfmt-dev libprotobuf-dev protobuf-compiler \
libwebsocketpp-dev libgtest-dev google-mock
# libgtest-dev only installs source files which need to be compiled -- actually compile them here
RUN cd /usr/src/googletest; \
cmake .; \
cmake --build . --target install
COPY . /usr/src/mazer
WORKDIR /usr/src/mazer
RUN cd /usr/src/mazer; \
mkdir build; \
cd build; \
cmake ..; \
make
CMD exec "./build/mazer-server/mazer-server"
I am starting the container with the following command:
docker run --rm -p 3001:3001 mazer-server