Configuration script for using docker in proxy environment on ubuntu 20.04.1

Configuration script for using docker in proxy environment on ubuntu 20.04.1

I have created a script to set Use docker in proxy environment on ubuntu 20.04.1 in a batch.

How to use

  1. Save it as docker-proxy.py.
  2. Give execute permission with chmod + x docker-proxy.py.
  3. Run it as sudo docker-proxy.py proxy-url.

Where proxy-url is a string such as http://192.168.11.61:3128.

Script body

#!/usr/bin/python3

import string
import subprocess
import os
import sys

###########################################################################
#  template for /etc/apt/apt.conf
###########################################################################
template_apt_conf = """\
Acquire::http::Proxy "$proxy_url";
Acquire::https::Proxy "$proxy_url";
"""


###########################################################################
#  template for /etc/systemd/system/docker.service.d/override.conf
###########################################################################
template_docker_service_override = """\
[Service]
Environment="HTTP_PROXY=$proxy_url"
Environment="HTTPS_PROXY=$proxy_url"
Environment="NO_PROXY=localhost,127.0.0.1"
"""

###########################################################################
#  template for ~/.docker/config.json
###########################################################################
template_config_json = """\
{
  "proxies": {
    "default": {
      "httpProxy": "$proxy_url",
      "httpsProxy": "$proxy_url",
      "noProxy": "localhost,127.0.0.1"
    }
  }
}
"""


###########################################################################
#  write proxy data to a file
###########################################################################
def write_proxy(file_name, input_template, proxy_url):
    context = {
        'proxy_url' : proxy_url
    }
    template = string.Template(input_template)
    data = template.substitute(context)

    with open(file_name, "w") as fout:
        fout.write(data)
    print("wrote: " + file_name)

###########################################################################
#  get the home directory of the original user
###########################################################################
def get_original_home():
    user = os.environ['SUDO_USER']
    if user:
        home_dir = os.path.expanduser('~' + user)
    else:
        home_dir = os.path.expanduser('~')
    return home_dir

###########################################################################
#  write etc/apt/apt.conf
###########################################################################
def write_apt_conf(proxy_url):
    write_proxy('/etc/apt/apt.conf', template_apt_conf, proxy_url)

###########################################################################
#  write /etc/systemd/system/docker.service.d/override.conf
###########################################################################
def write_docker_service_override(proxy_url):
    docker_service_d = '/etc/systemd/system/docker.service.d'
    if not os.path.isdir(docker_service_d):
        os.mkdir(docker_service_d)

    override_conf = os.path.join(docker_service_d, "override.conf")
    write_proxy(override_conf, template_docker_service_override, proxy_url)

###########################################################################
#  write ~/.docker/config.json
###########################################################################
def write_docker_config(proxy_url):
    # get the path of ~/.docker
    home_dir = get_original_home()
    docker_dir = os.path.join(home_dir, ".docker")

    # create ~/.docker
    if not os.path.isdir(docker_dir):
        os.mkdir(docker_dir)

    # get ~/.docker/config.json
    config_json = os.path.join(docker_dir, "config.json")
    write_proxy(config_json, template_config_json, proxy_url)

###########################################################################
#  usage
###########################################################################
def usage():
    print("usage: sudo docker-proxy.py proxy-url")
    print("example: sudo docker-proxy.py http://192.168.11.61:3128")
    
def exec(command):
    subprocess.call(command.split(), shell=False)
    print("ran: " + command)

if __name__ == '__main__':
    if len(sys.argv) != 2 or os.getuid() != 0:
        usage()
        sys.exit(1)

    proxy_url = sys.argv[1]

    exec('apt install -y docker.io docker-compose')

    write_apt_conf(proxy_url)
    write_docker_service_override(proxy_url)
    write_docker_config(proxy_url)

    exec('systemctl daemon-reload')
    exec('systemctl restart docker')

https://gist.github.com/m-tmatma/4f5d3e8233c1040fa453d5857326fd4c

Processing content

Install docker.

apt install -y docker.io docker-compose

Write the proxy settings to the following file. (Create a folder if necessary.)

Execute the following command to reflect the docker daemon configuration file.

Recommended Posts

Configuration script for using docker in proxy environment on ubuntu 20.04.1
Use docker in proxy environment on ubuntu 20.04.1
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Use MailHog for checking emails in the development environment (using Docker)
Try putting Docker in ubuntu on WSL
Note: nginx.conf settings for CORS in Exment on Lightsail + Docker (nginx) environment
Just install Laravel8 on docker in PHP8 environment
[Ubuntu 18.04] Environment construction for using PyTorch with RTX3090
Tips for using the Spotify app on Ubuntu
Support out of support in docker environment using centos6
Command memo to install xeyes in ubuntu docker environment
I tried using Docker Desktop for Windows on Windows 10 Home
[2021] Build a Docker + Vagrant environment for using React / TypeScript
Using Flutter on Ubuntu (Part 2)
Install Docker on Ubuntu Server 20.04
Oracle Java 8 on Docker Ubuntu
Install MAV Proxy on Ubuntu 18.04
Install docker and docker-compose on ubuntu in the shortest process
Build a browser test environment using Capybara in the Docker development environment
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Install Docker on Ubuntu and set up remote connection using tls
Using Azure IOT Hub on Ubuntu 20.10.
Environment construction with Docker for beginners
Try using Redmine on Mac docker
Install ngrok on ubuntu16.04 using Vagrant
Docker on NILFS2 filesystem (for raspbianOS)
Install laravel/Dusk in docker environment (laravel6)
Build a XAMPP environment on Ubuntu
Rails on Docker environment construction procedure
Jupyter's Docker environment for running TensorFlow
Using Docker on Windows10 Home WSL2
Build ffmpeg 4.3.1 on Ubuntu for Windows
Build Unity development environment on docker
Installing and using Ansible on Ubuntu 16.04
Check MySQL logs in Docker environment
[For beginners] Introducing the procedure to Hello, World in C/C ++ language using Visual Studio Code on Ubuntu
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
Put Zabbix in Ubuntu with Docker and monitor Docker on the same host
[Rails 5] Countermeasures for git commit failure from another branch in Docker environment
Create a web environment quickly using Docker
Alert slack with alert manager in Docker environment
WSL2 + Ubuntu 20.04 Install Vivado in LTS environment
[Docker] Use environment variables in Nginx conf
Run NordVPN on Docker (Windows) Ubuntu container
Support for gdb errors in Docker containers
Scraping with puppeteer in Nuxt on Docker.
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Edit Mysql with commands in Docker environment
Introducing Docker Desktop for Windows on WSL2
Proxy server with squid using docker image
[Ruby] Building a Ruby development environment on Ubuntu
Build an authentication proxy server using Docker
Microservices 101-I tried putting Docker on Ubuntu-
Batch implementation in RubyOnRails environment using Digdag
Build Redmine code reading environment on Docker
Show Better Errors in Rails + Docker environment
Build Cakephp environment from docker installation Ubuntu
Try the Docker environment on AWS ECS
Modern Java environment for Windows using Chocolatey