-1

So I know that with each RUN command docker creates a layer, or intimidate images if you will, and that this leads to caching apt-get update. But I am having very very difficult times with installing new packages with apt-get in docker, and I'm not sure what is going on, I have spent hours now fidgeting around getting no where, I'm mainly using ubuntu as a base image.

Certain packages seem to randomly not install, for instance wget. If I go into the terminal in my container, and do an apt-get update then a apt-get install wget I can install wget. But in the docker file, even when I delete all the images created by each RUN line to stop the update from being cached, I cannot install wget with the exact same commands, it is absolute madness, and I feel as if I am going insane.

Other packages seem to randomly work and not work. I've even had this package: python-glpk which was installing in the Dockerfile at first, but not anymore... I am unable to see any deterministic behaviour to update and install, they seem to work when the wind is only blowing a certain direction.

It takes a long time to do an update with my internet and I'm on a virtual machine so my workflow is completely broken, lost so much time, is anyone else having issues with simple apt-get update && apt-get install? What are the best practices when dealing with apt-get in docker so ensure everything is working as it should?

2
  • 1
    Please show the content of your Dockerfile and the build output.
    – BMitch
    Jan 23, 2020 at 14:26
  • If you want a good answer, you need to show us how you create your docker container. I guess you are running something like: sudo docker run -it ubuntu:16.04 /bin/bash? Oct 31, 2020 at 13:44

2 Answers 2

1

Just a curious note on this topic. Several people tend to customize FROM ubuntu and add helper scripts to trim package installation. But the official ubuntu:18.04 image comes with a few improvements. Run this:

docker run --rm -it ubuntu:18.04 bash

And take a look around /etc/apt to notice it has a few triggers in place specifically for docker builds.

root@12711b0547f3:/# ll /etc/apt/apt.conf.d/
total 40
drwxr-xr-x 1 root root 4096 Feb  6 03:37 ./
drwxr-xr-x 1 root root 4096 Feb  4 21:03 ../
-rw-r--r-- 1 root root   42 Apr 20  2018 01-vendor-ubuntu
-rw-r--r-- 1 root root  927 Jan 25 11:51 01autoremove
-r--r--r-- 1 root root 1081 Feb  4 21:03 01autoremove-kernels
-rw-r--r-- 1 root root  182 Feb 28  2018 70debconf
-rw-r--r-- 1 root root   44 Feb  6 03:37 docker-autoremove-suggests
-rw-r--r-- 1 root root  318 Feb  6 03:37 docker-clean
-rw-r--r-- 1 root root   70 Feb  6 03:37 docker-gzip-indexes
-rw-r--r-- 1 root root   27 Feb  6 03:37 docker-no-languages

apt is pre-configured to clear caches after every apt install, use compressed indeces, and to avoid suggesting any further packages.

0

Some output or logs would be nice to answer your question.

Sometimes apt-get update is getting slow, it has nothing to do with docker itself. If you have bad connection packages might fail to download completely.

As you said, each line in Dockerfile will create an image layer, and cache will be saved locally, to speed up image creation. There are some practices to minimize docker image size, and put as few lines as possible, by combining them, so in case of package installation following pattern is used:

RUN \
  apt-get update &&\
  apt-get install -y zip &&\
  rm -rf /var/lib/apt/lists/*

This will take some time to update cache, install package and cleanup space, and it will generate only single image layer, so any other package installation you will need to apt-get update first to install packages.

One approach you can try is running each step in a new RUN statement:

RUN \
  apt-get update

RUN \
  apt-get install -y zip

RUN \
  apt-get install -y curl

This way you will have more layers, but will have "updated" apt cache layer, and will install packages one by one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .