1

I can't figure out how to change timezone from UTC inside of docker container.
I would love to have docker time same as my host machine time(utc+1) but nothing i tried worked.
Time is always set as UTC.


Thanks for trying to help.

Here is my code!
Dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 5010
ENV ASPNETCORE_URLS=http://+:5010






FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY . .

RUN apt-get update && \
    apt-get install -yq tzdata && \
    ln -fs /usr/share/zoneinfo/Europe/Belgrade /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata
    
ENV TZ="Europe/Belgrade"

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone  

FROM build AS publish


RUN dotnet publish "Restoran.Api" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .

ENTRYPOINT ["dotnet", "Restoran.Api.dll"]


and here is docker-compose.yml

version: '3'
services:
#mssql docker
  restoran-sql:
    image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
    restart: unless-stopped
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=QWEqwe123!
      - MSSQL_PID=Developer
      
      
    ports:
      - 1401:1433
    networks:
      - restorannet
      

  restoran-api:
    restart: unless-stopped
    build:
      context: .
    environment:
      - ConnectionStrings:testCon=Server=restoran-sql,1433;Database=testDb;User=sa;Password=QWEqwe123!;ConnectRetryCount=0
      
      
    ports:
      - 49857:5010  
    networks:
      - restorannet      
    links:
      - restoran-sql
    depends_on:
      - restoran-sql
     
       
networks:
  restorannet:
    driver: bridge      
    
    

In Dockerfile i tried adding

ENV TZ=Europe/Belgrade
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone


and placed it right below this line

ENV ASPNETCORE_URLS=http://+:5010

and in docker-compose.yml i tried adding these changes but nothing seems to work

environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=QWEqwe123!
      - MSSQL_PID=Developer
    volumes:
    - "/etc/timezone:/etc/timezone:ro"
    - "/etc/localtime:/etc/localtime:ro"  


and for api image

restoran-api:
    restart: unless-stopped
    build:
      context: .
    environment:
      - ConnectionStrings:testCon=Server=restoran-sql,1433;Database=testDb;User=sa;Password=QWEqwe123!;ConnectRetryCount=0
    volumes:
    - "/etc/timezone:/etc/timezone:ro"
    - "/etc/localtime:/etc/localtime:ro"  
sasko
  • 207
  • 2
  • 20
  • 1
    In general, servers should not have their timezone changed from UTC. You want to be able to serve users from more than one timezone. – BMitch Dec 15 '21 at 17:29
  • 1
    Have you tried installing tzdata, per this question? https://stackoverflow.com/questions/57607381/how-do-i-change-timezone-in-a-docker-container – Nick ODell Dec 15 '21 at 17:34
  • 2
    @BMitch Thanks for trying to help, in my case i need to change docker timezone so my app could be correctly tested. – sasko Dec 15 '21 at 19:17
  • @NickODell I tried but still doesnt work, i updated post to show new code in dockerfile – sasko Dec 15 '21 at 19:18

0 Answers0