docker 启动容器相关操作

2019-03-05  本文已影响0人  75221f946ec3

启动容器相关操作:

运行一个容器

# 运行容器,并再容器运行后执行pwd命令
root@jyq-virtual-machine:/# docker run  ubuntu pwd
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6cf436f81810: Pull complete 
987088a85b96: Pull complete 
b4624b3efe06: Pull complete 
d42beb8ded59: Pull complete 
Digest: sha256:7a47ccc3bbe8a451b500d2b53104868b46d60ee8f5b35a24b41a86077c650210
Status: Downloaded newer image for ubuntu:latest
/  # pwd命令的输出
# 容器桩体为exited,说明容器已经退出停止
root@jyq-virtual-machine:/# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                    NAMES
410e5ec23e74        ubuntu              "pwd"                    8 seconds ago       Exited (0) 7 seconds ago                            upbeat_leakey

在后台运行一个容器

为了保证提供服务,不能让容器运行后立马退出,所以需要使容器保持常时间运行
运行容器时使用-d参数可以使容器在后台一直处于运行状态

root@jyq-virtual-machine:/# docker run -d  ubuntu  /bin/bash -c 'while true; do sleep 1; done'
0346f58ef16c493be70ff0bc1a29e3c960d587210ea53bd669bcfdfb6ef4bfcd
root@jyq-virtual-machine:/# docker  ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0346f58ef16c        ubuntu              "/bin/bash -c 'while…"   6 seconds ago       Up 5 seconds                                 eloquent_jennings

启动操作系统后必须执行一个可以一直运行的程序,不然即使使用-d参数,操作系统因为没有运行的任务状态将为created,看不出up的状态。

查看容器运行时执行的历史命令

注意:history 后面必须是镜像的名称,而不是容器的id

root@jyq-virtual-machine:/# docker history  ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
47b19964fb50        3 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           3 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           3 weeks ago         /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           3 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   745B                
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:529264c6593975a61…   88.1MB  

启动容器时给容器命名:

使用--name 参数在容器启动时给容器命名一个自己容易识别的名称:

root@jyq-virtual-machine:/# docker  run --name my-web -d -p 8081:80 httpd
a4eee9b6872928748847db3637cc4087cf5a7db7c91dac609e8c68264abcc101
root@jyq-virtual-machine:/# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a4eee9b68729        httpd               "httpd-foreground"       5 seconds ago       Up 5 seconds        0.0.0.0:8081->80/tcp     my-web

停止一个容器:

docker stop [长ID|短ID|name]

root@jyq-virtual-machine:/# docker  run --name my-web -d -p 8081:80 httpd
a4eee9b6872928748847db3637cc4087cf5a7db7c91dac609e8c68264abcc101
root@jyq-virtual-machine:/# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a4eee9b68729        httpd               "httpd-foreground"       5 seconds ago       Up 5 seconds        0.0.0.0:8081->80/tcp     my-web
root@jyq-virtual-machine:/# docker stop a4eee9b68729
a4eee9b68729
root@jyq-virtual-machine:/# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
a4eee9b68729        httpd               "httpd-foreground"       6 minutes ago       Exited (0) 8 seconds ago                             my-web

怎么进入一个已经运行的容器:

attach 和exec的区别:

容器运行分类:

停止容器、启动容器和重启容器:

容器的挂起和解除挂起

删除容器:

创建容器和启动容器:

上一篇 下一篇

猜你喜欢

热点阅读