<p>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.</p>
<p>If you're using nodejs and express, you can add something like this to your service:</p>
<pre class="lang-javascript prettyprint-override"><code>app.use((req, res, next) =&gt; {
  res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
  res.header(&quot;Access-Control-Allow-Headers&quot;, &quot;Origin, X-Requested-With, Content-Type, Accept, Authorization&quot;);
  res.header(&quot;Access-Control-Allow-Methods&quot;, &quot;GET&quot;);

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

  next();
});
</code></pre>
<p>Or, there are plenty of CORS libraries that can help you out.</p>

CC BY-SA 4.0
2023-09-04T14:13:02.280Z
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
CC BY-SA 4.0
2021-01-04T13:13:51.607Z
2021-01-04T13:13:51.607Z
Software Engineer
<p>I have activated my Docker Engine V1.41 REST API in docker with <a href="https://blog.usejournal.com/how-to-enable-docker-remote-api-on-docker-host-7b73bd3278c6" rel="nofollow noreferrer">this article</a>.</p>
<p>After that in my browser it works fine like http://localhost:port/container/list will show me all lists of my containers in json.</p>
<p>But now, when I try to reach the API with axios in react I get an CORS error: <a href="https://i.sstatic.net/bSXFF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bSXFF.png" alt="enter image description here" /></a></p>
<p>So I think I have to setup cors for docker?</p>
<p>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.</p>
<p>Any answer?</p>

CC BY-SA 4.0
2021-01-05T04:26:26.680Z
Why do your posts all contain a superfluous link to [opstrainerz.com](http://opstrinerz.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
<docker><cors>
CORS in Docker Engine REST Api
likith sunny
# CORS in Docker Engine REST Api

(Asked by: username_0 on 2021-01-04)

I have activated my Docker Engine V1.41 REST API in docker with [this article](https://blog.usejournal.com/how-to-enable-docker-remote-api-on-docker-host-7b73bd3278c6).

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](https://i.sstatic.net/bSXFF.png)](https://i.sstatic.net/bSXFF.png)

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?

**Tags:** docker, cors

> **Comments:**
> **username_1**: Why do your posts all contain a superfluous link to [opstrainerz.com](http://opstrinerz.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?  

---

## 1 answers

### Answer by username_2 on 2021-01-04. Score: 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.

> **Comments:**
> **username_3**: 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  

---

