Build your own APT-Mirror with Docker

Install Docker if you don’t have it already.

I’ll perform this update on my Debian Dell XPS 15 9560.

Step I — Create the structure

The following command will create our needed directory structure:

mkdir -p docker-mirror/build/files

The -p option means that all parent directories are also created

Now it should look like that:

$ tree docker-mirror/
docker-mirror/
└── build
└── files

Step II — Create the Dockerfile

This Dockerfile will provide us all we need to buid our own mirror for APT-Based systems.

So we do the following:

vim docker-mirror/build/Dockerfile

Then you need to paste the following content in it:

FROM debian:9COPY files/entrypoint.sh /opt/entrypoint.shRUN apt-get update -q \
&& \
apt install -y -qq apt-mirror \
&& \
apt-get autoremove \
&& \
rm -rf /var/cache/apt/* \
&& \
mkdir -p /mnt/mirror/debian \
&& \
touch /var/log/cron.log \
&& \
chmod u+x /opt/entrypoint.sh
COPY files/mirror.list /etc/apt/mirror.listVOLUME ["/mnt/mirror/debian"]ENTRYPOINT ["/opt/entrypoint.sh"]

We will we wil not worry about theentrypoint.sh and mirror.list for now.

This will basically install the apt-mirror tool which will later on download all content we provide in our mirror.list

The VOLUME provides us a mountpoint which we can use to store our files locally later.

Step III — Create a mirror.list

Now we are going to create the mirror.list file. This will contain all repositories we are willing to download.

Create the mirror file:

vim docker-mirror/build/files/mirror.list

And paste for example the following content:

############# config ##################
#
set base_path /mnt/mirror/debian
#
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads 20
set _tilde 0
#
############# end config ##############
## Debian Stretch
# BASE
deb-amd64 http://ftp2.de.debian.org/debian/ stretch main
deb-amd64 http://security.debian.org/debian-security stretch/updates main contrib non-free
deb-amd64 http://ftp2.de.debian.org/debian/ stretch-updates main
## Debian Jessie
# BASE
deb-amd64 http://ftp2.de.debian.org/debian/ jessie main
deb-amd64 http://security.debian.org/debian-security jessie/updates main
deb-amd64 http://ftp2.de.debian.org/debian/ jessie-updates main
## Ubuntu 16.04
# BASE
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ xenial-security main restricted
deb http://security.ubuntu.com/ubuntu/ xenial-security universe
deb http://security.ubuntu.com/ubuntu/ xenial-security multiverse

# Cleanup old packages
clean http://ftp2.de.debian.org/debian
clean http://archive.ubuntu.com/ubuntu
clean http://security.ubuntu.com/ubuntu

The provided mirror.list will download you the following:

  • Ubuntu Xenial
  • Debian Stretch (amd64 architecture)
  • Debian Jessie (amd64 architecture)

You can easily enhance/edit this with your personal favorites.

Step IV — Create the entrypoint.sh

The entrypoint.sh is the file providing a Docker startup command.
In our case, we want to run the apt mirror and then let it sleep for a few hours.

Why not continous syncing?

There are not that many changes on those mirrors, so we need a timespan of minimum 6 hours or less often.

So we create another file:

vim docker-mirror/build/files/entrypoint.sh

And paste the following content, if it fits:

#!/bin/bash# Refresh mirror every X hours
while true;
do
apt-mirror
echo "Start sleep 6h..."
sleep 6h
done

Step V — Lets put it all together

Finally we have prepared everything we need to get our mirror working.

Now we need to Build our Image. First we need to change into the build directory cd docker-mirror/build/ .

Now we just need to type the build command:

docker build -t aptmirror:latest .

The following output will appear:

docker build -t aptmirror:latest .
Sending build context to Docker daemon 7.168kB
Step 1/6 : FROM debian:9
---> 2b98c9851a37
Step 2/6 : COPY files/entrypoint.sh /opt/entrypoint.sh
---> Using cache
---> 45a243119ff2
Step 3/6 : RUN apt-get update -q && apt install -y -qq apt-mirror && apt-get autoremove && rm -rf /var/cache/apt/* && mkdir -p /mnt/mirror/debian && touch /var/log/cron.log && chmod u+x /opt/entrypoint.sh
---> Running in eb6a1e81fb0c
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org stretch/updates InRelease [94.3 kB]
[...]Removing intermediate container eb6a1e81fb0c
---> 1fdf179c4a6b
Step 4/6 : COPY files/mirror.list /etc/apt/mirror.list
---> 2b9304d9c870
Step 5/6 : VOLUME ["/mnt/mirror/debian"]
---> Running in 0a051fa24ebe
Removing intermediate container 0a051fa24ebe
---> 8d3f81a96b3a
Step 6/6 : ENTRYPOINT ["/opt/entrypoint.sh"]
---> Running in 9a366a2a9afe
Removing intermediate container 9a366a2a9afe
---> 02295b160144
Successfully built 02295b160144
Successfully tagged aptmirror:latest

Now you’ve the aptmirror:latest Image locally.

Step VI — Test your mirror

To test your mirror locally run the following command from your terminal:

docker run -v "$(pwd)/mirror_data":/mnt/mirror/debian aptmirror:latest

You should get some output like this:

Downloading 434 index files using 20 threads...
Begin time: Thu Jul 19 15:16:30 2018
[20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... ^C[7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]...

You may want to stop your container using CRTL+C before your harddisk is filled 100%…

If you want to add new mirrors, you’ve just to rebuild the Docker-Image.
Or you may want to try to mount the mirror.list file as volume, so you can change it on the fly.

Congrats! You now have a working APT-Mirror.

Cheers

Sources:

Working as a IT-Operations engineer at NeXenio, a spin-off by Hasso-Plattner-Institute for products around a digitial workspace.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store