1. 安装依赖包
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2. 配置 docker 源
~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 查看源是否配置上 docker-ce.repo
~]# cd /etc/yum.repos.d
yum.repos.d]# ls
bak centos-addons.repo centos.repo docker-ce.repo
# 修改源地址
~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# 安装Docker
~]# yum -y install docker-ce
# 启动docker
~]# systemctl enable docker && systemctl start docker
# 查看docker服务状态
~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; preset: disabled)
Active: active (running) since Mon 2024-02-26 21:11:02 CST; 52s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 10609 (dockerd)
Tasks: 17
Memory: 37.9M
CPU: 739ms
CGroup: /system.slice/docker.service
└─10609 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 配置镜像加速
[root@docker ~]# cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors":[
"https://bhtknto5.mirror.aliyuncs.com",
"https://yo3sdl2l.mirror.aliyuncs.com",
"https://blkjc4x3.mirror.aliyuncs.com",
"https://gfmnzvu1.mirror.aliyuncs.com",
"https://yxzrazem.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
[root@docker ~]#
[root@docker ~]# cat /etc/docker/daemon.json
{
"registry-mirrors":[
"https://bhtknto5.mirror.aliyuncs.com",
"https://yo3sdl2l.mirror.aliyuncs.com",
"https://blkjc4x3.mirror.aliyuncs.com",
"https://gfmnzvu1.mirror.aliyuncs.com",
"https://yxzrazem.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
],
"exec-opts": ["native.cgroupdriver=systemd"]
}
# 重启使配置生效
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
# 查看docker自动生成的网桥
[root@docker ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:f1:e3:a5:88 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.常用命令
# 查看docker概要信息
docker info
# 查看docker镜像
~]# docker images
# 下载一个镜像 比如:centos:7
[root@docker ~]# docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 2 years ago 204MB
# 使用镜像,创建一个虚拟机(容器)
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 2 years ago 204MB
# it => 表示交互模式, rm => 表示退出容器的时候自动删除容器, centos:7 镜像名字:TAG, /bin/bash 容器的运行shell环境(ls,mv,cp)
[root@docker ~]# docker run -it --rm centos:7 /bin/bash
[root@11b92ff9a123 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@11b92ff9a123 /]# ls /root/
anaconda-ks.cfg
[root@11b92ff9a123 /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
# 使用新镜像ubuntu18,后台模式运行容器
[root@docker ~]# docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 20.04 ba6acccedd29 2 years ago 72.8MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@docker ~]# docker run -it -d ubuntu:20.04 /bin/bash # 后台运行模式
07067ffcd9d5ecfbdf4d969f549ac018ca965e3ff33d408ad899819a8c37d8e6
[root@docker ~]#
[root@docker ~]# docker ps # 查看正在运行的容器 -a => 查看所有运行的或者关机的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 4 seconds ago Up 3 seconds gallant_volhard
[root@docker ~]#
[root@docker ~]# docker exec -it 07067ffcd9d5 /bin/bash # 进入后台运行的容器
root@07067ffcd9d5:/#
root@07067ffcd9d5:/#
root@07067ffcd9d5:/#
root@07067ffcd9d5:/#
root@07067ffcd9d5:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
root@07067ffcd9d5:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@07067ffcd9d5:/#
root@07067ffcd9d5:/# exit # 退出容器
exit
[root@docker ~]# docker ps # 容器依然运行着
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 54 seconds ago Up 53 seconds gallant_volhard
# 启动mysql容器
[root@docker ~]# docker pull mysql
Using default tag: latest
latest: 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
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@docker ~]#
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 2 years ago 516MB
ubuntu 20.04 ba6acccedd29 2 years ago 72.8MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@docker ~]# docker run -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
d89bec2b9c711dff7586454725e74af74a6bf3f9e56d767d96d77f53b215fe7f
[root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d89bec2b9c71 mysql:latest "docker-entrypoint.s…" 3 seconds ago Up 3 seconds 3306/tcp, 33060/tcp laughing_hypatia
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 13 minutes ago Up 13 minutes gallant_volhard
[root@docker ~]#
[root@docker ~]# docker exec -it d89bec2b9c71 /bin/bash
root@d89bec2b9c71:/#
root@d89bec2b9c71:/# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql>
# 创建一个nginx容器
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 years ago 141MB
mysql latest 3218b38490ce 2 years ago 516MB
ubuntu 20.04 ba6acccedd29 2 years ago 72.8MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@docker ~]#
[root@docker ~]# docker run -it -d -p 10082:80 nginx:latest /bin/bash
c271ced3148106d01f52ab6f653b22fc916c53090fa5c6289ac967c09abc1923
[root@docker ~]#
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c271ced31481 nginx:latest "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:10082->80/tcp, :::10082->80/tcp inspiring_cray
d89bec2b9c71 mysql:latest "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 3306/tcp, 33060/tcp laughing_hypatia
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 21 minutes ago Up 21 minutes gallant_volhard
[root@docker ~]#
[root@docker ~]# docker exec -it c271ced31481 /bin/bash
root@c271ced31481:/# echo "This is my web" > /usr/share/nginx/html/index.html
root@c271ced31481:/# nginx start
nginx: invalid option: "start"
root@c271ced31481:/# nginx
2024/02/26 14:12:25 [notice] 14#14: using the "epoll" event method
2024/02/26 14:12:25 [notice] 14#14: nginx/1.21.5
2024/02/26 14:12:25 [notice] 14#14: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2024/02/26 14:12:25 [notice] 14#14: OS: Linux 5.14.0-375.el9.x86_64
2024/02/26 14:12:25 [notice] 14#14: getrlimit(RLIMIT_NOFILE): 1073741816:1073741816
2024/02/26 14:12:25 [notice] 15#15: start worker processes
2024/02/26 14:12:25 [notice] 15#15: start worker process 16
root@c271ced31481:/# 2024/02/26 14:12:25 [notice] 15#15: start worker process 17
2024/02/26 14:12:25 [notice] 15#15: start worker process 18
2024/02/26 14:12:25 [notice] 15#15: start worker process 19
2024/02/26 14:12:25 [notice] 15#15: start worker process 20
2024/02/26 14:12:25 [notice] 15#15: start worker process 21
2024/02/26 14:12:25 [notice] 15#15: start worker process 22
2024/02/26 14:12:25 [notice] 15#15: start worker process 23
2024/02/26 14:12:25 [notice] 15#15: start worker process 24
2024/02/26 14:12:25 [notice] 15#15: start worker process 25
2024/02/26 14:12:25 [notice] 15#15: start worker process 26
2024/02/26 14:12:25 [notice] 15#15: start worker process 27
# 宿主机查看nginx
[root@docker ~]# curl 127.0.0.1:10082
This is my web
# 物理机浏览器直接访问
http://10.1.1.11:10082/
# 停止/删除容器
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c271ced31481 nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:10082->80/tcp, :::10082->80/tcp inspiring_cray
d89bec2b9c71 mysql:latest "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 3306/tcp, 33060/tcp laughing_hypatia
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 27 minutes ago Up 27 minutes gallant_volhard
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker stop c271ced31481
c271ced31481
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c271ced31481 nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (137) 2 seconds ago inspiring_cray
d89bec2b9c71 mysql:latest "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 3306/tcp, 33060/tcp laughing_hypatia
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 28 minutes ago Up 28 minutes gallant_volhard
[root@docker ~]#
[root@docker ~]# docker rm c271ced31481
c271ced31481
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d89bec2b9c71 mysql:latest "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 3306/tcp, 33060/tcp laughing_hypatia
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 28 minutes ago Up 28 minutes gallant_volhard
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# docker rm -f d89
d89
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 29 minutes ago Up 29 minutes gallant_volhard
# 容器网卡
# -----------docker0:172.17.0.1 ------------------
# | |
# | |
# veth1f4bbf3 veth1f4bbf4
# 172.17.0.2 172.17.0.3
[root@docker ~]# docker run -it -d centos:7 /bin/bash
05fb15d4365f4b42cd41fa25df5a7fb5ab3e4cf39de6170d60b1f1dd8d1966cb
# 两个容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05fb15d4365f centos:7 "/bin/bash" 4 minutes ago Up 4 minutes laughing_ganguly
07067ffcd9d5 ubuntu:20.04 "/bin/bash" 41 minutes ago Up 41 minutes gallant_volhard
# 查看两个容器的网卡
[root@docker ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:f1ff:fee3:a588 prefixlen 64 scopeid 0x20<link>
ether 02:42:f1:e3:a5:88 txqueuelen 0 (Ethernet)
veth1f4bbf3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::44eb:a3ff:fe24:41af prefixlen 64 scopeid 0x20<link>
ether 46:eb:a3:24:41:af txqueuelen 0 (Ethernet)
vethcf0b6ea: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::9085:27ff:fe69:5e39 prefixlen 64 scopeid 0x20<link>
ether 92:85:27:69:5e:39 txqueuelen 0 (Ethernet)
[root@docker ~]# docker exec -it 05 /bin/bash
# centos7 替换源 容器内部
[root@05fb15d4365f /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@05fb15d4365f /]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@05fb15d4365f /]# yum -y install net-tools # ifconfig
[root@d5f22ddf98ef /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
# 宿主机使用 inspect 查看
[root@docker ~]# docker inspect d5f22ddf98ef
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:11:00:03",
"NetworkID": "c44cee693844c821171759da4bd7a1e8cdbc8072c14a2e044ccd2a2c83ba0b9d",
"EndpointID": "1e9da94dc6905376cc5bb83281c7bb0a4e44a4d50f9ff8d4c3ee3327b5288a51",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
}
}
# ping 另外一个容器
[root@d5f22ddf98ef /]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.140 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.139 ms
^C
--- 172.17.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1056ms
rtt min/avg/max/mdev = 0.139/0.139/0.140/0.011 ms
标签:ago,演示,14,minutes,36,15,docker,root,综合
From: https://www.cnblogs.com/ckh2023/p/18070139