docker容器管理+镜像管理

目录

一、  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 6379: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

小结

一、什么是镜像?

镜像是一种轻量级,可执行的独立的软件包。包含运行某个软件所需的所有内容,把应用程序和配置依赖打包,形成一个可交付的运行环境(包括代码、运行时需要的库、环境变量和配置文件)这个打包好的运行环境就是image镜像文件

只有通过这个image镜像文件,才能生成docker容器实例 

二、镜像分层

以tomcat为例

下载是有先后次序、分层的

三、UnionFS(联合文件系统)

是一种分层、轻量级并且高性能的文件系统,支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同的目录挂载到同一个虚拟文件系统下。UnionFS文件系统是Docker镜像的基础,镜像可以通过分层来进行继承,基于基础镜像,可以制作具体的应用镜像。

四、docker镜像加载原理


docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统unionFS(联合文件系统)
上层:rootfs
下层:boot
bootfs(boot file system)主要包含bootloader和kernel(内核),
bootloader主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统,在docker镜像的最底层是引导文件系统bootfs,这一层与典型的Linux/unix系统是一样的,包含boot加载器和内核。当boot加载完成之后,整个内核都在内存之中,此时内存的使用权已经由bootfs转交给内核,此时系统就会卸载bootfs。(bootfs是最基础的Linux系统)

rootfs(root file system), 在bootfs之上,包含的就是最典型Linux系统中的/dev  /proc /bin /etc  等标准的目录文件。rootfs就是各种不同的操作系统发行版。比如,ubuntu、centos等(无论ubuntu还是centos,都是最基础、最简单的版本)

对于一个精简的OS,rootfs可以很小,只需要包含最基本的命令,最基本的工具和程序。

因为底层最直接由宿主机的kernel(内核),自己只需要提供rootfs即可。对于不同的Linux发行版,bootfs基本是一致的,而rootfs会有差别,因此不同的发行版公用bootfs(体现出docker的特性和优势,一般centos都是几个G,而docker中只有200M左右)

五、为什么采用分层结构

镜像分层最大的优点就是共享资源,方便复制迁移,为了能够复用

例如有多个镜像都从多个相同的base镜像构建而来,那么docker host(宿主机)只需要在磁盘上保存一份base镜像

同时内存也只需要加载一份base镜像,就可以为所有容器服务。

注意:docker镜像层都是只读的,容器层是可写的

当容器启动时,一个新的可写层被加载到镜像顶部。

这一层通常被称作“容器层”,‘容器层之下都叫“镜像层”’

所有对容器的改动(添加、修改、删除文件)都只会发生在容器层

五层:container(容器层,可写层)

四层:image(添加新功能)

三层:image(添加新功能)

二层:Base image (基础镜像)

一层:bootfs

六、commit案例,分层原理:

原始ubuntu是72.8M,不包含vim命令,添加新功能vim,基于基础镜像添加,ubuntu是原始镜像

更新包管理工具

安装vim

安装完成之后,使用提交副本成为一个新的镜像,使用docker commit

格式:

docker commit -m="提交的描述信息" -a='作者' 容器ID 要创建的目标镜像名称:版本号

查看描述信息

使用创建出来的镜像运行容器,发现有了vim命令和之前创建的文件

docker中的镜像分层,支持通过扩展现有镜像,创建新的镜像。基于一个基础镜像,按需扩展,新镜像就能从bash镜像一层一层叠加生成。每安装一个软件,就在现有的镜像基础之上增加一层

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SKY慕雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值