프록시 뒤의 dockerfile에서`apt-get`을 어떻게 실행합니까?
Docker (버전 0.8.1, 빌드 a1598d1)가있는 가상 머신 (Ubuntu 13.10)을 실행하고 있습니다. dockerfile로 이미지를 만들려고합니다. 먼저 패키지를 업데이트하고 싶지만 (프록시가 난독 화됨) 아래 코드를 사용하여 apt-get
시간 초과됩니다 Could not resolve 'archive.ubuntu.com'
.
FROM ubuntu:13.10
ENV HTTP_PROXY <HTTP_PROXY>
ENV HTTPS_PROXY <HTTPS_PROXY>
RUN export http_proxy=$HTTP_PROXY
RUN export https_proxy=$HTTPS_PROXY
RUN apt-get update && apt-get upgrade
또한 호스트 시스템에서 다음을 실행했습니다.
sudo HTTP_PROXY=http://<PROXY_DETAILS>/ docker -d &
호스트는 apt-get
문제없이 실행할 수 있습니다.
컨테이너 내에서 우분투 서버에 도달 할 수 있도록 dockerfile을 어떻게 변경할 수 있습니까?
최신 정보
나는 (변화에 CentOS에서 코드를 실행 FROM ubuntu:13.10
에 FROM centos
) 그리고 벌금을했다. Ubuntu에 문제가있는 것 같습니다.
업데이트 :
ENV에서 환경 변수의 대문자가 잘못되었습니다. 올바른 것은입니다 http_proxy
. 귀하의 예는 다음과 같아야합니다.
FROM ubuntu:13.10
ENV http_proxy <HTTP_PROXY>
ENV https_proxy <HTTPS_PROXY>
RUN apt-get update && apt-get upgrade
또는
FROM centos
ENV http_proxy <HTTP_PROXY>
ENV https_proxy <HTTPS_PROXY>
RUN yum update
ENV에 지정된 모든 변수는 모든 RUN 명령 앞에 추가됩니다. 모든 RUN 명령은 자체 컨테이너 / 환경에서 실행되므로 이전 RUN 명령의 변수를 상속하지 않습니다!
참고 :이 작업을 위해 프록시를 사용하여 docker 데몬을 호출 할 필요가 없습니다. 이미지 등을 가져 오려면 docker deamon에 대한 프록시도 설정해야합니다. /etc/default/docker
Ubuntu 에서 데몬 용 프록시를 설정할 수 있습니다 (컨테이너 설정에는 영향을주지 않음).
또한 호스트 (예 : localhost, 127.0.0.1) 에서 프록시 를 실행 하는 경우에도 발생할 수 있습니다 . 호스트의 로컬 호스트가 컨테이너의 로컬 호스트와 다릅니다. 이 경우 다른 IP (예 : 172.17.42.1)를 사용하여 프록시를 바인딩해야하거나 0.0.0.0에 바인딩하는 경우 .NET Framework 동안 컨테이너에서 연결하는 데 127.0.0.1 대신 172.17.42.1을 사용할 수 있습니다 docker build
.
여기에서 예를 찾을 수도 있습니다. 캐시를 사용하여 dockerfile을 빠르게 재 구축하는 방법?
2018 년 2 월 10 일 업데이트
docker option의 새로운 기능을 사용하면 --config
더 이상 Dockerfile에서 프록시를 설정할 필요가 없습니다. 회사 환경 안팎에서 동일한 Dockerfile을 사용할 수 있습니다.
--config string Location of client config files (default "~/.docker")
또는 환경 변수 DOCKER_CONFIG
`DOCKER_CONFIG` The location of your client configuration files.
$ export DOCKER_CONFIG=~/.docker
https://docs.docker.com/engine/reference/commandline/cli/
https://docs.docker.com/network/proxy/
내가 설정 프록시 추천 httpProxy, httpsProxy, ftpProxy
과 noProxy
(공식 문서는 변수 그리워 ftpProxy
가끔 유용하다)
{
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:3001",
"httpsProxy": "http://127.0.0.1:3001",
"ftpProxy": "http://127.0.0.1:3001",
"noProxy": "*.test.example.com,.example2.com"
}
}
}
필요한 경우 프록시 IP 및 포트를 조정하고 ~/.docker/config.json
제대로 설정 한 후에는 도커 빌드를 실행하고 도커를 정상적으로 실행할 수 있습니다.
$ docker build -t demo .
$ docker run -ti --rm demo env|grep -ri proxy
(standard input):http_proxy=http://127.0.0.1:3001
(standard input):HTTPS_PROXY=http://127.0.0.1:3001
(standard input):https_proxy=http://127.0.0.1:3001
(standard input):NO_PROXY=*.test.example.com,.example2.com
(standard input):no_proxy=*.test.example.com,.example2.com
(standard input):FTP_PROXY=http://127.0.0.1:3001
(standard input):ftp_proxy=http://127.0.0.1:3001
(standard input):HTTP_PROXY=http://127.0.0.1:3001
이전 답변 (해제 됨)
Dockerfile의 아래 설정이 저에게 효과적입니다. CoreOS
, Vagrant
및 에서 테스트했습니다 boot2docker
. 프록시 포트가3128
Centos에서 :
ENV http_proxy=ip:3128
ENV https_proxy=ip:3128
Ubuntu에서 :
ENV http_proxy 'http://ip:3128'
ENV https_proxy 'http://ip:3128'
형식에주의하세요. 일부는 http가 있고 일부는없는, 일부는 단일 할당량이 있습니다. IP 주소가 192.168.0.193이면 설정은 다음과 같습니다.
Centos에서 :
ENV http_proxy=192.168.0.193:3128
ENV https_proxy=192.168.0.193:3128
Ubuntu에서 :
ENV http_proxy 'http://192.168.0.193:3128'
ENV https_proxy 'http://192.168.0.193:3128'
Coreos에서 프록시 설정이 필요한 경우 (예 : 이미지 가져 오기)
cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.0.193:3128"
--build-arg
Dockerfile을 사용하여 빌드하려는 경우이 옵션을 사용할 수 있습니다 .
https://github.com/docker/docker/issues/14634 의 링크에서 "여러 HTTP_PROXY를 사용하여 --build-arg로 빌드"섹션을 참조하십시오.
[root@pppdc9prda2y java]# docker build
--build-arg https_proxy=$HTTP_PROXY --build-arg http_proxy=$HTTP_PROXY
--build-arg HTTP_PROXY=$HTTP_PROXY --build-arg HTTPS_PROXY=$HTTP_PROXY
--build-arg NO_PROXY=$NO_PROXY --build-arg no_proxy=$NO_PROXY -t java .
참고 : 자체 시스템에서 HTTP_PROXY 및 NO_PROXY 환경 변수를 설정했는지 확인하십시오.
Dockerfile의 apt-get 명령 앞에 다음 줄을 넣어야합니다.
COPY apt.conf /etc/apt/apt.conf
Dont'f forget to create apt.conf in the same folder that you have the Dockerfile, the content of the apt.conf file should be like this:
Acquire::socks::proxy "socks://YOUR-PROXY-IP:PORT/";
Acquire::http::proxy "http://YOUR-PROXY-IP:PORT/";
Acquire::https::proxy "http://YOUR-PROXY-IP:PORT/";
if you use username and password to connect to your proxy then the apt.conf should be like as below:
Acquire::socks::proxy "socks://USERNAME:PASSWORD@YOUR-PROXY-IP:PORT/";
Acquire::http::proxy "http://USERNAME:PASSWORD@YOUR-PROXY-IP:PORT/";
Acquire::https::proxy "http://USERNAME:PASSWORD@YOUR-PROXY-IP:PORT/";
for example :
Acquire::https::proxy "http://foo:bar@127.0.0.1:8080/";
Where the foo is the username and bar is the password.
Use --build-arg in lower case environment variable:
docker build --build-arg http_proxy=http://proxy:port/ --build-arg https_proxy=http://proxy:port/ --build-arg ftp_proxy=http://proxy:port --build-arg no_proxy=localhost,127.0.0.1,company.com -q=false .
i had the same problem and found another little workaround: i have a provisioner script that is added form the docker build environment. In the script i set the environment variable dependent on a ping check:
Dockerfile:
ADD script.sh /tmp/script.sh
RUN /tmp/script.sh
script.sh:
if ping -c 1 ix.de ; then
echo "direct internet doing nothing"
else
echo "proxy environment detected setting proxy"
export http_proxy=<proxy address>
fi
this is still somewhat crude but worked for me
As suggested by other answers, --build-arg
may be the solution. In my case, I had to add --network=host
in addition to the --build-arg
options.
docker build -t <TARGET> --build-arg http_proxy=http://<IP:PORT> --build-arg https_proxy=http://<IP:PORT> --network=host .
If you have the proxies set up correctly, and still cannot reach the internet, it could be the DNS resolution. Check /etc/resolve.conf
on the host Ubuntu VM. If it contains nameserver 127.0.1.1
, that is wrong.
Run these commands on the host Ubuntu VM to fix it:
sudo vi /etc/NetworkManager/NetworkManager.conf
# Comment out the line `dns=dnsmasq` with a `#`
# restart the network manager service
sudo systemctl restart network-manager
cat /etc/resolv.conf
Now /etc/resolv.conf
should have a valid value for nameserver, which will be copied by the docker containers.
We are doing ...
ENV http_proxy http://9.9.9.9:9999
ENV https_proxy http://9.9.9.9:9999
and at end of dockerfile ...
ENV http_proxy ""
ENV https_proxy ""
This, for now (until docker introduces build env vars), allows the proxy env vars to be used for the build ONLY without exposing them
The alternative to solution is NOT to build your images locally behind a proxy but to let docker build your images for you using docker "automated builds". Since docker is not building the images behind your proxy the problem is solved. An example of an automated build is available at ...
https://github.com/danday74/docker-nginx-lua (GITHUB repo)
https://registry.hub.docker.com/u/danday74/nginx-lua (DOCKER repo which is watching the github repo using an automated build and doing a docker build on a push to the github master branch)
and If you want to set proxy for wget you should put these line in your Dockerfile
ENV http_proxy YOUR-PROXY-IP:PORT/
ENV https_proxy YOUR-PROXY-IP:PORT/
ENV all_proxy YOUR-PROXY-IP:PORT/
As Tim Potter pointed out, setting proxy in dockerfile is horrible. When building the image, you add proxy for your corporate network but you may be deploying in cloud or a DMZ where there is no need for proxy or the proxy server is different.
Also, you cannot share your image with others outside your corporate n/w.
A slight alternative to the answer provided by @Reza Farshi (which works better in my case) is to write the proxy settings out to /etc/apt/apt.conf
using echo
via the Dockerfile e.g.:
FROM ubuntu:16.04
RUN echo "Acquire::http::proxy \"$HTTP_PROXY\";\nAcquire::https::proxy \"$HTTPS_PROXY\";" > /etc/apt/apt.conf
# Test that we can now retrieve packages via 'apt-get'
RUN apt-get update
The advantage of this approach is that the proxy addresses can be passed in dynamically at image build time, rather than having to copy the settings file over from the host.
e.g.
docker build --build-arg HTTP_PROXY=http://<host>:<port> --build-arg HTTPS_PROXY=http://<host>:<port> .
as per docker build docs.
참고URL : https://stackoverflow.com/questions/22179301/how-do-you-run-apt-get-in-a-dockerfile-behind-a-proxy
'developer tip' 카테고리의 다른 글
jQuery의 keydown () 이벤트 핸들러에서 누른 문자를 디코딩하는 방법 (0) | 2020.11.16 |
---|---|
gradle의 versionNameSuffix에 날짜 빌드를 추가하는 방법 (0) | 2020.11.16 |
JavaScript : 정의되지 않음! == 정의되지 않음? (0) | 2020.11.16 |
Android Eclipse 플러그인 : Instrumentation Test Runner가 지정되지 않음 (0) | 2020.11.16 |
상태 표시 줄의 높이? (0) | 2020.11.16 |