container-run-bash.md 1.3 KB
Newer Older
微枫Micromaple's avatar
微枫Micromaple 已提交
1
# docker container 操作(2): 运行,进入交互式shell环境
F
feilong 已提交
2 3 4 5 6 7 8 9 10 11 12

一个Docker镜像(image)运行后,就是一个容器实例,称为`container`

运行容器后可以通过参数指定执行的第一个程序,例如可以先拉取`ubuntu`的容器镜像

```bash
docker image pull  library/ubuntu
```

下载 ubuntu 镜像成功

F
fix bug  
feilong 已提交
13
![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/1.容器(docker)/3.docker container 操作/container-run-bash-ubuntu.jpg)
F
fix bug  
feilong 已提交
14
<br/>
F
feilong 已提交
15 16 17 18 19 20 21 22 23

接着,启动container 并执行第一个程序`bin/bash`:

```bash
docker container run -it ubuntu bin/bash
```

可以看到成功进入了ubuntu容器实例的shell环境,并在shell环境里执行了`ls``ll`命令。

F
feilong 已提交
24
![](https://gitcode.net/csdn/skill_tree_cloud_native/-/raw/master/data/1.云原生初阶/1.容器(docker)/3.docker container 操作/container-run-bash-ubuntu-shell.jpg)
F
fix bug  
feilong 已提交
25
<br/>
F
feilong 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

以下描述错误的是?

## 答案

所有的容器都能执行 `/bin/bash` 程序

## 选项

### A

可以在启动容器的时候指定执行的第一个程序

### B

如果容器程序带有 bash 程序,就可以在启动的时候指定执行bash程序从而进入容器里的shell环境。

### C

因为指定了参数`-it`,因此可以直接在启动容器的终端上输入命令,回车交给容器里的shell环境执行命令。