Setup Docker Container to Use Charles Proxy on macOS

From Bonus Bits
Jump to: navigation, search


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.


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

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 instead of localhost


Add environment variables to ~/.bash_profile

# Show Current Settings
function proxy-show(){
  echo "ALL_PROXY = ($ALL_PROXY)"
  echo "http_proxy = ($http_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=
  export http_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.,,10.0.2.,/var/run/docker.sock,
  export no_proxy=$NO_PROXY

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

# Turn on by default

Configure Container to Use Proxy


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=""
    export https_proxy=""
    export no_proxy="localhost,.local,,169.254.,10.0.2.,/var/run/docker.sock,"

Test Kitchen

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

cat /var/log/messages


formatted text
cat /var/log/messages

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