复习:
1、docker介绍
容器,解决异构环境下的应用部署问题。
2、镜像
> 启动容器的模板
- docker search [镜像名称]
- -f : 筛选
- docker search -f stars=500 nginx
- docker pull [镜像名称]
- docker pull nginx
- docker login [仓库连接]
- -u|--username : 指定用户名称
- -p | --password : 指定密码
- docker login --username=yh01j**** registry.cn-hangzhou.aliyuncs.com
- docker tag [镜像ID] [镜像的TAG]
- 仓库URL
- 命名空间
- 镜像名称
- 版本号
- docker tag 08502081bff6 registry.cn-hangzhou.aliyuncs.com/alvi/redis:v5
-
- docker push [镜像TAG]
docker push registry.cn-hangzhou.aliyuncs.com/alvi/redis:v5
- 删除和清空镜像
- docker rmi [镜像ID|镜像名称]
- docker rmi redis
- docker image prune
- -a : 清空所有的
- 保存镜像
- 保存容器为镜像(docker commit)
- 保存镜像为压缩包(docker save)
- docker load
- 保存容器为镜像压缩包(docker export)
- docker import
- docker history 查看镜像构建历史
- 查看镜像详细信息 docker inspect
- -f "{{.xxx.xxx}}"
docker export 的应用场景:主要用来制作基础镜像,比如我们从一个 ubuntu 镜像启动一个容器,然后安
装一些软件和进行一些设置后,使用 docker export 保存为一个基础镜像。然后,把这个镜像分发给其他人使用,
比如作为基础的开发环境。
docker save 的应用场景:如果我们的应用是使用 docker-compose.yml 编排的多个镜像组合,但我们要部
署的客户服务器并不能连外网。这时就可以使用 docker save 将用到的镜像打个包,然后拷贝到客户服务器上使
用 docker load 载入。
一、docker容器
容器就是对外提供服务的一个实例。
容器启动的必要条件:容器内至少有一个进程运行在前台 如果容器没有进程在运行前台,那么容器生命周期机会消失
获取镜像
[root@docker ~]
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
创建centos容器
[root@docker ~]
3a2b1ba930af702df91a2c6cff15e784c8a88abe3aeaf346f1315f55a064e491
查看容器列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55a34d5d9adc nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp thirsty_swartz
d98d9e45c781 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:40080->80/tcp, :::40080->80/tcp boring_khayyam
查看所有容器列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a2b1ba930af centos:7 "/bin/bash" 10 seconds ago Exited (0) 8 seconds ago
因为centos的执行是一个bin/bash条件,执行完毕就没有了
这时候需要docker ps才能看见centos的进程,但是此进程已经生命周期凉凉了
[root@docker yum.repos.d]
[root@docker yum.repos.d]
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 54069/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp6 0 0 :::80 :::* LISTEN 54069/nginx: master
tcp6 0 0 :::22 :::* LISTEN 1387/sshd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1053/rpcbind
udp 0 0 0.0.0.0:801 0.0.0.0:* 1053/rpcbind
udp 0 0 127.0.0.1:323 0.0.0.0:* 1055/chronyd
[root@docker yum.repos.d]
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@docker yum.repos.d]
[root@docker yum.repos.d]
示例: docker run -it [镜像名称] [cmd]====前台运行
[root@docker ~]
[root@docker ~]
[root@ee3be4769513 /]
[root@ee3be4769513 yum.repos.d]
[root@ee3be4769513 yum.repos.d]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1775 100 1775 0 0 6058 0 --:--:-- --:--:-- --:--:-- 6058
[root@ee3be4769513 yum.repos.d]
total 4
-rw-r--r-- 1 root root 1775 Jul 14 19:24 CentOS-Base.repo
[root@ee3be4769513 yum.repos.d]
[root@ee3be4769513 yum.repos.d]
[root@ee3be4769513 yum.repos.d]
[root@ee3be4769513 yum.repos.d]
nginx version: nginx/1.20.1
[root@ee3be4769513 yum.repos.d]
[root@ee3be4769513 yum.repos.d]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 19:22 pts/0 00:00:00 /bin/bash
root 89 1 0 19:28 ? 00:00:00 nginx: master process nginx
nginx 90 89 0 19:28 ? 00:00:00 nginx: worker process
root 91 1 0 19:30 pts/0 00:00:00 ps -ef
[root@ee3be4769513 yum.repos.d]
exit
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1、创建容器
docker run [参数] [镜像名称] [运行容器的启动命令]
-d : 以守护进程的方式运行一个容器(相当于后台运行)
docker run -d [镜像名称] [cmd]
例:docker run -d nginx
--name : 指定容器的名称
docker run -d --name [容器名称] [镜像的名称] [cmd]
例:docker run -d --name boss_nginx nginx
-p : 指定端口映射
docker run -d -p 宿主主机端口:容器内端口 [镜像名称] [cmd]
例:docker run -d -p 43080:80 nginx
-P :随机端口映射
docker run -d -P [镜像名称] [cmd]
例:docker run -d -P nginx
-i : 打开标准输出
-t : 创建一个伪终端
docker run -it [镜像名称] [cmd]====前台运行
-v : 挂载目录到容器中
docker run -v 宿主主机目录:容器内目录 [镜像名称] [cmd]
--rm : 容器生命周期结束时立即删除
docker run --rm [镜像名称] [cmd]
-e : 在容器中创建一个环境变量
docker run -e NAME=Centos -d [镜像名称] [cmd]
--link : 连接上一个容器,实现网络互通
docker run --link 被连接的容器的名称:连接别名 [镜像名称] [cmd]
-h : 设置容器主机名
docker run -h "主机名" [镜像名称] [cmd]
案例:
[root@docker ~]
[root@6c78e33cc606 /]
total 12
-rw-r--r-- 1 root root 12114 Nov 13 2020 anaconda-post.log
lrwxrwxrwx 1 root root 7 Nov 13 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jul 14 17:49 dev
drwxr-xr-x 1 root root 66 Jul 14 17:49 etc
drwxr-xr-x 2 root root 6 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 Nov 13 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 13 2020 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 134 root root 0 Jul 14 17:49 proc
dr-xr-x--- 2 root root 114 Nov 13 2020 root
drwxr-xr-x 11 root root 148 Nov 13 2020 run
lrwxrwxrwx 1 root root 8 Nov 13 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Jul 14 17:49 sys
drwxrwxrwt 7 root root 132 Nov 13 2020 tmp
drwxr-xr-x 13 root root 155 Nov 13 2020 usr
drwxr-xr-x 18 root root 238 Nov 13 2020 var
[root@bf0b90672aed /]
exit
Ctrl+p+q快捷键:容器不停止退出
1、检查本地是否用指定镜像,如果没有则去对应的仓库下载镜像
2、启动容器,如果指定了命令则使用指定的命令,如果没有则使用默认的命令
3、返回容器ID
参数示例:
[root@docker ~]
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eca17c89df56 centos:7 "/bin/bash" 27 seconds ago Up 27 seconds great_galileo
[root@docker ~]
[root@f5c4af20aa25 /]
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@f5c4af20aa25 /]
[root@f5c4af20aa25 tmp]
[root@f5c4af20aa25 tmp]
[root@f5c4af20aa25 test]
[root@f5c4af20aa25 test]
回到宿主机查看
[root@docker tmp]
总用量 0
drwx------ 3 root root 17 4月 13 19:30 systemd-private-d3aaaad7a5524a6889e6c9d6a996458c-chronyd.service-KfuDfT
drwxr-xr-x 2 root root 137 7月 15 03:46 test
[root@docker tmp]
[root@docker test]
总用量 0
-rw-r--r-- 1 root root 0 7月 15 03:46 10.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 1.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 2.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 3.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 4.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 5.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 6.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 7.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 8.txt
-rw-r--r-- 1 root root 0 7月 15 03:46 9.txt
[root@docker test]
b04ba0b4213f694d326616c837bf459ce9c5cb1e7203ed672b2a0be9947d3d71
[root@docker test]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b04ba0b4213f nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 80/tcp adoring_pare
[root@docker test]
adoring_pare
[root@docker test]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker test]
[root@docker test]
[root@docker test]
Unable to find image 'centos:latest' locally
^[[A^C
[root@docker test]
[root@12cb3511a427 /]
HOSTNAME=12cb3511a427
TERM=xterm
NAME=CentOS
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SHLVL=1
HOME=/root
=/usr/bin/printenv
[root@12cb3511a427 /]
[root@docker ~]
4314afb5dcab46dccfa90bd2bc7098398dc8467bb31f79bd235fe8b2348aaa7f
docker run --link 被连接的容器的名称:连接别名 [镜像名称] [cmd]
[root@docker test]
[root@docker ~]
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
[root@3dade8a95476 /]
或者
[root@docker test]
c0d1f2b4a17a nginx "/docker-entrypoint.…" 24 minutes ago Up 9 minutes 80/tcp nginx
[root@3dade8a95476 /]
docker run -h "主机名" [镜像名称] [cmd]
[root@docker ~]
[root@xxx /]
[root@xxx /]
[root@xxx /]
补充:容器原理图
2、查看本机的容器列表
docker ps [参数]
docker ps
-a : 查看系统中所有的容器。
-q : 仅显示容器的ID
-n=: 显示最近创建的容器
3、停止和启动一个容器
docker stop [容器的ID|名称]
docker start [容器的ID|名称]
docker restart [容器的ID|名称]
docker kill [容器的ID|名称]
获取镜像
[root@docker ~]
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
创建容器
[root@docker ~]
3a2b1ba930af702df91a2c6cff15e784c8a88abe3aeaf346f1315f55a06
查看容器列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55a34d5d9adc nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp thirsty_swartz
d98d9e45c781 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:40080->80/tcp, :::40080->80/tcp boring_khayyam
查看所有容器列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cab90565a0e centos:7 "bash" 11 minutes ago Exited (127) 6 minutes ago angry_williams
6c78e33cc606 centos:7 "bash" 12 minutes ago Exited (0) 11 minutes ago upbeat_lewin
3a2b1ba930af centos:7 "/bin/bash" 18 minutes ago Exited (0) 18 minutes ago compassionate_ride
55a34d5d9adc nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp thirsty_swartz
d98d9e45c781 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:40080->80/tcp, :::40080->80/tcp boring_khayyam
3da76fc84d3f nginx "/docker-entrypoint.…" About an hour ago Exited (0) About an hour ago zen_hermann
启动容器
[root@docker ~]
9cab90565a0e
这时候就有了centos的列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cab90565a0e centos:7 "bash" 12 minutes ago Up 4 seconds angry_williams
55a34d5d9adc nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp thirsty_swartz
d98d9e45c781 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:40080->80/tcp, :::40080->80/tcp boring_khayyam
停止容器
[root@docker ~]
9cab90565a0e
查看容器列表
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55a34d5d9adc nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp, :::49153->80/tcp thirsty_swartz
d98d9e45c781 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:40080->80/tcp, :::40080->80/tcp boring_khayyam
4,删除容器
docker rm [容器名称|ID]
-f : 强制删除
docker rm -f [容器名称|ID]
docker rm $(docker ps -a -q)
docker rm -f $(docker ps -a -q)
[root@\ docker~]
sleepy_elgamal
[root@\ docker~]
sleepy_elgamal
5,查看容器内容
docker inspect [容器名称|ID]
docker inspect -f '{{.State.Running}}' nginx
[root@\ docker~]
true
6、复制命令
[root@\ docker~]
14c63482dc1d nginx "/docker-entrypoint.…" 45 minutes ago Up 45 minutes 80/tcp nginx_store
1、复制到容器内
docker cp [宿主主机文件路径] 容器ID:容器内路径
[root@\ docker~]
[root@\ docker~]
root@14c63482dc1d:/
all.tarr
示例
[root@docker ~]
[root@docker ~]
[root@1e6655a2461b /]
或者
[root@1e6655a2461b /]
2、复制到容器外
docker cp 容器ID:容器内路径 [宿主主机文件路径]
[root@\ docker~]
root@14c63482dc1d:/
root@14c63482dc1d:/
[root@\ docker~]
[root@\ docker~]
-rw-r--r-- 1 root root 0 2021-03-22 11:05 bb.txt
示例
[root@docker ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f46537766a8f nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp nginx
94993fd7d030 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp busy_liskov
[root@docker ~]
[root@docker ~]
总用量 486416
-rw-r--r-- 1 root root 5873664 7月 14 20:14 alpine.tar
-rw-------. 1 root root 1701 3月 29 15:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 1463808 7月 14 20:21 busybox.tar
-rw-r--r-- 1 root root 137441280 7月 14 20:17 nginx444.tar
-rw-r--r-- 1 root root 648 7月 6 23:11 nginx.conf
7、进入容器
1、exec : 在容器外向容器内执行一个命令(官方推荐)
docker exec [参数] [容器的名称|ID] [cmd]
示例1:
[root@\ docker~]
all.tarr
bb.txt
示例2:
[root@docker ~]
root@f46537766a8f:/
root@f46537766a8f:/usr/share/nginx/html
50x.html busybox.tar index.html
示例2:
终端1:
[root@\ docker~]
[root@aed66b3efd41 /]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:09 pts/0 00:00:00 bash
root 15 0 0 03:10 pts/1 00:00:00 bash
root 29 1 0 03:10 pts/0 00:00:00 ps -ef
终端2 开启,关闭:
[root@\ docker~]
[root@aed66b3efd41 /]
再次查看终端1:
[root@aed66b3efd41 /]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:09 pts/0 00:00:00 bash
root 30 1 0 03:10 pts/0 00:00:00 ps -ef
2、attach : 在早期docker提供的进入容器的命令(缺点:当其结束时,容器也跟着结束了)
docker attach [容器ID|名称]
3、nsenter : 建立一个管道连接上容器主ID
nsenter --target $( docker inspect -f {{.State.Pid}} 30d369d70bcd) --mount --uts --ipc --net --pid
4、ssh : 通过ssh连接
|