[docker] 启动docker的container(或者说image的实例)

1 首先查看有哪些image以及有哪些container

sudo docker image ls

# 结果
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
currycode/tf_serving_vc   v1.0                26fdfe329859        2 months ago        3.85GB
currycode/tf_serving_vc   1.0                 d6957b0caf48        2 months ago        3.79GB
ubuntu                    18.04               93fd78260bd1        3 months ago        86.2MB
tensorflow/serving        latest              d42952c6f8a6        4 months ago        230MB
hello-world               latest              4ab4c602aa5e        6 months ago        1.84kB

sudo docker ps -a

# 结果
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS               NAMES
2effa7569ce3        currycode/tf_serving_vc:1.0   "/bin/bash"              2 months ago        Exited (0) 7 weeks ago                          tf_container_vc
a01e03520497        tensorflow/serving            "/usr/bin/tf_serving…"   2 months ago        Exited (137) 2 months ago                       determined_morse
ea05fb751b1a        hello-world                   "/hello"                 2 months ago        Exited (0) 2 months ago                         dazzling_kirch

2 启动container

2.1  如果你之前没有创建container,则需要基于image新创建一个container

docker run --name=tf_container_ASR -p 9001:9001 -p 9002:9002 -td currycode/tf_serving:v0330 
# 也就是从上面的currycode/tf_serving_vc镜像中启动一个container
# i表示获得一个交互式的连接,通过 standard in (STDIN)获取container的输入,也就是让容器的标准输入保持打开。d表示后台运行A
# t表示让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上
# 如果想指定端口和ip映射关系,可以使用-p参数(选项指定Host:Container之间的端口映射关系),默认映射到'172.17.0.2:9000'

# docker run: runs a container.
# tf_container_vc2: is the image you would like to run.
# -t: flag assigns a pseudo-tty or terminal inside the new container.
# -i: flag allows you to make an interactive connection by grabbing the standard in (STDIN) of the container.
# /bin/bash: launches a Bash shell inside our container.

创建成功后会进入container的终端(因为-it参数)

参考:

http://www.runoob.com/docker/docker-image-usage.html

https://docs.docker.com/engine/reference/commandline/run/

https://blog.csdn.net/dongdong9223/article/details/52998375 

https://blog.csdn.net/Yushl_sirius/article/details/76619792

2.2 如果你之前已经创建过container(比如上面docker ps 命令中显示有三个container),则可以直接启动container,不需要从image创建一个新的container了

docker start -i tf_container_vc

# -i表示Attach container’s STDIN
# 成功后如下面第二行所显示会直接进入container的终端
[currycode@mjrc-server11 ~]$ sudo docker start -i tf_container_vc
root@2effa7569ce3:/tensorflow-serving# 

如果使用docker start tf_container_vc 则是在后台运行container
[currycode@mjrc-server11 ~]$ sudo docker start tf_container_vc
tf_container_vc
[currycode@mjrc-server11 ~]$ sudo docker stop tf_container_vc
tf_container_vc

参考:

https://docs.docker.com/engine/reference/commandline/start/

http://www.runoob.com/docker/docker-start-stop-restart-command.html

如果container此时运行在后台(没有使用-i参数),那么需要使用docker attach命令或者docker exec命令进入 他的终端

 2.2.1 docker attach

[currycode@mjrc-server11 ~]$ sudo docker attach tf_container_vc
root@2effa7569ce3:/tensorflow-serving#

但是attach方法有个缺点,exit后container也跟着退出了

root@2effa7569ce3:/tensorflow-serving# exit
exit
[currycode@mjrc-server11 ~]$ sudo docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS               NAMES
2effa7569ce3        currycode/tf_serving_vc:1.0   "/bin/bash"              2 months ago        Exited (0) 3 seconds ago                        tf_container_vc
a01e03520497        tensorflow/serving            "/usr/bin/tf_serving…"   2 months ago        Exited (137) 2 months ago                       determined_morse
ea05fb751b1a        hello-world                   "/hello"                 2 months ago        Exited (0) 2 months ago                         dazzling_kirch

