Docker Container: time/timezone wrong - time

I am in a running docker container with node and for some reason the timezones / the time of the host machine vs inside the docker container never line up:
root#foobar:~# node -e "console.log(new Date())"
>> Tue May 17 2016 15:12:43 GMT+0200 (CEST)
root#foobar:~# docker exec 9179105c0ff9 node -e "console.log(new Date())"
>> Tue May 17 2016 13:13:01 GMT+0000 (Europe)
root#foobar:~# cat /etc/timezone
>> Europe/Vienna
root#foobar:~# docker exec 9179105c0ff9 cat /etc/timezone
>> Europe/Vienna
So what I already did in my docker-start shell is script is the following:
docker run \
...
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro \
-e "TZ=Europe/Vienna" \
...
... but still, as you can see in the first codeblock, the time is still wrong! Any ideas on this? What am I missing?
(fyi: I am running a meteor app deployed via mupx)
UPDATE:
After running date on the host and inside the container, there again is a difference of 2hrs. so for some reason the docker container just does not "apply" my timezone and it seems like the problem is not JS/node related, since dateis just a simple unix system cmd ... what am I missing here?!

>> Tue May 17 2016 15:12:43 GMT+0200 (CEST)
and
>> Tue May 17 2016 13:13:01 GMT+0000 (Europe)
are approximately the same time(around 18sec difference, because you didn't ran the commands at the same time).
Take a closer look, it's around 3pm GMT+0200 and around 1pm GMT+0000.
This is just a difference in output format, but the time is the same.
If you execute .getTime() on the value of the new Date(), you will probably have the same values.
This is probably due to differences in default output format in different node.js versions.

I do it in this way:
args: ["-v /etc/timezone:/etc/timezone:ro","-v /etc/localtime:/usr/share/zoneinfo/Europe/Prague:ro", '-e TZ=Europe/Prague']
Need to install timedatectl

I was surprised to see the same problem when using new Date().toString() in a container. It always returns the string for GMT+000, and not for the server's time zone. There might be container-specific settings to change the time zone, but since I know my time zone - New York, I just used toLocaleString:
new Date().toLocaleString("en-US", {timeZone: "America/New_York"})

Related

npm package's binary accessible until I add flags

I'm on a mac am trying to install elasticdump. It is also worth noting that I am using zsh, but am getting the equivalent error described below when switching over to bash.
I just re-installed npm from scratch and am now using 6.12.0. After this, I ran npm install elasticdump -g which appears to have worked just fine.
$ which elasticdump
/usr/local/bin/elasticdump
It appears I am still having the same issue I was having before I tried re-installing everything.
I can run elasticdump directly with no issues. It tells me I need an input and an output as I would expect.
$ elasticdump
Sat, 09 Nov 2019 04:52:40 GMT | Error Emitted => {"errors":["`input` is a required input","`output` is a required input"]}
I can even run it with --help.
$ elasticdump --help
elasticdump: Import and export tools for elasticsearch
version: 6.15.7
...
But when I add an input and output, things fall apart!
$ elasticdump \
--input /Users/slant/Desktop/data.json \
--output http://localhost:9200/companies \
--type data
zsh: command not found: elasticdump
And in bash:
$ elasticdump \
> --input /Users/slant/Desktop/data.json \
> --output http://localhost:9200/companies \
> --type data
bash: elasticdump : command not found
I'm completely baffled as to how adding specific flags causes it to become completely inaccessible! Any help at all would be greatly appreciated.
Edit: In case anyone requests this:
$ ls -la /usr/local/bin/elasticdump
lrwxr-xr-x 1 slant admin 47 Oct 1 20:42 /usr/local/bin/elasticdump -> ../lib/node_modules/elasticdump/bin/elasticdump
Accessing the binary through the file directly behaves exactly the same way as using just elasticdump by itself.
Updated, for clarification.
/usr/local/lib/node_modules/elasticdump/bin/elasticdump
Sat, 09 Nov 2019 18:40:56 GMT | Error Emitted => {"errors":["`input` is a required input","`output` is a required input"]}
$ /usr/local/lib/node_modules/elasticdump/bin/elasticdump \
--input /Users/ryan/Desktop/data.json \
--output http://localhost:9200/companies \
--type data
zsh: no such file or directory: /usr/local/lib/node_modules/elasticdump/bin/elasticdump

Git Commit is using a wrong timezone

Normally I use two PCs, say: PC1 and PC2.
On both of them I'm using CygWin on Windows 10.
They had the same configuration.
On PC1 I was trying to do some adjustments on how to display the date and time when doing:
$ git log
I was trying multiple bash and git commands.
In the past and also currently on PC2 when I do:
$ date
I get the following:
Tue Sep 25 16:17:34 CDT 2018
but on PC1 (the wrong one), with the same command, I get:
Tue, Sep 25, 2018 16:17:34
where you can see that the TimeZone indicator is not showing up.
Also, now on PC1, when I do a commit and then list the history with: $ git log, dates are like:
Date: Tue Sep 25 22:58:42 2018 +0100
where it should be:
Date: Tue Sep 25 16:58:42 2018 -0500
because my current timezone is: CDT (America/Chicago) (-05:00)
Any idea on how to solve this?
Thanks!
[EDIT 1]
Responding to suggestion from #Johan below where he said:
--- begin of comment ---
You need to set the git log --date to local.
git config --global log.date local
--- end of comment ---
I just tried that and now the date that shows up on my last commit doesn't have the TimeZone indicator. Using the same time reference as before it would be:
Date: Tue Sep 25 22:58:42 2018
I need to get the TimeZone indicator on the commit list.
[EDIT 2]
Responding to suggestion from #Johan below where he said:
--- begin of comment ---
You need to set the git log --date to default.
git config --global log.date default
--- end of comment ---
I just tried that and now and I get again:
Date: Tue Sep 25 22:58:42 2018 +0100
even trying new commits.
You need to set the git log --date to default.
git config --global log.date default
Source: git: timezone and timestamp format
I don’t think this will address the root issue on your system, but it will certainly help with git’s commit dates. Git uses an environment variable called TZ to set the timezone (for any command, not only commit), so you can set an alias for it in your shell:
alias git='TZ=CDT /usr/bin/git'
Check if the issue persists when:
using a regular CMD
setting a simplified PATH
That is:
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
Note that with that setup, you benefit from 200+ Linux-like commands without even opening a bash (again, from a regular CMD session): you can type ls -alrth or grep or ... (directly from the CMD, no bash)
Since the date used in a commit is directly from the system, if time /T returns the proper hour (including DST), you won't have any issue with a git log date.

Restricting cpu cores in a docker container

I'm trying to restrict the CPUs of the container using the docker --cpuset-cpus option. But I'm not getting the desired result for some reason. For example the following command should just print 1:
docker run -it --cpuset-cpus=0 ubuntu:latest grep processor /proc/cpuinfo | wc -l
But I get the result as 4 (4 is the number of cpus shown in my host). This is so for any OS.
docker run -it --cpuset-cpus=0 centos grep processor /proc/cpuinfo | wc -l
docker run -it --cpuset-cpus=0 alpine grep processor /proc/cpuinfo | wc -l
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:42:45 2017
OS/Arch: linux/amd64
Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:41:24 2017
OS/Arch: linux/amd64
Experimental: false
Am I wrong in my understanding of the cpuset-cpus option? If so, what is the exact parameter that I need to pass to get the desired behavior I'm expecting? (grep processor /proc/cpuinfo | wc -l should output 1)
You mean you want to use less CPU in percentage or in numbers. Like you want to use 50% of your CPU, or 2 CPU's?
$ docker run -it --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash
This means processes in container can be executed on cpu 0, cpu 1 and cpu 2.
The --cpu-quota flag limits the container’s CPU usage. The default 0 value allows the container to take 100% of a CPU resource (1 CPU).
It seems that there is an issue about that https://github.com/moby/moby/issues/20770
try to use something similar to
docker run --rm --cpuset-cpus=0,1 ubuntu sh -c "cat /sys/fs/cgroup/cpuset/cpuset.cpus"
And check if that works.
Hope it helps.
As far as I know the kernel's CPU sets don't affect the proc file in any way.

Running code without ssh in beaglebone black

I want to run some code in Beaglebone black without doing ssh when I apply power.
I have tried putting some commands to run the code in ~/.bashrc file but it only works when I login using ssh. I have tried same thing with /etc/rc.local file but didn't work even after ssh.
I have also tried #reboot my_command in crontab -e but it also requires me to login using ssh
Any suggestions??
EDIT:
root#beaglebone:~# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie
root#beaglebone:~# ps aux | grep cron | grep -v grep
root 295 0.0 0.3 4428 1988 ? Ss 15:03 0:00 /usr/sbin/cron -f
Output of crontab -e: last few lines
root#beaglebone:~# crontab -e
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
##reboot /root/wiringBone-master/library/main not working
#*/5 * * * * /root/wiringBone-master/library/main works
main is the script I want to run
/etc/rc.local is a quick way. Make sure to launch into background and don't prevent the script from finishing.
Writing a proper systemd service file would be better though.
crontab -e method worked!!. My script required two overlays to execute the code which I didn't load that's why my #reboot command didn't work. I solved my problem by adding required overlays.
#reboot config-pin overlay cape-universaln
#reboot config-pin overlay BB-ADC
#reboot /root/wiringBone-master/library/main
And now my code works on reboot.
I don't know anything about beagle bone, but on a normal Linux system you'd likely do this with either an init script, or, more easily, in a cron script set to run at boot.
You'll have to check if you're environment would support either of those. Even if it doesn't have cron, it is probably running some sort of init (likely to be the thing starting SSH on boot, but YMMV).

Implementing shutdown command from scratch

root ~$ shutdown
-sh: shutdown: not found
root ~$ shutdown -h now
-sh: shutdown: not found
None of the commands are working . I think I need to link the command from scratch. Can anybody guide?
uname -a
gives
2.6.35.3 #49 PREEMPT Wed Jun 11 20:03:43 IST 2014 armv5tejl GNU/Linux
Try to call the command with complete path: /sbin/shutdown
moreover on most systems it is a root only command, so you should call it from root user (I am seeing the $ at the end of command prompt, so I am guessing you are not root)
Other commands you can try are presented here;
or you can use the init 0 command always as root user.
Writing the shutdown code is a last costly resort only if all the other alternatives fail.

Resources