docker教程

概述

容器较为官方的解释
一句话概括容器:容器就是将软件打包成标准化单元,以用于开发、交付和部署。

容器镜像是轻量的、可执行的独立软件包 ,包含软件运行所需的所有内容:代码、运行时环境、系统工具、系统库和设置。
容器化软件适用于基于 Linux 和 Windows 的应用,在任何环境中都能够始终如一地运行。
容器赋予了软件独立性,使其免受外在环境差异(例如,开发和预演环境的差异)的影响,从而有助于减少团队间在相同基础设施上运行不同软件时的冲突。
容器较为通俗的解释
如果需要通俗地描述容器的话,我觉得容器就是一个存放东西的地方,就像书包可以装各种文具、衣柜可以放各种衣服、鞋架可以放各种鞋子一样。我们现在所说的容器存放的东西可能更偏向于应用比如网站、程序甚至是系统环境

虚拟化技术和容器化技术

虚拟化技术

首先,Docker 容器虚拟化技术为基础的软件,那么什么是虚拟化技术呢?

简单点来说,虚拟化技术可以这样定义:

虚拟化技术是一种资源管理技术,是将计算机的各种实体资源(CPU、内存、磁盘空间、网络适配器等),予以抽象、转换后呈现出来并可供分割、组合为一个或多个电脑配置环境。由此,打破实体结构间的不可切割的障碍,使用户可以比原本的配置更好的方式来应用这些电脑硬件资源。这些资源的新虚拟部分是不受现有资源的架设方式,地域或物理配置所限制。一般所指的虚拟化资源包括计算能力和数据存储。

Docker 基于 LXC 虚拟容器技术

Docker 技术是基于 LXC(Linux container- Linux 容器)虚拟容器技术的。

LXC,其名称来自 Linux 软件容器(Linux Containers)的缩写,一种操作系统层虚拟化(Operating system–level virtualization)技术,为 Linux 内核容器功能的一个用户空间接口。它将应用软件系统打包成一个软件容器(Container),内含应用软件本身的代码,以及所需要的操作系统核心和库。通过统一的名字空间和共用 API 来分配不同软件容器的可用硬件资源,创造出应用程序的独立沙箱运行环境,使得 Linux 用户可以容易的创建和管理系统或应用容器。

LXC 技术主要是借助 Linux 内核中提供的 CGroup 功能和 name space 来实现的,通过 LXC 可以为软件提供一个独立的操作系统运行环境。

cgroup 和 namespace 介绍:

namespace 是 Linux 内核用来隔离内核资源的方式。 通过 namespace 可以让一些进程只能看到与自己相关的一部分资源,而另外一些进程也只能看到与它们自己相关的资源,这两拨进程根本就感觉不到对方的存在。具体的实现方式是把一个或多个进程的相关资源指定在同一个 namespace 中。Linux namespaces 是对全局系统资源的一种封装隔离,使得处于不同 namespace 的进程拥有独立的全局系统资源,改变一个 namespace 中的系统资源只会影响当前 namespace 里的进程,对其他 namespace 中的进程没有影响。

