How to set locale in Docker Alpine?
Asked Answered
C

3

19

I can set locale with CentOS image with

FROM centos

ENV LANG en_US.UTF-8

ENV LC_ALL en_US.UTF-8

But It seems not work with Alpine image. How can I set locale with Alpine image?

Caviar answered 1/3, 2018 at 3:35 Comment(1)
The default image doesn't seem to support it, see: github.com/gliderlabs/docker-alpine/issues/144Thingumabob
F
12

It works for me, Dockerfile:

FROM openjdk:8-jdk-alpine

RUN apk update
RUN apk add tzdata
RUN cp /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime
RUN rm -r /usr/share/zoneinfo/Africa && \
    rm -r /usr/share/zoneinfo/Antarctica && \
    rm -r /usr/share/zoneinfo/Arctic && \
    rm -r /usr/share/zoneinfo/Asia && \
    rm -r /usr/share/zoneinfo/Atlantic && \
    rm -r /usr/share/zoneinfo/Australia && \
    rm -r /usr/share/zoneinfo/Europe  && \
    rm -r /usr/share/zoneinfo/Indian && \
    rm -r /usr/share/zoneinfo/Mexico && \
    rm -r /usr/share/zoneinfo/Pacific && \
    rm -r /usr/share/zoneinfo/Chile && \
    rm -r /usr/share/zoneinfo/Canada
RUN echo "America/Sao_Paulo" >  /etc/timezone

ENV TZ America/Sao_Paulo
ENV LANG pt_BR.UTF-8
ENV LANGUAGE pt_BR.UTF-8
ENV LC_ALL pt_BR.UTF-8

ARG JAR_FILE
ADD ${JAR_FILE} /app/
RUN mv /app/${JAR_FILE} /app/app.jar
ENTRYPOINT java $JAVA_OPTS -jar /app/app.jar
Forsta answered 17/12, 2018 at 17:27 Comment(2)
Removing the unneeded files in the way you do doesn't really reduce the size of the image. If that is the intention, gather them in one RUN and use && between the commands.Changeover
Works for me, thank you !!! I didn't need to remove zoneInfos with rm command, my docker file: ` FROM eclipse-temurin:17-jre-alpine RUN apk update RUN apk add tzdata RUN cp /usr/share/zoneinfo/America/Recife /etc/localtime RUN echo "America/Recife" > /etc/timezone ENV TZ America/Recife ENV LANG pt_BR.UTF-8 ENV LANGUAGE pt_BR.UTF-8 ENV LC_ALL pt_BR.UTF-8 COPY --from=builder app/target/app*.war app.war EXPOSE 8080 ENTRYPOINT ["java","-jar","/app.war"] `Kearney
R
10

This is how I setup my time & encodings when containerizing an application in Alpine Linux.

The following is tested and known to work correctly as of Alpine Linux version 3.16:

In the Dockerfile:

Install required packages:

RUN apk add --no-cache --update musl musl-utils musl-locales tzdata

Set timezone from the choices in /usr/share/zoneinfo/:

ENV TZ=Europe/London
RUN cp /usr/share/zoneinfo/Europe/London /etc/localtime

In /usr/share/i18n/locales/musl, Musl will install the following encodings:

cs_CZ.UTF-8
de_CH.UTF-8
de_DE.UTF-8
en_GB.UTF-8
en_US.UTF-8
es_ES.UTF-8
fi_FI.UTF-8
fr_FR.UTF-8
it_IT.UTF-8
nb_NO.UTF-8
nl_NL.UTF-8
pt_BR.UTF-8
pt_PT.UTF-8
ru_RU.UTF-8
sr_RS.UTF-8
sv_SE.UTF-8

Choose your preferred encoding from the above list and substitute it for en_GB.UTF-8 if you don't want GB English in the following lines of your Dockerfile:

RUN echo 'export LC_ALL=en_GB.UTF-8' >> /etc/profile.d/locale.sh && \
  sed -i 's|LANG=C.UTF-8|LANG=en_GB.UTF-8|' /etc/profile.d/locale.sh

Although you have to sed the LANG= parameter, by appending export LC_ALL= to the end of /etc/profile.d/locale.sh the rest is done for you.

After the build, when you now execute:

locale

You'll see the default encodings have changed FROM C.UTF-8 TO en_GB.UTF-8:

bash-5.1# locale
LANG=en_GB.UTF-8
LC_CTYPE=en_GB.UTF-8
LC_NUMERIC=en_GB.UTF-8
LC_TIME=en_GB.UTF-8
LC_COLLATE=en_GB.UTF-8
LC_MONETARY=en_GB.UTF-8
LC_MESSAGES=en_GB.UTF-8
LC_ALL=en_GB.UTF-8
bash-5.1#

And when you execute:

date

You'll note the the time has now changed FROM UTC TO BST:

Mon Jul 18 16:59:51 BST 2022

Finally, some notes worth reading on Alpine's Musl implementation of locales is HERE

Renatarenate answered 18/7, 2022 at 16:5 Comment(0)
D
3

There is a cleaner way using apk del tzdata, described here:

Setting the timezone - Alpine Linux

Ductile answered 29/8, 2019 at 18:48 Comment(2)
First you need to install the page (apk add), configure the timezone, and optionally remove the package. But this is not what OP was asking for.Ammadis
this is only for uclibc, not musl or glibcSpitler

© 2022 - 2024 — McMap. All rights reserved.