Time zone discarded on Alpine docker
Environment : Alpine Docker 9dockerhub-latest) on a Centos 7 host (centos-release-7-6.1810.2.el7.centos.x86_64)
go version: go1.11.1 linux/amd64
describe the issue: STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime') always deduct the time from UTC
Code (error hand. removed for short code sample) : ` func sampleForGithub() { fmt.Println("now : ", time.Now()) fmt.Println("now local : ", time.Now().Local())
db, _ := sql.Open("sqlite3", "whatever.db") //any file new
sqlStmt := "select STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'localtime') as id "
rows, _ := db.Query(sqlStmt)
defer rows.Close()
for rows.Next() {
var id string
rows.Scan(&id)
fmt.Println(id)
}
} `
Expected result: now : 2019-02-14 14:53:53.499102547 +0200 IST m=+0.000432506 now local : 2019-02-14 14:53:53.499191615 +0200 IST from db: 2019-02-14 14:53:53.499
Which is exactly what happens when I run on centos host
Actual: now : 2019-02-14 14:53:53.499102547 +0200 IST m=+0.000432506 now local : 2019-02-14 14:53:53.499191615 +0200 IST from db: 2019-02-14 12:53:53.499
Please note that the go "now" is on the right TZ. Also confirmed +0200 on: $export TZ $cat /etc/timezone $date Installed tzdata and next to any optional tip on: https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes sqlite documentation suggest 'localtime' is taken from export TZ var , but apparently it does not, at least not on this package.
Can you please help determine where is the location taken from ? And if its a bug rather than configuration... fix it ?
zoneinfo.zip is bundled on Go's $GOROOT/lib/time/zoneinfo.zip
@GuyBrand I don't think this has anything to do with Go, or this driver. You asked SQLite itself (the C library) to output the current time.
sqlite documentation suggest 'localtime' is taken from export TZ var
Which documentation in particular are you referring to?
Look for localtime_r() in this documentation: https://www.sqlite.org/lang_datefunc.html
Follow localtime_r() in:
https://linux.die.net/man/3/localtime
And read: According to POSIX.1-2004, localtime() is required to behave as though tzset https://linux.die.net/man/3/tzset(3) was called, while localtime_r() does not have this requirement. For portable code tzset https://linux.die.net/man/3/tzset(3) should be called before localtime_r().
Then read https://linux.die.net/man/3/tzset
function initializes the tzname variable from the TZenvironment variable
Code:
http://webfiles.icebreak.org/webfiles/CBuilderCD/v4/Runimage/Cbuilder4/Source/RTL/SOURCE/TIME/TZSET.C
Also reads:
- Check for "TZ" string in the environment.
BTW, do you have a recommended libc to install on such (Alpine) docker ? perhaps the behavior would be different .
On Fri, 15 Feb 2019, 3:52 rittneje, [email protected] wrote:
@GuyBrand https://github.com/GuyBrand I don't think this has anything to do with Go, or this driver. You asked SQLite itself (the C library) to output the current time.
sqlite documentation suggest 'localtime' is taken from export TZ var
Which documentation in particular are you referring to?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-463875979, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMcswJn3G8AfqMEEEHFNx2steW9iqHks5vNhLCgaJpZM4a7jC8 .
@GuyBrand Can you please clarify how you installed tzdata and set your TZ environment variable for the docker image in question? Did you do it in the Dockerfile? Please post the exact steps you ran.
Actually, we tried so many options...
- Yes, it was a dockerfile, from Alpine latest, we always added
RUN apk --no-cache add wget && \
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk && \
apk add glibc-2.28-r0.apk
- For Setting the TZ, and tzdata, we tried next to anything in: https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes
And some other posted, The best rates in the Post Favour this:
ENV TZ=America/Los_Angeles RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk add --no-cache tzdata ENV TZ America/Los_Angeles
Yet with asia/jerusalem as the TZ
And also some combinations, between the different suggestions
The thing is, unlike most posts on the topic that describes a container level issue, in this case, container $date is displayed correctly, go displayed the right tz, MySQL driver as well, the only issue is with the sqlite.
I'm attaching the DockerFile, please discard the line with "mycompany" (copying some data etc) - I left it there so you would have the full picture, but nothing that has to do with the issue. Also, you may remove the ENTRYPOINT that just starts some service.
On Fri, 15 Feb 2019, 14:43 rittneje, [email protected] wrote:
@GuyBrand https://github.com/GuyBrand Can you please clarify how you installed tzdata and set your TZ environment variable for the docker image in question? Did you do it in the Dockerfile? Please post the exact steps you ran.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-464036642, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMcrUyykyjRk6UO5CuMiK-KOLqZQ9tks5vNqtdgaJpZM4a7jC8 .
@GuyBrand Not sure if this is just the result of you trying different things, but these two lines are backwards:
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime
ENV TZ=Asia/Jerusalem
You need to set the TZ environment variable before referencing it in your RUN commands.
In any case, I am able to reproduce your issue. Stranger still, when I use the SQLite CLI in the container, the query outputs the correct result!
After researching a bit more, this appears to be an issue pertaining to glibc. Basically, any applications that are dynamically linked against glibc do not function properly in Alpine, because it doesn't really have glibc. (https://stackoverflow.com/a/52057474) The recommended solution is to use the Golang Alpine docker image (golang:1.11.5-alpine3.9, for example) as a base, and build the binary in the docker or Dockerfile.
Isn't the go Alpine image size few hundreds of mb?
On Sat, 16 Feb 2019, 2:09 rittneje, [email protected] wrote:
@GuyBrand https://github.com/GuyBrand Not sure if this is just the result of you trying different things, but these two lines are backwards:
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime ENV TZ=Asia/Jerusalem
You need to set the TZ environment variable before referencing it in your RUN commands.
In any case, I am able to reproduce your issue. Stranger still, when I use the SQLite CLI in the container, the query outputs the correct result!
After researching a bit more, this appears to be an issue pertaining to glibc. Basically, any applications that are dynamically linked against glibc do not function properly in Alpine, because it doesn't really have glibc. (https://stackoverflow.com/a/52057474) The recommended solution is to use the Golang Alpine docker image (golang:1.11.5-alpine3.9, for example) as a base, and build the binary in the docker or Dockerfile.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-464256409, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMcsF1A-WBEnQ85cKNt9IT-O6HCJk0ks5vN0wpgaJpZM4a7jC8 .
It is 311 MB. But you don't have to keep it around in your final image. An easy way to accomplish this is with multi-stage builds.
For example, this would compile the test binary for this repo into the image. The result is only 14.2 MB.
FROM golang:1.11.5-alpine3.9 AS compile
RUN apk add --no-cache gcc musl-dev
COPY src/ /opt/src/
RUN GOPATH=/opt go test -c -o /opt/go-sqlite3.test github.com/mattn/go-sqlite3
FROM alpine:latest AS final
ENV TZ=Asia/Jerusalem
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk add --no-cache tzdata
COPY --from=compile /opt/go-sqlite3.test /opt/go-sqlite3.test
I'm familiar with multi stage build, what I don't understand, is, In the final docker you did not get libc installed at all, I don't get where/ when will the dependencies be pulled .
On Sat, 16 Feb 2019, 15:53 rittneje, [email protected] wrote:
It is 311 MB. But you don't have to keep it around in your final image. An easy way to accomplish this is with multi-stage builds https://docs.docker.com/v17.09/engine/userguide/eng-image/multistage-build/#use-multi-stage-builds .
For example, this would compile the test binary for this repo into the image. The result is only 14.2 MB.
FROM golang:1.11.5-alpine3.9 AS compile RUN apk add --no-cache gcc libc-dev COPY src/ /opt/src/ RUN GOPATH=/opt go test -c -o /opt/go-sqlite3.test github.com/mattn/go-sqlite3
FROM alpine:latest AS final ENV TZ=Asia/Jerusalem RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apk add --no-cache tzdata COPY --from=compile /opt/go-sqlite3.test /opt/go-sqlite3.test
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-464348912, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMclKYAHFjVxqLwMiT5DevvuHLS_SDks5vOA0_gaJpZM4a7jC8 .
The base Alpine image itself already has the libmusl library. Because the binary was compiled in Alpine itself, it will be linked against that instead of glibc.
/ # ldd /opt/go-sqlite3.test
/lib/ld-musl-x86_64.so.1 (0x7f2612f4e000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f2612f4e000)
FYI, you can also install musl-dev instead of libc-dev when compiling. I've updated my previous comment to reflect this.
@GuyBrand Has your issue been resolved?
For the sake of a deadline, integration team has temporary resolved this by pushing the micro service discussed outside the docker (to the house machine), In a couple of days when environment is stable, we will spend the time to resolve this, and I will surely report, and update the ticket accordingly.
Thanks much for the effort you spent by now, it's much appreciated!
On Tue, 19 Feb 2019, 1:18 rittneje, [email protected] wrote:
@GuyBrand https://github.com/GuyBrand Has your issue been resolved?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-464919318, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMcldIeQnqMO2A76Qoe-p0N6NdbiTtks5vOzThgaJpZM4a7jC8 .
@rittneje It looks like (after making small changes to the script - git installation, gopath to /go etc) the compilation displays the right time.
However, this does mean we need to switch our CI to be built by Alpine (currently centos). Do you know if there's a way to bind libc.musl running on centos ?
First install musl-gcc.
sudo apt-get install musl-dev musl-tools
Now use musl-gcc instead of regular gcc when compiling.
CC=musl-gcc go build ...
The resulting binary will be linked against libmusl and will thus work properly when copied into your Alpine docker. Note that I have tested this using Ubuntu, but I imagine the same will work on Centos. In any case, I would still recommend doing the compilation in the Alpine docker, as cross-compilation can get very tricky depending on what exactly you are doing.
Looks like this worked!
Great advice.
On Sun, 24 Feb 2019, 16:46 rittneje, [email protected] wrote:
First install musl-gcc.
sudo apt-get install musl-dev musl-tools
Now use musl-gcc instead of regular gcc when compiling.
CC=musl-gcc go build ...
The resulting binary will be linked against libmusl and will thus work properly when copied into your Alpine docker. Note that I have tested this using Ubuntu, but I imagine the same will work on Centos. In any case, I would still recommend doing the compilation in the Alpine docker, as cross-compilation can get very tricky depending on what exactly you are doing.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mattn/go-sqlite3/issues/694#issuecomment-466783318, or mute the thread https://github.com/notifications/unsubscribe-auth/AFpMchcmCIoWaORrs26HXPBTmZ-uSa6iks5vQqXYgaJpZM4a7jC8 .
Closable?
Sure, as alpine is the go-to docker when using go, I would advise to add:
when running on an alpine docker, it is best to compile using: ``` CC=musl-gcc go build ...
to this section:
https://github.com/mattn/go-sqlite3/#alpine