github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/docs/examples/apt-cacher-ng.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Dockerizing an apt-cacher-ng service" 4 description = "Installing and running an apt-cacher-ng service" 5 keywords = ["docker, example, package installation, networking, debian, ubuntu"] 6 [menu.main] 7 parent = "engine_dockerize" 8 +++ 9 <![end-metadata]--> 10 11 # Dockerizing an apt-cacher-ng service 12 13 > **Note**: 14 > - **If you don't like sudo** then see [*Giving non-root 15 > access*](../installation/binaries.md#giving-non-root-access). 16 > - **If you're using OS X or docker via TCP** then you shouldn't use 17 > sudo. 18 19 When you have multiple Docker servers, or build unrelated Docker 20 containers which can't make use of the Docker build cache, it can be 21 useful to have a caching proxy for your packages. This container makes 22 the second download of any package almost instant. 23 24 Use the following Dockerfile: 25 26 # 27 # Build: docker build -t apt-cacher . 28 # Run: docker run -d -p 3142:3142 --name apt-cacher-run apt-cacher 29 # 30 # and then you can run containers with: 31 # docker run -t -i --rm -e http_proxy http://dockerhost:3142/ debian bash 32 # 33 # Here, `dockerhost` is the IP address or FQDN of a host running the Docker daemon 34 # which acts as an APT proxy server. 35 FROM ubuntu 36 MAINTAINER SvenDowideit@docker.com 37 38 VOLUME ["/var/cache/apt-cacher-ng"] 39 RUN apt-get update && apt-get install -y apt-cacher-ng 40 41 EXPOSE 3142 42 CMD chmod 777 /var/cache/apt-cacher-ng && /etc/init.d/apt-cacher-ng start && tail -f /var/log/apt-cacher-ng/* 43 44 To build the image using: 45 46 $ docker build -t eg_apt_cacher_ng . 47 48 Then run it, mapping the exposed port to one on the host 49 50 $ docker run -d -p 3142:3142 --name test_apt_cacher_ng eg_apt_cacher_ng 51 52 To see the logfiles that are `tailed` in the default command, you can 53 use: 54 55 $ docker logs -f test_apt_cacher_ng 56 57 To get your Debian-based containers to use the proxy, you have following options 58 59 1. Add an apt Proxy setting 60 `echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/conf.d/01proxy` 61 2. Set an environment variable: 62 `http_proxy=http://dockerhost:3142/` 63 3. Change your `sources.list` entries to start with 64 `http://dockerhost:3142/` 65 4. Link Debian-based containers to the APT proxy container using `--link` 66 5. Create a custom network of an APT proxy container with Debian-based containers. 67 68 **Option 1** injects the settings safely into your apt configuration in 69 a local version of a common base: 70 71 FROM ubuntu 72 RUN echo 'Acquire::http { Proxy "http://dockerhost:3142"; };' >> /etc/apt/apt.conf.d/01proxy 73 RUN apt-get update && apt-get install -y vim git 74 75 # docker build -t my_ubuntu . 76 77 **Option 2** is good for testing, but will break other HTTP clients 78 which obey `http_proxy`, such as `curl`, `wget` and others: 79 80 $ docker run --rm -t -i -e http_proxy=http://dockerhost:3142/ debian bash 81 82 **Option 3** is the least portable, but there will be times when you 83 might need to do it and you can do it from your `Dockerfile` 84 too. 85 86 **Option 4** links Debian-containers to the proxy server using following command: 87 88 $ docker run -i -t --link test_apt_cacher_ng:apt_proxy -e http_proxy=http://apt_proxy:3142/ debian bash 89 90 **Option 5** creates a custom network of APT proxy server and Debian-based containers: 91 92 $ docker network create mynetwork 93 $ docker run -d -p 3142:3142 --net=mynetwork --name test_apt_cacher_ng eg_apt_cacher_ng 94 $ docker run --rm -it --net=mynetwork -e http_proxy=http://test_apt_cacher_ng:3142/ debian bash 95 96 Apt-cacher-ng has some tools that allow you to manage the repository, 97 and they can be used by leveraging the `VOLUME` 98 instruction, and the image we built to run the service: 99 100 $ docker run --rm -t -i --volumes-from test_apt_cacher_ng eg_apt_cacher_ng bash 101 102 $$ /usr/lib/apt-cacher-ng/distkill.pl 103 Scanning /var/cache/apt-cacher-ng, please wait... 104 Found distributions: 105 bla, taggedcount: 0 106 1. precise-security (36 index files) 107 2. wheezy (25 index files) 108 3. precise-updates (36 index files) 109 4. precise (36 index files) 110 5. wheezy-updates (18 index files) 111 112 Found architectures: 113 6. amd64 (36 index files) 114 7. i386 (24 index files) 115 116 WARNING: The removal action may wipe out whole directories containing 117 index files. Select d to see detailed list. 118 119 (Number nn: tag distribution or architecture nn; 0: exit; d: show details; r: remove tagged; q: quit): q 120 121 Finally, clean up after your test by stopping and removing the 122 container, and then removing the image. 123 124 $ docker stop test_apt_cacher_ng 125 $ docker rm test_apt_cacher_ng 126 $ docker rmi eg_apt_cacher_ng