120

I put the locale setting codes below into my dockerfile,

FROM node:4-onbuild

# Set the locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

but it gives me the error

/bin/sh: 1: locale-gen: not found
The command '/bin/sh -c locale-gen en_US.UTF-8' returned a non-zero code: 127

any idea?

Hammer
  • 8,538
  • 12
  • 44
  • 75

2 Answers2

246

Thanks for your comment, edwinksl. I updated my dockerfile below which solved the locale-gen error:

FROM node:4-onbuild

# Set the locale
RUN apt-get clean && apt-get update && apt-get install -y locales
RUN locale-gen en_US.UTF-8
leesei
  • 6,020
  • 2
  • 29
  • 51
Hammer
  • 8,538
  • 12
  • 44
  • 75
  • 35
    For efficiency you'll want to avoid multiple `RUN` statements, especially when they are closely related like this. Something like `RUN apt-get clean && apt-get -y update && apt-get install -y locales && locale-gen en_US.UTF-8` would create a single layer instead of three. – tripleee Apr 13 '17 at 06:04
  • 10
    Maybe `update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8` is also useful https://askubuntu.com/a/505424/196423 – koppor Aug 16 '17 at 07:18
  • 3
    For keeping your sanity, you'll want to avoid stashing all you commands together in one RUN – mihai Aug 26 '20 at 09:44
  • @mihai thankfully, bash lets you break commands into multiple lines with `/`. you'll actually want to do this and keep them all in one `RUN`, otherwise you will pollute your local environment with hundreds of useless layers. – Sam Gammon Sep 26 '22 at 01:05
  • @mihai's advice should be packaged with a reminder about `docker system prune --all` – Sam Gammon Sep 26 '22 at 01:05
15
apt-get install -y locales
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

localedef is also good to use.

Izana
  • 2,537
  • 27
  • 33
  • This should be the accepted answer, it's the only thing that set the perl locale correctly of all the answers on this page! – Ahi Tuna Jan 09 '22 at 20:08