Details the common commands to modify docker time zone and docker

  • 2020-05-30 21:23:45
  • OfStack

I encountered such a business scenario a few days ago. The database is running in docker, and the downtown area of docker is utc, so it is 8 hours different from Beijing time. However, you cannot re-run a container, so you can only keep the database running and copy the host's time zone to the docker container. Very upset,

First, I changed the host's time zone to CST Beijing time. The host's time zone is then copied to the docker container. The command is

docker cp /etc/localtime: The container ID or NAME 】 /etc/localtime

Of course, I can also enter the container to modify the time zone (although my container always reads /etc/localtime file only when I modify it, so I can't modify it. So use the above method), command as follows

Start by adding all the time zones

Then change the time zone

apk add tzdata 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
echo "Asia/Shanghai" > /etc/timezone

Of course, changing it inside the container is also a hassle, because you have to change it every time you start a new container, so it's even better to change it inside dockerfile. The command is

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Finally, the docker common command is attached

docker ps view the running container

docker ps-a view the stopped container

docker ps-q only lists containers ID

docker ps-s shows the container size.

docker images view mirror

docker images -q only lists mirror ID

docker images and tree all commit histories of the mirror are listed in a tree.

docker pull IMAGE_ID download image

docker push IMAGE_ID upload image

docker rmi IMAGE_ID delete image

docker rmi-f forcibly removes the image, even though it is being used;

docker logs -f < Container name orID > View container logs

docker start|stop|restart start, stop, and restart one or more of the specified containers.

Start one container and go into interactive mode

docker start -i containerID

Displays the history of a mirror;

docker history image_name

Into the container

docker exec -it   The container id  bin/bash

Start the container and start bash (interactive mode) :

$docker run -i -t <image_name/continar_id> /bin/bash

Start the container to run in back-desk mode (more general) :

$docker run -d -it image_name

docker run command explanation

-d background runs the container and returns the container ID;

-i runs the container in interactive mode, usually in conjunction with -t;

-t reassign a pseudo-input terminal to the container, usually used at the same time as -i;

Map the port of container to the port of the host

docker run -i -t -p <host_port:contain_port> 

Solidify 1 container into 1 new image, repo:tag is optional

docker commit <container> [repo:tag] 

Delete 1 or more container

apk add tzdata 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
echo "Asia/Shanghai" > /etc/timezone

Delete all container

apk add tzdata 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
echo "Asia/Shanghai" > /etc/timezone

Ditto, delete all container

apk add tzdata 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
echo "Asia/Shanghai" > /etc/timezone

Related articles: