Ubuntu – How to set locale when building an Ubuntu Docker image with Packer

dockerlocalizationpackerUbuntu

I'm using Packer to build a Docker image based on Ubuntu 14.04, i.e., in my Packer template I have:

"builders": [{
    "type": "docker",
    "image": "ubuntu",
    "commit": true
}],

and I build it using:

$ packer build my.json

What do I need to put in the template to get a specific locale (say en_GB) to be set when I subsequently run the following?

$ sudo docker run %IMAGE_ID% locale

Additional info

As it stands, I get:

LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
...
LC_IDENTIFICATION="POSIX"
LC_ALL=

which causes a few problems for things I want to do next, like installing certain Python packages.

I've tried adding:

{
    "type": "shell",
    "inline": [
        "locale-gen en_GB.UTF-8",
        "update-locale LANG=en_GB.UTF-8 LANGUAGE=en_GB.UTF-8 LC_ALL=en_GB.UTF-8"
    ]
}

but while that does set up the locale config it doesn't affect the env used by docker run. Even if I add extra export lines like:

{
    "type": "shell",
    "inline": [
    ...
        "export LANG=en_GB.UTF-8"
    ]
}

they have no effect, presumably because when using docker run, it's not a child process of the command packer build uses when running these commands initially.

As a workaround I can pass env vars to docker run, but don't want to have to do that each time, e.g.:

sudo docker run -e LANG=en_GB.UTF-8 -e LANGUAGE=en_GB.UTF-8 -e LC_ALL=en_GB.UTF-8 %IMAGE_ID% locale

Best Answer

I haven't tried it, but according to the documentation, you should be able to do this using the docker-import post-processor: https://www.packer.io/docs/post-processors/docker-import.html

Example:

{
  "type": "docker-import",
  "repository": "local/ubuntu",
  "tag": "latest",
  "changes": [
    "ENV LC_ALL en_GB.UTF-8"
  ]
}