4

I am working on microsoft translator and api is not working inside container. I am trying to set proxy server inside my docker container but it is not working I tried to run on PowerShell it works

[Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://1.1.1.1:3128", [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://1.1.1.1:3128", [EnvironmentVariableTarget]::Machine)

But when I tried to run same commands inside docker container it is not executing, it gave me error .

docker container exec -it microsofttranslator /bin/sh 

ERROR

/bin/sh: 1: Syntax error: word unexpected (expecting ")")
Muhammad Rashid
  • 563
  • 1
  • 6
  • 25
  • Your Dockerfile file could give us more clarity .. 1. whether which Linux flavor being used 2. Does it has bash or sh installed 3. what is the start script or CMD or ENTRYPOINT 4. Env variables for proxy is set or not – My IT GURU Feb 27 '22 at 20:23

4 Answers4

1

The error is because in your start script of docker container, your syntax cannot be executed by plain sh, you should use bash instead.

I have re-produced with a simple example.

cat sh_bash.sh
winner=bash_or_sh
if [[  ( $winner == "bash_or_sh" ) ]]
   then
      echo " bash is winner"
    else
      echo "sh is looser"
fi
$ sh sh_bash.sh
sh_bash.sh: 2: Syntax error: word unexpected (expecting ")")
$ bash sh_bash.sh
 bash is winner

So, try docker container exec -it microsofttranslator /bin/bash

Should you need to pass proxy env variables , please read this

My IT GURU
  • 144
  • 1
  • 7
0

There could be various reasons for it, Considering there is not much detail, I will point some of the common issues that might be there.

  1. If you are using any script in Dockerfile. Although your script can be run by sh however it might require bash. In such cases you might need to add/install bash in your dockerfile.
  2. Also there could be some syntax error. e.g. Some extra spaces done by editor.
  3. Ensure your editor is making sure that files that have been edited and uploaded from a Windows machine to a Linux machine work. If not please use some command like dos2unix on your files. If you are using windows, You can go through Notepad++, and ensure that Encoding is UTF-8 not UTF-8 BOM

And to run the docker container with proxy inside them. You can go through this solution. How to configure docker container proxy?

This is one of the common issue which might be causing this, otherwise there could be many other reasons.

axel
  • 21
  • 7
0

If you have a dockerfile, could you add these lines and give it a try

#Adding proxy 
ENV HTTP_PROXY "http://1.1.1.1:3128"
ENV HTTPS_PROXY "http://1.1.1.1:3128"
ENV NO_PROXY  ""  #if needed
Sandeepp
  • 24
  • 3
0

you can easily set up a proxy for your specific container or for all containers just by using these two environmental variables HTTP_PROXY and HTTPS_PROXY

1. For spcific container

Proxy For specific container using Dockerfile:

#Add these env vars to your dockerfile
ENV HTTP_PROXY="http://1.1.1.1:3128"
ENV HTTPS_PROXY="http://1.1.1.1:3128"

Proxy for specific container without defining them in Dockerfile:

docker run -d -e HTTP_PROXY="http://1.1.1.1:3128" -e HTTPS_PROXY="http://1.1.1.1:3128" image:tag

2. For all containers

You to execute bellow mentioned commands:

mkdir /etc/systemd/system/docker.service.d
vim /etc/systemd/system/docker.service.d/http-proxy.conf

Paste bellow mentioned content into the file and save it

[Service]
Environment="HTTP_PROXY=http://user01:password@10.10.10.10:8080/"
Environment="HTTPS_PROXY=https://user01:password@10.10.10.10:8080/"
Environment="NO_PROXY= hostname.example.com,172.10.10.10"

# reload the systemd daemon
systemctl daemon-reload
# restart docker
systemctl restart docker
# Verify that the configuration has been loaded
systemctl show docker --property Environment