(以上关于 namespace 介绍内容来自https://www.cnblogs.com/sparkdev/p/9365405.html ,更多关于 namespace 的呢内容可以查看这篇文章 )。

CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制、记录、隔离进程组 (process groups) 所使用的物力资源 (如 cpu memory i/o 等等) 的机制。

(以上关于 CGroup 介绍内容来自 https://www.ibm.com/developerworks/cn/linux/1506_cgroup/index.html ,更多关于 CGroup 的呢内容可以查看这篇文章 )。

cgroup 和 namespace 两者对比:

两者都是将进程进行分组,但是两者的作用还是有本质区别。namespace 是为了隔离进程组之间的资源,而 cgroup 是为了对一组进程进行统一的资源监控和限制。

Docker基本组成
Docker 中有非常重要的三个基本概念,理解了这三个概念,就理解了 Docker 的整个生命周期。

镜像(Image)
容器(Container)
仓库(Repository)
理解了这三个概念,就理解了 Docker 的整个生命周期

docker安装

查看系统内核和系统信息

uname -r #查看系统内核版本
cat /etc/os-release #查看系统版本

开始安装Docker

卸载旧版本

命令:

yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

下载依赖安装包

yum install -y yum-utils

配置镜像仓库

国外的地址·

yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo

设置阿里云的Docker镜像仓库

yum-config-manager \· --add-repo \ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新yum软件包

yum makecache fast

下载docker

yum install docker-ce docker-ce-cli containerd.io # 安装社区版
yum install docker-ee docker-ee-cli containerd.io # 安装企业版

启动Docker

命令:

systemctl start docker # 启动Docker
docker version # 查看当前版本号,是否启动成功
systemctl enable docker # 设置开机自启动

示例:

$ systemctl start docker

$ docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:47:44 2022
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:46:10 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0

Docker的HelloWorld

命令:

docker run hello-world
示例:

$ docker run hello-world
Unable to find image 'hello-world:latest' locally # 本地没有
latest: Pulling from library/hello-world # pull一个最新版
2db29710123e: Pull complete # pull成功
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest

Hello from Docker! # 运行结果
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

  1. The Docker client contacted the Docker daemon.
  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
  3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
  4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Docker卸载

yum remove docker-ce docker-ce-cli containerd.io # 卸载依赖
rm -rf /var/lib/docker # 删除资源 . /var/lib/docker是docker的默认工作路径

配置阿里云镜像

进入阿里云官网,搜索容器镜像服务
执行命令

sudo tee /etc/docker/daemon.json <<-'EOF
{
  "registry-mirrors": ["https://axvfsf7e.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

docker 运行原理

Docker常用命令






镜像命令

docker images 查看本地主机的所有镜像
示例:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
列表参数介绍:

解释:

1.REPOSITORY 镜像的仓库源
2.TAG 镜像的标签
3.IMAGE ID 镜像的id
4.CREATED 镜像的创建时间
5.SIZE 镜像的大小

可选参数

-a/--all 列出所有镜像
-q/--quiet 只显示镜像的id

docker search 搜索镜像

示例:

可选参数

Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output

搜索收藏数大于3000的镜像

$ docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12427 [OK]
mariadb MariaDB Server is a high performing open sou… 4787 [OK]

docker pull 镜像名[:tag] 下载镜像

示例:

$ docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 删除镜像

1.删除指定的镜像id

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id

2.删除多个镜像id

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f 镜像id 镜像id 镜像id

3.删除全部的镜像id

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f $(docker images -aq)

容器命令

docker run [可选参数] image 运行容器

docker run [可选参数] image

参数说明

--name="名字" 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
( -p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口
-p 容器端口)
-P 随机指定端口(大写的P)

示例:

$ docker run c20987f18b13
2022-04-18 07:07:31+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-04-18 07:07:31+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-04-18 07:07:31+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-04-18 07:07:31+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD

进入容器

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it [容器ID] /bin/bash

exit 退出容器

exit 停止并退出容器(后台方式运行则仅退出)

Ctrl+P+Q 不停止容器退出

[root@bd1b8900c547 /]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]#

docker ps列出容器

docker ps

 # 列出当前正在运行的容器

-a # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器 (default -1)
-q # 只显示容器的编号
-s # 显示总文件大小

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
703fad61eccb c20987f18b13 "docker-entrypoint.s…" 2 minutes ago Exited (0) 2 minutes ago inspiring_feistel
2e6f020d0299 c20987f18b13 "docker-entrypoint.s…" 3 minutes ago Exited (1) 3 minutes ago angry_stonebraker
a4889b27716f hello-world "/hello" 14 minutes ago Exited (0) 14 minutes ago trusting_mcclintock

docker rm删除容器

选项:

-f # 移除正在运行的容器(使用SIGKILL)
-l # 移除容器间的网络连接,而非容器本身
-v # 删除与容器关联的卷

docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q | xargs docker rm # 删除所有的容器

启动和重启容器命令

docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前运行的容器
docker kill 容器id #强制停止当前容器

其他命令

后台启动容器

命令 docker run -d 镜像名

$ docker run -d ubuntu
c21cd5dd2594ec109dfb7e8eeba6bd129291de1f1095389c9b31492e98360947

问题docker ps,发现ubuntu停止了

常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用了,就会自动停止

nginx,容器启动后发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

$ docker logs --help

Usage: docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
-f # 跟踪日志输出
--since # 显示某个开始时间的所有日志
-t # 显示时间戳
-n # 仅列出最新N条容器日志(默认为“全部”)

常用:
docker logs -tf 容器id
docker logs --tail number 容器id #num为要显示的日志条数
docker logs -n number 容器id #num为要显示的日志条数

docker容器后台运行,必须要有一个前台的进程,否则会自动停止

编写shell脚本循环执行,使得centos容器保持运行状态

$ docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
f51f3cbb27511b49d85c98fa62691a1a19397d4a272a8cc7d4769d3d6ec41f2a

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f51f3cbb2751 centos "/bin/sh -c 'while t…" 10 seconds ago Up 9 seconds busy_ride

$ docker logs -tf --tail 10 f51f3cbb2751
2022-04-18T07:24:27.364628955Z hi
2022-04-18T07:24:32.365938530Z hi
2022-04-18T07:24:37.367324268Z hi
2022-04-18T07:24:42.368615239Z hi
2022-04-18T07:24:47.369976390Z hi
2022-04-18T07:24:52.371426169Z hi
2022-04-18T07:24:57.372834380Z hi
2022-04-18T07:25:02.374156939Z hi
2022-04-18T07:25:07.375425598Z hi

查看容器中进程信息

命令 docker top 容器id

dockertopf51f3cbb2751UIDPIDPPIDCSTIMETTYTIMECMDroot1787417853015:24?00:00:00/bin/shcwhiletrue;doechohi;sleep5;doneroot1816417874015:25?00:00:00/usr/bin/coreutilscoreutilsprogshebang=sleep/usr/bin/sleep512345 docker inspect 容器id

docker inspect : 获取容器/镜像的元数据。

语法

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

OPTIONS说明:

-f :指定返回值的模板文件。
-s :显示总的文件大小。
--type :为指定类型返回JSON。

进入当前正在运行的容器
方式一:

我们通常容器使用后台方式运行的, 需要进入容器,修改一些配置

命令

docker exec -it 容器id /bin/bash

测试

$ docker exec -it df358bc06b17 /bin/bash
[root@df358bc06b17 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@df358bc06b17 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug11 pts/0 00:00:00 /bin/bash
root 29 0 0 01:06 pts/1 00:00:00 /bin/bash
root 43 29 0 01:06 pts/1 00:00:00 ps -ef

方式二

docker attach 容器id

docker exec # 进入容器后开启一个新的终端,可以在里面操作

docker attach # 进入容器正在执行的终端,不会启动新的进程

语法

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

OPTIONS说明:

-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端

拷贝容器文件到主机
拷贝容器的文件到主机中

docker cp 容器id:容器内路径 目的主机路径

进入容器中

$ docker cp 8b84603c410a:/home/test.java /home

$ ls
alex arod hello.java neos test.java

拷贝只是一个手动功能,未来我们使用 -v 卷的技术,可以实现自动同步

posted @   Takiyo  阅读(157)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示