Using proxy in Dockerfile

Hello,
I have a Dockerfile like below:

FROM node:14-alpine
WORKDIR /usr/src/app

COPY package*.json /usr/src/app/
RUN npm install

COPY . /usr/src/app

ENV PORT 5000
EXPOSE $PORT
CMD [ "npm", "start" ]

I have a SOCKS5 proxy on the host and I want the RUN npm install command to download files through this proxy. Is this possible?

Thank you.

As I have already seen some posts from you here on the forum, I get the impression you are a little bit careless. It seems you are copy-pasting a lot of config pieces from the Internet (Stackoverflow?), without actually understanding and checking.

Please be aware that node 14 is end of life since last year. Please make sure you always run latest versions to avoid running any containers with open security issues. You don’t want any malicious actor taking over your server.


npm does not seem to support socks proxy, see issue. There are some workarounds discussed.

Alternatively you might try to configure Docker to use a proxy, see docs.

5 Likes

Hello,
Thank you so much for your reply.
No, I didn’t copy this Dockerfile from somewhere. When you create a project on the GitLab server from the templates, this file is generated.
Socksify tool? Can Proxychains be used?

I have added the following lines to the /lib/systemd/system/docker.service file for pulling images:

[Service]
Environment="HTTP_PROXY=socks5://127.0.0.1:<PROXY_PORT>"
Environment="HTTPS_PROXY=socks5://127.0.0.1:<PROXY_PORT>"
Replace 

But it doesn’t work for Dockerfile.

As @bluepuma77 said, Docker supports the use of proxies for building images (see docs).
What does not appear in the documentation, is that it is possible to clarify the proxy protocol.

For example: in the documentation it shows the following:

~/.docker/config.json.
{
 "proxies": {
   "default": {
     "httpProxy": "http://proxy.example.com:3128",
     "httpsProxy": "https://proxy.example.com:3129",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0.0/8"
   }
 }
}

You can change the protocol to use socks:

{
 "proxies": {
   "default": {
     "httpProxy": "socks5h://proxy.example.com:3128",
     "httpsProxy": "socks5h://proxy.example.com:3129",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0.0/8"
   }
 }
}

StackOverflow: Use socks5 proxy from host for docker build - Stack Overflow

3 Likes

Hi,
Thank you so much for your reply.
After this, does the RUN npm install command use a proxy to receive files?

Hello,
In my system, the proxy is running on the address 127.0.0.1:9050. Why is Docker’s IP address used in this example?

Thank you.

Hello,
I did the following:

{
        "proxies":
        {
            "default":
            {
                "httpProxy": "socks5h://127.0.0.1:<PROXY_PORT>",
                "httpsProxy": "socks5h://127.0.0.1:<PROXY_PORT>",
                "noProxy": ""
            }
        }
    }

And:

"httpProxy": "socks5h://172.16.0.1:<PROXY_PORT>"

None of them worked properly!