顯示具有 container 標籤的文章。 顯示所有文章
顯示具有 container 標籤的文章。 顯示所有文章

start versus restart for docker containers

Docker start vs restart:快速掌握差異

Docker 是個方便的服務部署工具,可快速安裝執行伺服軟體,供瀏覽器等前台使用。當用戶從 docker 映像檔建立 (create) 及啟動 (start) 容器時,容器將分別處於 巳建立 (created) 及 執行 (running) 狀態。然後,遇到執行意外出錯 或 管理者故意下停止指令 (docker stop) 時,容器將進入退出 (exited) 狀態。

處於退出狀態的容器,因行程退出,將喪失記憶體狀態,但仍保有原來的可寫層 (writable layer) ,即儲存體狀態。此時,兩種指令 docker start 與 docker restart 都能讓既有容器,從現有可寫層狀態,再跑起來,效果完全一樣。以下用表格整理說明兩指令適用時機之差異。

1. 指令行為比較

指令 行為說明 適用狀態
docker start 啟動 巳建立 (created) 及退出 (exited) 的既有容器,直接執行其進入點命令 (entrypoint command) created / exited
docker restart 同上,但遇到執行狀態的容器會先讓其 stop,再 start created / running / exited

2. 執行期影響比較

項目 start restart
送 SIGTERM / SIGKILL❌✅
PID 改變❌✅
記憶體狀態重置❌✅
中斷連線❌✅
停機時間無短暫

3. docker restart 的清理範圍

區域 是否清理
In-memory data✅
Processes / threads✅
Open sockets✅
TCP connections✅
Writable filesystem❌
Volumes❌
Env / config❌
Image❌

4. 結論:適用性與效果

docker start 與 docker restart 兩個指令目的都在不放棄既有容器的可寫層之下,以新記憶體,執行容器的服務。在容器處於 退出 狀態時,兩者效果完全相同,都會重新啟動容器主行程,重新分配記憶體,並執行 entrypoint command。 其適用時機摘要如下表。

適用情境 效果
容器正常退出,要再跑一次docker start
就現有可寫層,啟動既有容器
程式卡住、連線異常docker restart
保留可寫層,重新啟動既有容器
組態設定或資料壞掉docker run
放棄可寫層,重新建立啟動新容器

how many instances and containers will be launched in AWS ECS service

AWS 彈性容器服務 (ECS) 分成 ECS on Fargate 及 ECS on EC2 兩種。其中,ECS on Fargate 由 Fargate 服務全權負責容器如何配置到個體,屬無伺服器 (serverless)或 託管 (managed)的運算類服務。ECS on EC2 則由用戶自行負責容器如何配置到個體,其服務參數的任務配置(task placement)選項包含: az balanced spread,az balanced binpack,binpack,one task per host,random,custom等。預設 az balanced spread 為先平衡分散到不同的可用區,再平衡分散到可用區內個體。binpack乃依記憶體儘量配置容器到最少數量的個體上,即挑選足夠放置之下,剩餘記憶體最小之個體先放容器。

至於 ECS 到底會啟動幾個虛擬機個體 (instance)及容器 (container)來提供服務,可由如下因素決定: 
  1. 個體數: 由 ECS 叢集參數的個體希望容量(desired capacity) 上下限決定。其實際數量可由叢集的自動伸縮群組(ASG, Auto Scaling Group)參數,依個體負載指標值(metric)變化,在希望容量的上下限之間,採用目標追蹤(target tracking)類型原則,作伸縮調整。
     
  2. 容器數: 由 ECS 部署建立服務的所須任務數 (desired tasks)參數,及建立任務定義時使用的容器數量有多少兩者決定。ECS服務的實際任務數可由服務自動伸縮 (Service Auto Scaling) 參數,依服務負載指標值變化,看是採用目標追蹤(target tracking)類型或步進伸縮(step scaling)類型的原則,作伸縮調整。
Reference:
1. ECS叢集個體自動擴展: Automatically manage Amazon ECS capacity with cluster auto scaling - Amazon Elastic Container Service

docker command line summary

docker command line summary

image: a read-only file system consisting of a base layer and a series of difference layers

container: a writable file system consisting of an image and a top writable layer with network and volume hooks

    Note:
  • an image has id, name, tag, read-only_layers, entrypoint, and cmd
  • a container has id, name, source_image, writable_layer, overwrite_cmd, cpu_limit, memory_limit, volume_hook, network_hook, and state (active/un-active)
what docker can do with images in a local image store
  -- creation --
  - import/export with docker hub
   search in a docker hub (docker search)
   pull from a docker hub (docker pull)
   push to a docker hub (docker push)
  - import/export with local filesystem
   load from a local file (docker load)
   save to a local file (docker save)
  - born from scratch
   commit an un-active container to create an image (docker commit)
   build an image with Dockerfile instructions (docker build)
 
  -- management --
  list (docker images)
  remove (docker rmi)

