Docker - Pull Image then start or stop


  • List image
isaac@isaac-KVM:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
  • Pull ubuntu:latest
isaac@isaac-KVM:~$ docker pull ubuntu:latest
latest: Pulling from library/ubuntu
6abc03819f3e: Pull complete
05731e63f211: Pull complete
0bd67c50d6be: Pull complete
Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
Status: Downloaded newer image for ubuntu:latest
isaac@isaac-KVM:~$ docker image list
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              7698f282e524        4 weeks ago         69.9MB
  • Pull nginx:latest
isaac@isaac-KVM:~$ docker pull nginx:latest
latest: Pulling from library/nginx
fc7181108d40: Pull complete
c4277fc40ec2: Pull complete
780053e98559: Pull complete
Digest: sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
Status: Downloaded newer image for nginx:latest
isaac@isaac-KVM:~$
isaac@isaac-KVM:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        4 days ago          109MB
ubuntu              latest              7698f282e524        4 weeks ago         69.9MB
  • Access image by ssh. Ex. ubuntu
    Run a container and attache ssh to it
    "-it" flag switch shell into the terminal of the container
isaac@isaac-KVM:~$ docker container run -it ubuntu:latest /bin/bash
root@81977c50304b:/# ll
total 72
drwxr-xr-x   1 root root 4096 Jun 15 14:35 ./
drwxr-xr-x   1 root root 4096 Jun 15 14:35 ../
-rwxr-xr-x   1 root root    0 Jun 15 14:35 .dockerenv*
drwxr-xr-x   2 root root 4096 May 15 14:07 bin/
drwxr-xr-x   2 root root 4096 Apr 24  2018 boot/
drwxr-xr-x   5 root root  360 Jun 15 14:35 dev/
drwxr-xr-x   1 root root 4096 Jun 15 14:35 etc/
drwxr-xr-x   2 root root 4096 Apr 24  2018 home/
drwxr-xr-x   8 root root 4096 May 23  2017 lib/
drwxr-xr-x   2 root root 4096 May 15 14:06 lib64/
drwxr-xr-x   2 root root 4096 May 15 14:06 media/
drwxr-xr-x   2 root root 4096 May 15 14:06 mnt/
drwxr-xr-x   2 root root 4096 May 15 14:06 opt/
dr-xr-xr-x 245 root root    0 Jun 15 14:35 proc/
drwx------   2 root root 4096 May 15 14:07 root/
drwxr-xr-x   1 root root 4096 May 15 21:20 run/
drwxr-xr-x   1 root root 4096 May 15 21:20 sbin/
drwxr-xr-x   2 root root 4096 May 15 14:06 srv/
dr-xr-xr-x  13 root root    0 Jun 15 14:35 sys/
drwxrwxrwt   2 root root 4096 May 15 14:07 tmp/
drwxr-xr-x   1 root root 4096 May 15 14:06 usr/
drwxr-xr-x   1 root root 4096 May 15 14:07 var/
root@81977c50304b:/#
  • Press Control+PQ to exit container without terminating it
  • List all container by ls
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         16 minutes ago      Up 16 minutes                           gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         22 minutes ago      Up 22 minutes                           cocky_poincare

isaac@isaac-KVM:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        4 days ago          109MB
ubuntu              latest              7698f282e524        4 weeks ago         69.9MB
  • Use "run" command, new container will be started
# List current containers
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         2 hours ago         Up 2 hours                              gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              cocky_poincare

# Run container and attach ssh
isaac@isaac-KVM:~$ docker container run -it ubuntu:latest /bin/bash
root@28a62f8da2d0:/# isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
28a62f8da2d0        ubuntu:latest       "/bin/bash"         7 seconds ago       Up 5 seconds                            affectionate_curran
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         2 hours ago         Up 2 hours                              gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              cocky_poincare

# Image list won't change
isaac@isaac-KVM:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        4 days ago          109MB
ubuntu              latest              7698f282e524        4 weeks ago         69.9MB

# Run container and attach ssh
isaac@isaac-KVM:~$ docker container run -it ubuntu:latest /bin/bash

# New container attached
root@20a69a2f40b0:/docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
20a69a2f40b0        ubuntu:latest       "/bin/bash"         14 seconds ago       Up 13 seconds                           sharp_zhukovsky
28a62f8da2d0        ubuntu:latest       "/bin/bash"         About a minute ago   Up About a minute                       affectionate_curran
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         2 hours ago          Up 2 hours                              gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         3 hours ago          Up 3 hours                              cocky_poincare

# List image, still 2
isaac@isaac-KVM:~$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        4 days ago          109MB
ubuntu              latest              7698f282e524        4 weeks ago         69.9MB
  • Attach shell to terminal of a running container with the docker container exec command. 
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
20a69a2f40b0        ubuntu:latest       "/bin/bash"         3 minutes ago       Up 3 minutes                            sharp_zhukovsky
28a62f8da2d0        ubuntu:latest       "/bin/bash"         4 minutes ago       Up 4 minutes                            affectionate_curran
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              cocky_poincare
isaac@isaac-KVM:~$ docker container exec -it 20a69a2f40b0 bash
  • Use "stop" to stop container by ID or Name
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
20a69a2f40b0        ubuntu:latest       "/bin/bash"         6 minutes ago       Up 6 minutes                            sharp_zhukovsky
28a62f8da2d0        ubuntu:latest       "/bin/bash"         8 minutes ago       Up 7 minutes                            affectionate_curran
4df4b2d4c69e        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              gracious_jones
81977c50304b        ubuntu:latest       "/bin/bash"         3 hours ago         Up 3 hours                              cocky_poincare
isaac@isaac-KVM:~$ docker container stop 20a69a2f40b0
20a69a2f40b0
isaac@isaac-KVM:~$ docker container stop affectionate_curran
affectionate_curran
isaac@isaac-KVM:~$ docker container stop 4df4b2d4c69e
4df4b2d4c69e
isaac@isaac-KVM:~$ docker container stop cocky_poincare
cocky_poincare
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
isaac@isaac-KVM:~$

沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...