Docker笔记5 | 容器的基本操作
  NHjqxS4aAIAZ 2023年11月02日 16 0

(5 | 容器的基本操作)

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时的运行过程

image.png

image.png

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可以查看终止的容器

在这里插入图片描述

使用清除命令:

在这里插入图片描述

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  uRXtDv0LQww7   2023年12月09日   15   0   0 Ubuntu
NHjqxS4aAIAZ
最新推荐 更多