Reputation: 469
I have a python application that run in docker. In application, I am using datetime.now() function and this function takes value from container timezone. So I should configure container timezone.
My directory structure:
- myapp (project folder)
- DockerFile
- docker-compose.yml
- requirement.txt
- run.py
My Dockerfile:
FROM ubuntu:latest
ENV TZ=Europe/Istanbul
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
ENV HOME=/app
WORKDIR /app
RUN pip install -r requirements.txt
ENV LOGIN_USERNAME=master_login_username
ENV LOGIN_PASSWORD=master_login_password
ENV SLAVE_LOGIN_USERNAME=slave_login_username
ENV SLAVE_LOGIN_PASSWORD=slave_login_password
ENV AWS_ACCESS_KEY_ID=....
ENV AWS_SECRET_ACCESS_KEY=....
ENV CLUSTER_NAME=....
EXPOSE 5000
ENTRYPOINT ["gunicorn","-b","0.0.0.0:5000","-w","1","myapp:create_app()","--access-logfile","/app/myapp/logs/access.log","--error-logfile","/app/myapp/logs/error.log","--timeout","90"]
My docker-compose.yml:
version: '3.5'
services:
master:
build: .
ports:
- "5000:5000"
container_name: master
environment:
- MONGO_URI=mongodb://mongodb:27017/master
mongodb:
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/usr/data/db
- MONGO_LOG_DIR=/dev/null
ports:
- 27017:27017
command: mongod --smallfiles --logpath=/dev/null # --quiet
networks:
default:
name: master
So, Actually in Dockerfile I configure timezone. But I am building with docker-compose up --build
and container timezone doesn't change.
When I build without compose as docker build -t
and run with docker run --name slave -d -p 80:5000
timezone is changes as I want to.
What am I missing ?
Thanks and best regards..
Upvotes: 6
Views: 23892
Reputation: 1224
Updating /etc/timezone
is the usual way, but, at least in my case, I had to do something else: create a link from the desired time zone to etc/localtime
and reconfigure tzdata
(install it first, if you don't have it):
# Set TimeZone to Spain (mainland)
ENV TZ=Europe/Madrid
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
Best regards!
Upvotes: 7
Reputation: 469
I solved the problem. Actually, I checked /usr/share/zoneinfo folder is exists. And I saw that not exists. So, Tzdata package is missing. I added line in Dockerfile apt-get install tzdata -y
and solved my problem.
Upvotes: 6
Reputation: 1793
You need to set environment variable in docker-compose
version: '3.5'
services:
master:
build: .
environment:
- "TZ=Europe/Istanbul"
ports:
- "5000:5000"
container_name: master
environment:
- MONGO_URI=mongodb://mongodb:27017/master
mongodb:
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/usr/data/db
- MONGO_LOG_DIR=/dev/null
ports:
- 27017:27017
command: mongod --smallfiles --logpath=/dev/null # --quiet
networks:
default:
name: master
Upvotes: 0