0

I have activated my Docker Engine V1.41 REST API in docker with this article.

After that in my browser it works fine like http://localhost:port/container/list will show me all lists of my containers in json.

But now, when I try to reach the API with axios in react I get an CORS error: enter image description here

So I think I have to setup cors for docker?

I found a bunch of articles about how to enable CORS for a docker image / container but nothing about enabling it for the docker agent / host itself.

Any answer?

1
  • 2
    Why do your posts all contain a superfluous link to opstrainerz.com? Maybe you should be asking them for help and not S/O if their training is not clear? Is no one at opstrainerz "trained" well enough to answer? Are you misleadingly trying to drive traffic to the site? Do you have an association to the site?
    – Ian W
    Jan 5, 2021 at 4:26

1 Answer 1

2

CORS is applied at the application layer, not the infrastructure (docker) layer. So, this has nothing to do with docker. It also has nothing to do with the axios client. Rather, your server needs to allow requests from your client by returning the correct headers via the OPTIONS method.

If you're using nodejs and express, you can add something like this to your service:

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Methods", "GET");

  if (req.method === "OPTIONS") return res.status(200).json({});

  next();
});

Or, there are plenty of CORS libraries that can help you out.

1
  • I think the question is about controlling the local docker deamon with a webapplication. Your answer can't be applied here, because in this case docker is the "application layer"..... I'm still looking for an answer to the same question
    – ehhc
    Sep 4, 2023 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.