目录
一、 Docker的基本组成
二、 容器和镜像的关系
2.1 面向对象角度
2.2 从镜像容器角度
三、镜像命令
3.1 查看当前已有镜像
3.2 查看已有的全部镜像
3.3 查看镜像ID
3.4 镜像删除
四、 容器命令
4.1 下载镜像
4.2 新建和启动容器 run
4.3 交互式
4.4 后台运行
4.5 部署redis实例
4.6 查看容器运行日志log
4.7 查看容器内部top
4.8 查看容器内部信息
4.9 容器的启动和关闭
4.10 容器删除
五、进入正在运行的容器并以命令交互(2种)
5.1 使用exec
5.2 使用attach
5.3 Redis案例
下载镜像
创建redis容器实例
进入redis
启动redis客户端,连接本地的redis服务
redis简单的常用命令
使用服务
六、从容器内部拷贝文件到主机上
七、导入和导出容器
7.1 导出
7.2 导入
小结
一、 Docker的基本组成
Docker 是一种轻量级的虚拟化容器解决方案,它利用容器来打包应用程序和其依赖项,提供了一种快速部署和运行应用程序的方式。下面是 Docker 的基本组成部分:
镜像(Image):
镜像是 Docker 容器的基础。它包含了应用程序运行所需的所有文件系统内容、库、环境变量和配置等信息。镜像可以用于创建容器实例。
容器(Container):容器是 Docker 镜像的运行实例。每个容器都是相互隔离的,可以在其中运行一个或多个应用程序。容器具有自己的文件系统、网络和进程空间,并且可以被快速创建、启动、停止和删除。
仓库(Repository):仓库是存储 Docker 镜像的地方,可以是本地主机上的仓库,也可以是远程仓库(如 Docker Hub)。用户可以从仓库中拉取镜像到本地使用,也可以将自己创建的镜像推送到仓库中供他人使用。
Docker 守护进程(Docker Daemon):Docker 守护进程是在主机上运行的后台服务,负责管理 Docker 对象,如镜像、容器、网络等。它接收来自 Docker 客户端的请求,并处理这些请求。
Docker 客户端(Docker Client):Docker 客户端是与 Docker 守护进程通信的命令行工具。用户可以通过 Docker 客户端执行各种操作,如构建镜像、运行容器、管理网络等。
网络(Network):Docker 提供了各种网络驱动程序,用于连接 Docker 容器,使它们可以相互通信。用户可以创建自定义网络并将容器连接到这些网络中,实现灵活的网络配置。
数据卷(Volume):数据卷是用于在容器之间共享数据的一种方法。它允许容器访问宿主机上的特定目录或文件,以持久化存储数据或共享文件。
以上是 Docker 的基本组成部分,它们共同构成了 Docker 的核心功能和架构。通过合理使用这些组件,可以更高效地管理和运行应用程序容器。
二、 容器和镜像的关系
2.1 面向对象角度
docker利用容器(container)独立运行的一个和一组应用,应用程序或服务运行在容器里面,容器类似于一个虚拟化的运行环境,容器是用镜像创建的运行实例
镜像是一个静态的定义,容器是镜像运行时的实体
容器为镜像提供了一个标准的隔离的运行环境,它可以被启动、开始、停止、删除
每个容器都是相互隔离的、保证安全的平台
2.2 从镜像容器角度
可以把容器看作是一个简易版的Linux环境(包括root用户权限,进程空间,用户空间和网络空间等)和运行在其中的应用程序
三、镜像命令
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with
column headers (default)
'table TEMPLATE': Print output in table format using
the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go
template.
Refer to https://docs.docker.com/go/formatting/ for more
information about formatting output with templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
3.1 查看当前已有镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
3.2 查看已有的全部镜像
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
3.3 查看镜像ID
[root@localhost ~]# docker images -q
d2c94e258dcb
ba6acccedd29
#通常和-a连用,用来批量删除镜像
[root@localhost ~]# docker images -aq
d2c94e258dcb
ba6acccedd29
3.4 镜像删除
删除指定镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
[root@localhost ~]# docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:4bd78111b6914a99dbc560e6a20eab57ff6655aea4a80c50b0c5491968cbc2e6
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
删除全部镜像
[root@localhost ~]# docker images rmi -f $(docker images -aq)
四、 容器命令
docker必须部署在Linux内核上,如果其他系统想部署docker就必须安装一个虚拟的Linux环境
因此,docker自带一个迷你的、微小版的Linux环境
实际的环境中,必须有镜像才能创建容器,这是根本前提
从上到下的层次关系
centos7镜像 =>
docker容器 =>
centos7.9 =>
VMware =>
Windows =>
台式机/笔记本电脑如果是Ubuntu镜像
Ubuntu镜像 =>
docker容器 =>
centos7.9 =>
VMware =>
Windows =>
台式机/笔记本电脑
4.1 下载镜像
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
4.2 新建和启动容器 run
使用run按照镜像,生成一个个的容器实例(相当于安装的一个个的虚拟机实例),也就是鲸鱼背上的集装箱
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
[OPTIONS] 说明
--name 容器名字 =>为容器指定一个名称,若不指定则随机分配
-d 后台运行模式,并返回容器ID,也称启动守护式容器(后台运行)-i 以交互式模式运行容器,通常与 -t 一起使用
-t 为容器重新分配一个伪输入终端,通常与 -i 一起使用
也即 启动交互式容器(前台有伪终端,等待交互)-P 随机端口映射(系统随机分配),大写P
-p 指定端口映射,小写p
-p hostPort:containerPort =>主机端口号:容器内端口号 例 -p 80:8080
容器实例是运行在docker上,访问容器实例
以redis为例,首先要指定宿主机(docker)暴露的6379端口,在docker内部找6379端口的Redis容器,即为-p 6379s:6379;左边是访问宿主机暴露的端口,右边是docker访问Redis提供的端口
直接运行
[root@localhost ~]# docker run ubuntu
#没有任何返回值,使用docker ps 也没有正在运行的
4.3 交互式
使用镜像centos:latest以交互式启动一个容器,在容器内执行/bin/bash命令(表示在载入容器后运行bash,docker中必须保持一个进程的运行,否则整个容器就会退出,这个就表示启动容器之后启动bash)
(Bash,Unix,shell的一种,Bash是一个命令处理器,通常运行于文本窗口,并能够执行用户直接输入的命令,Bash还能从文件中读取命令,这样的文件称为脚本)
docker run -it centos /bin/bash
-i 交互式
-t 终端
centos 镜像,没加latest,默认是最新版,否则需要加TAG
/bin/bash 放在镜像后的命令,希望以一个交互式的shell,因此使用/bin/bash
需要退出终端,直接输入exit
如果镜像关闭,使用以下命令,重新进入docker容器
docekr exec -it 容器名称(或ID) bash
[root@localhost ~]# docker ps --help
Usage: docker ps [OPTIONS]
[OPTIONS]
-a 显示全部容器,正在运行的+历史运行过的
-l 显示最近创建
-n 显示最近n个创建的容器
-q 只显示容器ID,静默模式
实例
用exit退出会自动停止容器运行,使用ctrl+p+q退出并不会停止运行
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
[root@localhost ~]#
[root@localhost ~]# docker run -it -P --name wq-test ubuntu
root@da93329d147f:/#
4.4 后台运行
直接-d,容器自动停止
后台运行必须有一个前台进程(docker机制)
[root@localhost ~]# docker run -d ubuntu
c86fe1c9bd0308eae62c70cccd3cf913f607909dcb3d6fdd237deda611dc162f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c86fe1c9bd03 ubuntu "bash" 4 seconds ago Exited (0) 4 seconds ago vigilant_jepsen
例如,ubuntu、nginx,如果是以后台运行,就会导致前台没有运行的应用,类似这样的容器后台启动后,就会立即停止,解决方案是,将要运行的容器以前台进程的形式运行,常见的是命令行模式(-it)
不加/bin/bash或bash,默认会带shell脚本运行
[root@localhost ~]# docker run -d ubuntu
3311284af50180025cdbc6e6933b8f9f9f9b5f8bfacc69b54f606073c7100e5d
[root@localhost ~]# docker run -it ubuntu
root@b28af4f70f84:/#
4.5 部署redis实例
下载redis
[root@localhost ~]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete
1ed3521a5dcb: Pull complete
5999b99cee8f: Pull complete
3f806f5245c9: Pull complete
f8a4497572b2: Pull complete
eafe3b6b8d06: Pull complete
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
redis 6.0.8 16ecd2772934 3 years ago 104MB
以交互式方式运行redis
[root@localhost ~]# docker run -it redis:6.0.8
1:C 13 Mar 2024 01:21:02.019 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 13 Mar 2024 01:21:02.019 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 13 Mar 2024 01:21:02.019 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 13 Mar 2024 01:21:02.020 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 13 Mar 2024 01:21:02.020 # Server initialized
1:M 13 Mar 2024 01:21:02.020 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 13 Mar 2024 01:21:02.020 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 13 Mar 2024 01:21:02.020 * Ready to accept connections
这种交互式的模式,是不安全的,因为这个界面需要一直挂着,使用ctrl+c这个容器就会被终止
使用后台方式运行redis,
对于redis,mysql这类服务,使用-d进行后台运行
[root@localhost ~]# docker run -d redis:6.0.8
b9e19344da5e9172b8b90cf19948164915f08c6f931d02d8096146fdf5225e55
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9e19344da5e redis:6.0.8 "docker-entrypoint.s…" 2 seconds ago Up 1 second 6379/tcp trusting_kowalevski
从这可以体会到docker的便捷,秒级启动
4.6 查看容器运行日志log
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9e19344da5e redis:6.0.8 "docker-entrypoint.s…" 2 seconds ago Up 1 second 6379/tcp trusting_kowalevski
[root@localhost ~]# docker logs b9e19344da5e
1:C 13 Mar 2024 01:25:16.939 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 13 Mar 2024 01:25:16.939 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 13 Mar 2024 01:25:16.939 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 13 Mar 2024 01:25:16.940 * Running mode=standalone, port=6379.
1:M 13 Mar 2024 01:25:16.940 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 13 Mar 2024 01:25:16.940 # Server initialized
1:M 13 Mar 2024 01:25:16.940 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 13 Mar 2024 01:25:16.940 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 13 Mar 2024 01:25:16.940 * Ready to accept connections
4.7 查看容器内部top
[root@localhost ~]# docker top b9e19344da5e
UID PID PPID C STIME TTY TIME CMD
polkitd 12632 12611 0 21:25 ? 00:00:00 redis-server *:6379
4.8 查看容器内部信息
[root@localhost ~]# docker inspect b9e19344da5e
4.9 容器的启动和关闭
[root@localhost ~]# docker stop wq-test
wq-test
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd321849f520 ubuntu "bash" 2 minutes ago Up 2 minutes wq-test2
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd321849f520 ubuntu "bash" 2 minutes ago Up 2 minutes wq-test2
9a5beffef401 ubuntu "bash" 7 minutes ago Exited (0) 4 seconds ago wq-test
[root@localhost ~]# docker start wq-test
wq-test
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd321849f520 ubuntu "bash" 3 minutes ago Up 3 minutes wq-test2
9a5beffef401 ubuntu "bash" 8 minutes ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp wq-test
4.10 容器删除
关闭已经停止的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8217bc62d95c ubuntu "bash" 3 seconds ago Exited (0) 2 seconds ago w01
dd321849f520 ubuntu "bash" 11 minutes ago Up 11 minutes wq-test2
[root@localhost ~]# docker rm w01
w01
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd321849f520 ubuntu "bash" 11 minutes ago Up 11 minutes wq-test2
关闭正在运行的容器
[root@localhost ~]# docker rm -f wq-test2
wq-test2
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
批量删除
[root@localhost ~]# docker run -d ubuntu
cdee66294ad85365e319c43f91003d2cd47e768eaf8b383da70f7c4889a8fe0a
[root@localhost ~]# docker run -d ubuntu
8bef244a48bc1b3b5b00b51ca1d64b33525f315c8225477916caa75f55237b5a
[root@localhost ~]# docker run -d ubuntu
4bd4fc2af724f724286318046de0acda6d1dec727638ae167df6c9e9fe92fc90
[root@localhost ~]# docker run -d ubuntu
d449c3a51ff0e54dd27db62ef341c0cc296df799484ed1e280001d4dae205bb6
[root@localhost ~]# docker ps -aq
d449c3a51ff0
4bd4fc2af724
8bef244a48bc
cdee66294ad8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d449c3a51ff0 ubuntu "bash" 15 seconds ago Exited (0) 14 seconds ago nostalgic_black
4bd4fc2af724 ubuntu "bash" 15 seconds ago Exited (0) 15 seconds ago frosty_bardeen
8bef244a48bc ubuntu "bash" 16 seconds ago Exited (0) 16 seconds ago frosty_franklin
cdee66294ad8 ubuntu "bash" 19 seconds ago Exited (0) 18 seconds ago objective_saha
[root@localhost ~]# docker rm -f $(docker ps -aq)
d449c3a51ff0
4bd4fc2af724
8bef244a48bc
cdee66294ad8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
五、进入正在运行的容器并以命令交互(2种)
5.1 使用exec
下载镜像
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 10 months ago 13.3kB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
启动Ubuntu容器实例,并使用ctrl+p+q退出
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@9d73b80dd830:/# [root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d73b80dd830 ubuntu "/bin/bash" About a minute ago Up About a minute stoic_blackwell
重新进入容器
[root@localhost ~]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Execute a command in a running container
Aliases:
docker container exec, docker exec
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: "<name|uid>[:<group|gid>]")
-w, --workdir string Working directory inside the container
使用exec重新进入容器,再使用exit退出,容器不会停止
[root@localhost ~]# docker exec -it 9d73b80dd830 /bin/bash
root@9d73b80dd830:/# ll
total 0
drwxr-xr-x. 1 root root 6 Mar 14 00:32 ./
drwxr-xr-x. 1 root root 6 Mar 14 00:32 ../
-rwxr-xr-x. 1 root root 0 Mar 14 00:32 .dockerenv*
lrwxrwxrwx. 1 root root 7 Oct 6 2021 bin -> usr/bin/
drwxr-xr-x. 2 root root 6 Apr 15 2020 boot/
drwxr-xr-x. 5 root root 360 Mar 14 00:32 dev/
drwxr-xr-x. 1 root root 66 Mar 14 00:32 etc/
drwxr-xr-x. 2 root root 6 Apr 15 2020 home/
lrwxrwxrwx. 1 root root 7 Oct 6 2021 lib -> usr/lib/
lrwxrwxrwx. 1 root root 9 Oct 6 2021 lib32 -> usr/lib32/
lrwxrwxrwx. 1 root root 9 Oct 6 2021 lib64 -> usr/lib64/
lrwxrwxrwx. 1 root root 10 Oct 6 2021 libx32 -> usr/libx32/
drwxr-xr-x. 2 root root 6 Oct 6 2021 media/
drwxr-xr-x. 2 root root 6 Oct 6 2021 mnt/
drwxr-xr-x. 2 root root 6 Oct 6 2021 opt/
dr-xr-xr-x. 140 root root 0 Mar 14 00:32 proc/
drwx------. 2 root root 37 Oct 6 2021 root/
drwxr-xr-x. 5 root root 58 Oct 6 2021 run/
lrwxrwxrwx. 1 root root 8 Oct 6 2021 sbin -> usr/sbin/
drwxr-xr-x. 2 root root 6 Oct 6 2021 srv/
dr-xr-xr-x. 13 root root 0 Mar 13 23:57 sys/
drwxrwxrwt. 2 root root 6 Oct 6 2021 tmp/
drwxr-xr-x. 13 root root 145 Oct 6 2021 usr/
drwxr-xr-x. 11 root root 139 Oct 6 2021 var/
root@9d73b80dd830:/# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d73b80dd830 ubuntu "/bin/bash" 5 minutes ago Up 5 minutes stoic_blackwell
5.2 使用attach
docker attach 容器ID
区别:
attach直接进入容器启动命令的终端,不会重新启动新的进程,用exit退出,会导致容器停止
exec是在容器打开新的终端,并且可以启动新的进程,用exit退出,不会导致容器的停止
使用attach重新进入,exit退出,容器停止
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d73b80dd830 ubuntu "/bin/bash" 5 minutes ago Up 5 minutes stoic_blackwell
[root@localhost ~]# docker attach 9d73b80dd830
root@9d73b80dd830:/# pwd
/
root@9d73b80dd830:/# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.3 Redis案例
下载镜像
[root@localhost ~]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete
1ed3521a5dcb: Pull complete
5999b99cee8f: Pull complete
3f806f5245c9: Pull complete
f8a4497572b2: Pull complete
eafe3b6b8d06: Pull complete
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 years ago 72.8MB
redis 6.0.8 16ecd2772934 3 years ago 104MB
创建redis容器实例
[root@localhost ~]# docker run -d --name redis-test redis:6.0.8
be3f7c646e73a150e2b0d96c91b3020041a0437e3c4807fffabe57972dc3810d
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be3f7c646e73 redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 6379/tcp redis-test
进入redis
[root@localhost ~]# docker exec -it redis-test /bin/bash
root@be3f7c646e73:/data# pwd
/data
redis客户端基本语法:$redis-cli 端口号:-p 6379
启动redis客户端,连接本地的redis服务
root@be3f7c646e73:/data# redis-cli -p 6379
127.0.0.1:6379>
ping命令用于检测redis服务是否启动
127.0.0.1:6379> ping
PONG
redis简单的常用命令
1. SET key value
设置指定的key的值
2. GET key
获取指定的key的值
使用服务
127.0.0.1:6379> SET k1 v1
OK
127.0.0.1:6379> GET k1
"v1"
127.0.0.1:6379>
一般使用-d后台启动的程序,再用exec重新进入对应的容器实例
六、从容器内部拷贝文件到主机上
格式:docker cp 容器ID:容器内路径 目的主机路径
创建新的容器实例
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@4cf5a5915e36:/#
在tmp目录下新建文件
root@4cf5a5915e36:/# pwd
/
root@4cf5a5915e36:/# cd tmp/
root@4cf5a5915e36:/tmp# ll
total 0
drwxrwxrwt. 2 root root 6 Oct 6 2021 ./
drwxr-xr-x. 1 root root 6 Mar 14 01:29 ../
root@4cf5a5915e36:/tmp# touch a.txt
root@4cf5a5915e36:/tmp# ll
total 0
drwxrwxrwt. 1 root root 19 Mar 14 01:30 ./
drwxr-xr-x. 1 root root 17 Mar 14 01:29 ../
-rw-r--r--. 1 root root 0 Mar 14 01:30 a.txt
使用ctrl+p+q退出
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cf5a5915e36 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes nice_noyce
be3f7c646e73 redis:6.0.8 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 6379/tcp redis-test
进行docker cp
[root@localhost ~]# pwd
/root
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1572 Jan 10 06:20 anaconda-ks.cfg
[root@localhost ~]# docker cp 4cf5a5915e36:/tmp/a.txt /root/
Successfully copied 1.54kB to /root/
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1572 Jan 10 06:20 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 13 21:30 a.txt
七、导入和导出容器
export导出容器的内容作为tar文件
import 从tar包中的内容创建一个新的文件系统再导入为镜像
格式:docker export 容器ID > 文件名.tar
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
7.1 导出
[root@localhost ~]# docker export 4cf5a5915e36 > wq.tar
[root@localhost ~]# ll
total 73404
-rw-------. 1 root root 1572 Jan 10 06:20 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 13 21:30 a.txt
-rw-r--r--. 1 root root 75158016 Mar 13 21:39 wq.tar
7.2 导入
利用镜像创建容器实例,发现之前创建的文件还在
# 先删除之前的容器实例
[root@localhost ~]# docker rm -f 4cf5a5915e36
4cf5a5915e36
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be3f7c646e73 redis:6.0.8 "docker-entrypoint.s…" 22 minutes ago Up 22 minutes 6379/tcp redis-test
#进行导入
[root@localhost ~]# cat wq.tar | docker import - wq2/ubuntu:18.04
sha256:31424c420e750c07f401b1716b72e781b0949a6ebb9bed8812998c20ee4bffa5
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wq2/ubuntu 18.04 31424c420e75 9 seconds ago 72.8MB
ubuntu latest ba6acccedd29 2 years ago 72.8MB
redis 6.0.8 16ecd2772934 3 years ago 104MB
[root@localhost ~]# docker run -it 31424c420e75 /bin/bash
root@b921ddd90e45:/# cd /tmp/
root@b921ddd90e45:/tmp# ll
total 0
drwxrwxrwt. 2 root root 19 Mar 14 01:30 ./
drwxr-xr-x. 1 root root 6 Mar 14 01:42 ../
-rw-r--r--. 1 root root 0 Mar 14 01:30 a.txt