Docker镜像:docker镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。
Docker容器:镜像运行时的实体,Docker利用容器来运行应用。镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。
Docker仓库:集中存放镜像文件的地方。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本 。仓库分为公开仓库( Public)和私有仓库( Private) 两种形式,即Docker Registry 公开服务和私有Docker Registry 。
#服务器版本
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
#内核版本
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
添加docker-ce源
[root@localhost ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[root@localhost ~]# yum-config-manager --add-repo
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
修改docker-ce源
[root@localhost ~]# sed -i
'[email protected]@mirrors.tuna.tsinghua.edu.cn/docker-ce@g'
/etc/yum.repos.d/docker-ce.repo
#以下内容可能需要修改
[root@localhost ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/docker-ce.repo
[root@localhost ~]# sed -i 's/$basearch/x86_64/g' /etc/yum.repos.d/docker-ce.repo
更新源
[root@localhost ~]# yum makecache fast
安装docker-ce
#默认安装是最新版本
[root@localhost ~]# yum install docker-ce -y
#启动docker服务
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2023-08-23 14:29:15 CST; 3s ago
Docs: https://docs.docker.com
Main PID: 1482 (dockerd)
Tasks: 7
Memory: 41.0M
CGroup: /system.slice/docker.service
└─1482 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker info
#如果出现下面两行就代表配置成功
Registry Mirrors:
https://docker.mirrors.ustc.edu.cn/
注:如果出现这两行
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@localhost ~]# vim /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
[root@localhost ~]# sysctl -p 从配置文件/etc/sysctl.conf加载内核参
数设置
#下载ubuntu镜像
[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
b237fe92c417: Pull complete
Digest: sha256:ec050c32e4a6085b423d36ecd025c0d3ff00c38ab93a3d71a460ff1c44fa6d77
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
#下载centos7镜像
[root@localhost ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
#检验
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 01f29b872827 2 weeks ago 77.8MB
centos 7 eeb6ee3f44bd 23 months ago 204MB
[root@localhost ~]# docker run --name chenming -it centos:7
[root@b8c93deef3e7 /]# exit
exit
[root@localhost ~]# docker run --name cm -it ubuntu /bin/bash
root@9cf78c0a255a:/# exit
exit
docker start cm 启动容器
docker stop cm 停止容器
docker restart cm 重启容器
#启动容器
[root@localhost ~]# docker start cm
cm
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cf78c0a255a ubuntu "/bin/bash" 4 minutes ago Up 2 seconds cm
#停止容器
[root@localhost ~]# docker stop cm
cm
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#重启容器
[root@localhost ~]# docker restart cm
cm
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cf78c0a255a ubuntu "/bin/bash" 5 minutes ago Up 2 seconds cm
通过
docker ps --help
-a, --all Show all containers (default shows just running)
所以 docker ps 查看正在运行的容器
docker ps -a 查看所有的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cf78c0a255a ubuntu "/bin/bash" 9 minutes ago Up 3 minutes cm
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cf78c0a255a ubuntu "/bin/bash" 9 minutes ago Up 3 minutes cm
b8c93deef3e7 centos:7 "/bin/bash" 10 minutes ago Exited (137) 6 minutes ago chenming
第一种
#在容器内部直接输入exit退出
[root@localhost ~]# docker run --name cm -it ubuntu /bin/bash
root@9cf78c0a255a:/# exit
exit
第二种
#使用ctrl+d退出
[root@localhost ~]# docker exec -it cm /bin/bash
root@9cf78c0a255a:/#
exit
[root@localhost ~]# docker exec -it cm /bin/bash
root@9cf78c0a255a:/#
[root@localhost ~]# docker inspect cm
[
{
"Id": "9cf78c0a255a17250a6345d6ca157731b10ab9b2a09b9cbc1af3d6546d3920ef",
"Created": "2023-08-23T06:53:08.74171292Z",
"Path": "/bin/bash",
"Args": [],
···
查看所有镜像有两种方式
docker images
docker image ls
两种方式是等价的
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 01f29b872827 2 weeks ago 77.8MB
centos 7 eeb6ee3f44bd 23 months ago 204MB
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 01f29b872827 2 weeks ago 77.8MB
centos 7 eeb6ee3f44bd 23 months ago 204MB