要想退出container时,让container仍然在后台运行着,可以使用“docker exec -it”命令。每次使用这个命令进入container,当退出container后,container仍然在后台运行,命令使用方法如下:

2.2.2 docker exec

[currycode@mjrc-server11 ~]$ sudo docker exec -it tf_container_vc /bin/bash
root@2effa7569ce3:/tensorflow-serving# 

# tf_container_vc :要启动的container的名称
# /bin/bash:在container中启动一个bash shell

这样输入“exit”或者按键“Ctrl + C”退出container时,这个container仍然在后台运行:

root@2effa7569ce3:/tensorflow-serving# exit
exit
[currycode@mjrc-server11 ~]$ sudo docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS               NAMES
2effa7569ce3        currycode/tf_serving_vc:1.0   "/bin/bash"              2 months ago        Up 4 minutes                                    tf_container_vc
a01e03520497        tensorflow/serving            "/usr/bin/tf_serving…"   2 months ago        Exited (137) 2 months ago                       determined_morse
ea05fb751b1a        hello-world                   "/hello"                 2 months ago        Exited (0) 2 months ago                         dazzling_kirch

参考:

https://www.ibm.com/developerworks/community/blogs/132cfa78-44b0-4376-85d0-d3096cd30d3f/entry/%E4%B8%A4%E7%A7%8D%E8%BF%9B%E5%85%A5%E5%AE%B9%E5%99%A8%E7%9A%84%E6%96%B9%E6%B3%95_%E6%AF%8F%E5%A4%A95%E5%88%86%E9%92%9F%E7%8E%A9%E8%BD%AC_Docker_%E5%AE%B9%E5%99%A8%E6%8A%80%E6%9C%AF_23?lang=en

https://blog.csdn.net/dt763C/article/details/82719332

最后查看container IP address:

[currycode@mjrc-server11 ~]$ sudo docker network inspect bridge | grep IPv4Address
                "IPv4Address": "172.17.0.2/16",

其他方法:

[currycode@mjrc-server11 ~]$ sudo docker exec -it tf_container_vc  ip addr | grep global
[currycode@mjrc-server11 ~]$ sudo docker port tf_container_vc

当对container进行了一下修改后,想要永久保存这些修改,需要使用docker commit命令:

[currycode@mjrc-server11 ~]$ sudo docker commit 2effa7569ce3 currycode/tf_serving_vc:v2.0
sha256:90a90c637f62a6819e14f3e6c256926921d4d6f661d941d114d7990b3473c231
[currycode@mjrc-server11 ~]$ sudo docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                      PORTS               NAMES
2effa7569ce3        currycode/tf_serving_vc:1.0   "/bin/bash"              2 months ago        Up 10 hours                                     tf_container_vc
a01e03520497        tensorflow/serving            "/usr/bin/tf_serving…"   2 months ago        Exited (137) 2 months ago                       determined_morse
ea05fb751b1a        hello-world                   "/hello"                 2 months ago        Exited (0) 2 months ago                         dazzling_kirch
[currycode@mjrc-server11 ~]$ sudo docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
currycode/tf_serving_vc   v2.0                90a90c637f62        11 seconds ago      4GB
currycode/tf_serving_vc   v1.0                26fdfe329859        2 months ago        3.85GB
currycode/tf_serving_vc   1.0                 d6957b0caf48        2 months ago        3.79GB
ubuntu                    18.04               93fd78260bd1        3 months ago        86.2MB
tensorflow/serving        latest              d42952c6f8a6        4 months ago        230MB
hello-world               latest              4ab4c602aa5e        6 months ago        1.84kB

参考:

http://www.runoob.com/docker/docker-commit-command.html

删除docker的image(在此之前需要先删除依赖他的所有container):http://yaxin-cn.github.io/Docker/how-to-delete-a-docker-image.html 

你可能感兴趣的:(工具性知识点,实验室环境搭建)