📜  docker alpine set timezone (1)

📅  最后修改于: 2023-12-03 15:00:28.341000             🧑  作者: Mango

Docker Alpine Set Timezone

When working with Docker containers, it is important to ensure that the container's timezone is set to the correct value so that any time-related tasks are executed accurately. This can be particularly important for applications that rely heavily on scheduled tasks or time-sensitive operations.

One way to set the timezone in a Docker container running Alpine Linux is to use the tzdata package, which provides the necessary time zone data. Here's an example Dockerfile that demonstrates this technique:

FROM alpine:latest

RUN apk update && \
    apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

CMD ["sh"]

In this example, we start with the latest version of Alpine Linux (FROM alpine:latest). We then update the package index (apk update) and install the tzdata package (apk add --no-cache tzdata).

Next, we copy the appropriate time zone file from the /usr/share/zoneinfo directory to the /etc/localtime file in the container (cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime). This ensures that the container is using the correct timezone data.

Finally, we set the container's timezone by creating a new file at /etc/timezone with the appropriate timezone string (echo "Asia/Shanghai" > /etc/timezone).

Once this Dockerfile has been built into an image and a container has been started from that image, any time-related operations that occur inside the container will use the specified timezone.

By using this technique, you can ensure that your Docker containers are running with the correct timezone, helping to avoid any issues that may arise from time-related inconsistencies.


To summarize, setting the timezone in a Docker container running Alpine Linux involves installing the tzdata package, copying the appropriate timezone file to /etc/localtime, and creating a new file at /etc/timezone with the desired timezone string. By following these steps, you can ensure that your containers are running with the correct timezone and avoid any potential issues that may arise from time-related inconsistencies.