Setup Docker Container to Use Charles Proxy on macOS

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives a way to setup a local Docker container to use local Charles Proxy as it's proxy network on macOS. Charles Proxy containers the auth and enterprise proxy info and forwards the traffic sent to it on default TCP port 8888. This is for all you lucky individuals stuck behind a network proxy. The purpose is to allow internet and internal network access from our containers. Such as installing packages with yum/apt-get, pulling binaries from S3/Artifactory or Github access.


Prerequisites


Create Unique Local IP on Loopback Interface on the Host (macOS)

The standard loopback IP or 'localhost' obviously won't work from the container because it would just look at it self. We could use our host IP, but then it's static to our system and if the IP changes it would fail. So we are going to assign an IP to the loopback that will be consistent.

  1. Open a terminal
  2. Add an IP alias to Loopback Interface
    sudo ifconfig lo0 inet alias 169.254.169.254/32
    


Set Host CLI to Use Alias (Optional)

Because Test Kitchen pulls the CLI proxy environment variables and likely we'd be setting configurations to use our environment variables for proxy so it's more dynamic. I find it's best practice to switch you CLI proxy environment variables to use the new IP Alias.

  1. Change Bash Profile to use 169.254.169.254 instead of localhost

Example

Add environment variables to ~/.bash_profile

# Show Current Settings
function proxy-show(){
  echo "ALL_PROXY = ($ALL_PROXY)"
  echo "HTTP_PROXY = ($HTTP_PROXY)"
  echo "http_proxy = ($http_proxy)"
  echo "HTTPS_PROXY = ($HTTPS_PROXY)"
  echo "https_proxy = ($https_proxy)"
  echo "ftp_proxy = ($ftp_proxy)"
  echo "rsync_proxy = ($rsync_proxy)"
  echo "no_proxy = ($no_proxy)"
  echo "NO_PROXY = ($NO_PROXY)"
}

# Enable Proxy
function proxy-on(){
  export ALL_PROXY=http://169.254.169.254:8888
  export HTTP_PROXY=$ALL_PROXY
  export http_proxy=$ALL_PROXY
  export HTTPS_PROXY=$ALL_PROXY
  export https_proxy=$ALL_PROXY
  export ftp_proxy=$ALL_PROXY
  export rsync_proxy=$ALL_PROXY
  export NO_PROXY=localhost,.local,169.254.,127.0.0.1,10.0.2.,/var/run/docker.sock,.bonusbits.com
  export no_proxy=$NO_PROXY
  proxy-show
}

# Disable Proxy
function proxy-off(){
  unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy ftp_proxy rsync_proxy ALL_PROXY NO_PROXY no_proxy
  proxy-show
}

# Turn on by default
proxy-on


Configure Container to Use Proxy

Manually

First, we would most likely manually configure the proxy on a container to test our setup works.

  1. Launch Container Interactive Shell
    docker run -it <imagename>:<tag> /bin/bash
    
  2. Set Proxy
    export http_proxy="http://169.254.169.254:8888"
    
    export https_proxy="http://169.254.169.254:8888"
    
    export no_proxy="localhost,.local,127.0.0.1,169.254.,10.0.2.,/var/run/docker.sock,.bonusbits.com"
    


Test Kitchen

Example where EC2 and Docker configured in same Kitchen configuration file.

cat /var/log/messages

Dockerfile


formatted text
cat /var/log/messages

Icon-Tip-Square-Green.png Gnome-sticky-notes-applet