Robert Jamborski
Robert Jamborski

Reputation: 315

How to set locale in postgres using docker-compose?

I've tried multiple ways of changing the locale to en_GB and nothing seems to work.

My current docker-compose.yml

version: '3.9'

services:

  db:
    image: postgres
    restart: always
    build: 
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 5432:5432
    volumes:
      - ./pg-data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      PGDATA: ${PGDATA}
      # LANG: 'en_GB.UTF-8'

  adminer:
    depends_on: 
      - db
    image: adminer
    restart: always
    ports:
      - 8080:8080
  
volumes:
  pg-data:

An official postgres image docs says to create a Dockerfile with:

FROM postgres
RUN localedef -i en_GB -c -f UTF-8 -A /usr/share/locale/locale.alias en_GB.UTF-8
ENV LANG en_GB.utf8

However this has no effect whatsoever.. Tried setting enviorment variable LC_* and LANG however this throws build error of enviorment variables being configured wrong.

Having just a Dockerfile without the enviorment variables set container builds and db is being reinitialized but while running first migration with knex the table consisting money type is still in dollars.

Upvotes: 3

Views: 7992

Answers (1)

Red
Red

Reputation: 349

Imho: You can only use the tags "image" or "build" in the compose.yml If both are present, the image is ranked higher. Hence your build section is ignored.

Also your compose has a few double entries and a logical error on the data folder.

To combine compose.yml and a Dockerfile here is an example:

compose.yml

services:

  postgres:
    container_name: 'test_pg_db'
    build: 
      context: .
      dockerfile: ./Dockerfile
    # image: 'postgres:latest'  # as we build the image, this line is actually obsolete
    restart: always
    ports:
      # depending on the access you can skip the port binding
      - 5432:5432

    # set shared memory limit when using docker-compose (standard is 64MB) - no clue why the docker tutorial recommends this
    shm_size: '128mb'
 
    environment:
      POSTGRES_USER: test_admin # The PostgreSQL user (useful to connect to the database)
      POSTGRES_PASSWORD: MyVerySecretPassword  # The PostgreSQL password (useful to connect to the database)
      POSTGRES_DB: test_db   # The PostgreSQL default database (automatically created at first launch)
              
    volumes:
    # Move the data from the container to a save location (otherwise deleting the container will delete all data)
    
    # Use this entry to have docker taking care of the path of the volume on the host 
    # uncomment the two lines at the volume section as well!
    # - pgdata:/var/lib/postgresql/data 
 
    # Use this entry to use a specific folder on the host
      - /docker_data/postgres_db:/var/lib/postgresql/data

    healthcheck:
      # had to add the user and database for no FATAL errors in the logs (role root does not exist9
      test: ["CMD-SHELL", "pg_isready -U test_admin -d test_db"]
      interval: 1s
      timeout: 5s
      retries: 10

# uncomment if you want docker to handle the volume for the data
# volumes:
  # pg-data:

here is how the Dockerfile should look like (stored in the same diretory as the compose.yml)

FROM postgres:latest
RUN localedef -i de_DE -c -f UTF-8 -A /usr/share/locale/locale.alias de_DE.UTF-8
ENV LANG=de_DE.utf8

Upvotes: 1

Related Questions