1 启动容器
1.1 启动方式
两种启动方式:
- 新建容器并启动;
- 终止状态的容器重新启动。
1.2 新建容器并启动
- 启动命令:
docker run
; - 输出
hello world
,终止容器:
docker run ubuntu:18.04 /bin/echo "hello world"
noamanelson@noamanelson-Virtual-Machine:~$ docker run ubuntu:18.04 /bin/echo "hello world"
hello world
- 启动
bash
,进行用户交互操作:
docker run -t -i ubuntu:18.04 /bin/bash
noamanelson@noamanelson-Virtual-Machine:~$ docker run -t -i ubuntu:18.04 /bin/bash
root@b0d76f1a813b:/#
root@b0d76f1a813b:/#
root@b0d76f1a813b:/#
参数 | 说明 |
---|---|
-t |
让docker 分配一个伪终端并绑定到容器的标准输入上 |
-i |
让容器的标准输入保持打开 |
1.3 docker run时的运行过程
1.4 启动已终止容器
- 命令:
docker container start
;
1.5 后台运行
- 使用
-d
参数即可; - 比如如下,容器会在后台运行,而不会直直接打印:
docker run -d ubuntu:18.04 /bin/echo "sdsdsd"
noamanelson@noamanelson-Virtual-Machine:~$ docker run -d ubuntu:18.04 /bin/echo "sdsdsd"
056508ab9ff61b07a468418bd281b03ee023abbbc514f7a7392d3a59d18ac0dc
noamanelson@noamanelson-Virtual-Machine:~$
- 如果要查看结果,直接使用
docker logs
:
noamanelson@noamanelson-Virtual-Machine:~$ docker logs 056508ab9ff61b07a468418bd281b03ee023abbbc514f7a7392d3a59d18ac0dc
sdsdsd
noamanelson@noamanelson-Virtual-Machine:~$
1.6 查看容器信息
- 命令:
docker container ls
;
noamanelson@noamanelson-Virtual-Machine:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04489fec9611 ubuntu:18.04 "/bin/bash" 12 seconds ago Up 9 seconds modest_nobel
2 终止容器
- 命令:
docker container stop
; - 比如启动一个bash,然后查看容器,再终止容器,再启动容器:
启动:docker container start
重启:docker container restart
docker run -t -i ubuntu:18.04 /bin/bash
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a9622dfb8f8 ubuntu:18.04 "/bin/bash" 51 seconds ago Up 48 seconds exciting_hodgkin
docker container stop 0a9622dfb8f8
3 进入容器
3.1 docker attach
- 如下运行容器,查看容器,进入容器:
noamanelson@noamanelson-Virtual-Machine:~$ docker run -dit ubuntu:18.04
f28907e275433bc54d18c9e791e2d593e0a3b2d55562932a025022ae2e515532
noamanelson@noamanelson-Virtual-Machine:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3931186d9e8 ubuntu:18.04 "/bin/bash" 39 seconds ago Up 36 seconds distracted_hermann
noamanelson@noamanelson-Virtual-Machine:~$ docker attach e393
root@e3931186d9e8:/#
root@e3931186d9e8:/#
root@e3931186d9e8:/#
root@e3931186d9e8:/#
root@e3931186d9e8:/#
- 使用
docker attach
进入容器后,会导致容器停止。
3.2 docker exec
- 也是进入容器,和
docker attach
区别是不会导致容器停止,建议使用这个方式; - 如下运行容器,查看容器,进入容器之后,容器不会停止:
noamanelson@noamanelson-Virtual-Machine:~$ docker run -dit ubuntu:18.04
714618edc1103ff02eda2b2c5f33e4ff48ac15efc1aeda0a9087d6d63b124dbb
noamanelson@noamanelson-Virtual-Machine:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
714618edc110 ubuntu:18.04 "/bin/bash" 4 seconds ago Up 2 seconds jovial_brahmagupta
noamanelson@noamanelson-Virtual-Machine:~$ docker exec -i 7146
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Execute a command in a running container
noamanelson@noamanelson-Virtual-Machine:~$ docker exec -i 7146 bash
ls
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
- 结合-t使用后和
docker attach
类似了:
noamanelson@noamanelson-Virtual-Machine:~$ docker exec -it 7146 bash
root@714618edc110:/#
root@714618edc110:/#
root@714618edc110:/#
root@714618edc110:/#
root@714618edc110:/#
4 导入导出容器
4.1 导出容器
- 命令:
docker export
;
4.2 导入容器
- 命令:
docker import
;
5 删除容器
- 命令:
docker container rm
;
- 这个命令是删除没有运行的容器;
- 如果删除运行中的容器,会提示容器正在运行,无法删除:
noamanelson@noamanelson-Virtual-Machine:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
714618edc110 ubuntu:18.04 "/bin/bash" 13 minutes ago Up 13 minutes jovial_brahmagupta
noamanelson@noamanelson-Virtual-Machine:~$ docker container rm jovial_brahmagupta
Error response from daemon: You cannot remove a running container 714618edc1103ff02eda2b2c5f33e4ff48ac15efc1aeda0a9087d6d63b124dbb. Stop the container before attempting removal or force remove
noamanelson@noamanelson-Virtual-Machine:~$
- 要删除运行中的容器,可以加
-f
参数:
- 删除所有处于终止状态的容器,使用命令
docker container prune
:
使用
docker container ls -a
可以查看终止的容器
使用清除命令: