My blog is deployed inside Docker, and I found that the time of comments and messages differs from Beijing Time by 8 hours. I discovered that this was because the timezone setting in the container was inconsistent with the server, so it needs to be set.
Record the modification of server and Docker timezone
Preface
Change the Time Zone of a Linux Server
- View current timezone settings
Use the
datecommand to check the current system time and find that the current timezone is set to UTC. Please provide the content you would like translated to English.
$ date
Wed Mar 3 09:21:28 UTC 2021
Sure, please provide the content you would like translated to English.
2. List available time zone options
The ls command lists all timezone information under the /usr/share/zoneinfo directory.
Please provide the content you would like translated to English.
$ ls /usr/share/zoneinfo
Africa Chile Etc Indian MET posixrules US
America CST6CDT Europe Iran Mexico PRC UTC
Antarctica Cuba Factory iso3166.tab Mideast PST8PDT WET
Arctic EET GB Israel MST right W-SU
Asia Egypt GB-Eire Jamaica MST7MDT ROC zone.tab
Sure, please provide the content you would like translated to English.
3. Change the timezone setting
Use the ln command to link the /etc/localtime file to the /usr/share/zoneinfo/Asia/Shanghai file to change the system timezone setting to Shanghai timezone.
Please provide the content you would like translated to English.
$ sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Please provide the content you would like translated to English.
4. Verify time zone settings
Use the date command to check the current system time and find that the current timezone is set to Shanghai Time.
Sure, please provide the content you would like translated to English.
$ date
Wed Mar 3 17:21:28 CST 2021 Sure, please provide the content you would like translated to English.
Configuring Timezone in Docker
Create a Dockerfile file
Create a file named Dockerfile in the project root directory for building the Docker image. You can refer to the code example below for specific implementation:
Sure, please provide the content you would like translated to English.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY . .
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENTRYPOINT ["dotnet", "YourProjectName.dll"]
Sure, please provide the content you would like translated to English.
Set the Asia/Shanghai timezone and save it to the /etc/localtime and /etc/timezone files to ensure consistency with the host machine.
2. Build Docker Image
Run the following command in the project root directory to build the Docker image:
Please provide the content you would like translated into English.
docker build -t your-image-name .
Sure, please provide the content you would like translated to English.
Use the docker build command to build a Docker image and specify the image name as your-image-name. The final . indicates the current directory where the Dockerfile is located.
3. Run Docker container
After building the Docker image, you can use the following command to run the Docker container:
Sure, please provide the content you would like translated.
docker run -d -p 80:80 --name your-container-name your-image-name
Sure, please provide the content you would like me to translate.