what docker can do with containers in a local container store
  -- creation -- 
   create an un-active container from an image (docker create)
   run from an image with an overwrite command (docker run)
   execute a command (docker exec)

  -- management
   stop an active container with a grace period (docker stop)
   kill an active container (docker kill)
   start an un-active container (docker start)
   restart an active container (docker restart = 
   		docker stop + docker start)
  -
   copy with local filesystem (docker cp)
  -
   attach the console to a container (docker attach)
  -
   commit an un-active container to create an image (docker commit)
  -
   list (docker ps [-a])
   remove (docker rm [-f])
    Note:
  • an image runs a new container with an overwrite command = an image creates a new container + the container starts with an overwrite command
  • a container is created when docker creates from an image or runs from an image with an overwrite command
  • an image is created when docker commits a container or builds from a base image and a Dockerfile
#
#
# image operation
#
#

#### search for an image
docker search image_keyword

#### download an image
docker pull [registry.hub.docker.com/]image_path[:tag] 

#### list images in local drive
docker images [image_path]

#### create new images
1.docker commit -m "submit message" -a "submit user" container_id image_path[:tag]

 $ vi ./Dockerfile
 # This is a comment
 FROM ubuntu:14.04
 MAINTAINER Docker Newbee <newbee docker.com="">
 RUN apt-get -qq update
 RUN apt-get -qqy install ruby ruby-dev
 RUN gem install sinatra
 # put my local web site in myApp folder to /var/www
 ADD myApp /var/www
 # expose httpd port
 EXPOSE 80
 # the command to run
 CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

2.docker build -t="image_path:tag" .  

3.cat ubuntu-14.04-x86_64-minimal.tar.gz  | docker import - ubuntu:14.04

4.docker tag container_id image_path:new_tag

#### upload an image
docker push image_path # all tags

#### save and load an image as local file 
docker save -o ubuntu_14.04.tar ubuntu:14.04

docker load --input ubuntu_14.04.tar
docker load < ubuntu_14.04.tar

#### remove a local image
docker rmi image_path

#
#
# container operation
#
#

#### start and run a container with an image
docker run [-name container_name] ubuntu:14.04 /bin/echo 'Hello world'
docker run -t -i ubuntu:14.04 /bin/bash   ### start a container in interactive mode
docker start ubuntu:14.04

docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
docker ps [-a]     ### list containers, [all, inclusive of un-active containers]
docker logs insane_babbage

#### start and stop container
docker restart
docker stop
docker run -idt ubuntu
docker attach container_name

#### start a shell in a container, detach from it, and re-attach to it
docker exec -it container_name /bin/bash
# use Ctrl-p + Ctrl-q to detach from the container
docker attach container_name

#### import and export container 
docker export container_id > snapshot.tar
cat snapshot.tar | docker import - image_path[:tag]
docker import snapshot_url image_path

#### remove container
docker rm container_name
docker rm $(docker ps -aq)  ## remove all exited containers



References:
1.《Docker —— 從入門到實踐­》Run命令

2.How to use Docker without sudo on Ubuntu
  # grant docker permission to specific user 'foobar'
  > sudo setfacl -m user:foobar:rw /var/run/docker.sock
  > getfacl /var/run/docker.sock
  getfacl: 從絕對路徑名尾部去除"/"字符
  # file: var/run/docker.sock
  # owner: root
  # group: docker
  user::rw-
  user:foobar:rw-
  group::rw-
  mask::rw-
  other::---

3.Prepare docker.service.d/override.conf for the systemd drop-in service
  > sudo systemctl edit docker
  [Service]
  ExecStartPost=/bin/sh -c '/usr/bin/setfacl -m user:foobar:rw /var/run/docker.sock'
  > sudo systemctl daemon-reexec	# re-execute the systemd process
  > sudo systemctl daemon-reload	# reload the systemd manager configuration
  > sudo systemctl restart docker	# stop and start the docker service
    
4.Play With Docker provides an Alpine Linux 3.10 environment with docker and git ready for use
     CPU: Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz with 8 cores
     Memory: 32GB
     Drive: 64GB with 16GB used
     Limit: Use of up to 5 nodes for up to 4 hours per session

how to call Java classes in a web page by cheerpj

🚀 在瀏覽器中直接執行 Java:CheerpJ v3 實作指南 長期以來,Java Applets 因為安全性與插件問題早已退出歷史舞台。但隨著 WebAssembly (Wasm) 技術的成熟,現在網頁安裝如下 CheerpJ v3 套件庫,就可以在瀏覽器中直接呼叫 ...

總網頁瀏覽量