Handy Docker Things
When working with docker, especially with building images there’s a few handy things to note. This is not so much of a tutorial but more of a place to keep up with some handy notes and tips
When building an image and using Ubuntu as a base image, generally, there’s no need to keep the apt cache around after
installing whatever it is that’s needed for the service. This is a method to update the cache, install packages, and
then delete the cache. This is considered a “best practice” rather than using apt autoremove
.
See Docker Image Guidelines
|
|
Usually there’s no need to install the ‘suggets’ or ‘recommended’ packages if the install is in a container. Configure apt to not include those
|
|
Some packages will prompt the user for input upon installation, setting the noninteractive
env var will suppress
these
|
|
Sometime a container only has tools to be used as one-shot commands or for testing purposes you need a container to run without firing up a long running daemon. There’s a couple of acceptable way to keep the container running in these situations.
This will effectively run forever without actually doing anything
|
|
Another method is to call sleep
and tell it to run forever. The downside to this method is not all implementations of
sleep
support the infinity
argument. So…your mileage may vary
|
|
Sometimes you might want RUN
a command and provide some setting or flag via environment variables. Let’s say you want
to tell apt
to not prompt the user for any input, due to this not being a tty, only a script. Usually you could do
something like:
|
|
The issue here is the env var DEBIAN_FRONTEND
will only exist for the apt-get update
, it will be cleared when the
apt-get install ...
runs. There’s a couple of ways to get around this.
Either set the env var for each command to run:
|
|
or set it all to run as a shell script:
|
|
This will set the env var and then execute all the commands under a single shell thus inheriting the env var for all commands.