目录
- 一、认识docer
- 二、在阿里云服务器上配置Docker
- 三、底层原理
- 4、常用命令
- (1)Docker中常见镜像命令
- (2)Docker中常见容器命令
- (3)日志查看命令
- (4)进入容器的命令与拷贝命令
- 小结
- 四、部署容器
- 五、dockerfile命令
- DockerFile是什么
- 常见命令
- 自制一个centos镜像
一、认识docer
传统虚拟机方式
拥有完整内核和相应的库才能安装需要的app,模拟了一台完整的电脑
缺点:
- 资源占用多
- 冗余步骤多
- 启动慢
- 资源公用,某个设置出问题可能导致多个应用出问题
docer容器化技术
优点:
- 容器内的应用直接运行在宿主机的内核,容器没有自己的内核
- 每个容器内有一个属于自己的文件系统互不影响,容器之间相互隔离互不影响
- 打包镜像发布测试,一键运行
- 更便捷的升级和扩容
- 容器化之后,开发和测试环境高度一致
- 更高效的计算资源利用,Docer是内核级别的虚拟化,可以在一个物理机上运行多个容器实例,服务器性能可以压榨到极致
镜像(image)容器(container)仓库(repository)
流程:客户端启动docer里的一个容器,容器要从服务器上下载
二、在阿里云服务器上配置Docker
需要购买一个阿里云服务器,活动99一年的就够了,需要分配外网ipv4地址
步骤(参考docker官网文档)
1、卸载旧的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate\
docker-logrotate \
docker-engine
2、添加仓库
sudo yum install -y yum-utils
3、设置镜像仓库为阿里云国内源
sudo yum-config-manager --add-repo \
http://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo
4、更新索引
yum makecache fast
5、安装docker
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
6、启动docker
sudo systemctl start docker
7、判断是否启动成功
docker version
sudo docker run hello-world
hello-word程序的执行流程
8、配置阿里云镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://rpu0hzw5.mirror.aliyuncs.com"]
}
EOFEO
sudo systemctl daemon-reload
sudo systemctl restart docker
三、底层原理
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问,DockerServer接受到Docker-Client的指令,就会执行这个命令
Docker为什么比VM快
- Docker有着比虚拟机更少的抽象层
- Docker利用的是宿主机的内核,vm需要的是Guest OS(Guest Operating System 指在虚拟化环境中运行的操作系统)
所以,新建一个容器的时候,docker不需要和虚拟机一样重新加载一个操作系统的内核,直接利用宿主机的操作系统内核
4、常用命令
(1)Docker中常见镜像命令
查看帮助文档 docker [命令] --help
[root@aliyun_docker ~]# docker top --help
Usage: docker top CONTAINER [ps OPTIONS]
Display the running processes of a container
Aliases:
docker container top, docker top
搜索镜像
docker search
[root@iZ2vc3j8qdus28heb8myw2Z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL
mysql MySQL is a widely used, open-source relation… 15008 [OK]
mariadb MariaDB Server is a high performing open sou… 5722 [OK]
下载镜像
docker pull
[root@iZ2vc3j8qdus28heb8myw2Z ~]# docker pull mysql
Using default tag: latest#如果不写tag默认下载最新版本
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@iZ2vc3j8qdus28heb8myw2Z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
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 images
[root@iZ2vc3j8qdus28heb8myw2Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 11 months ago 13.3kB
mysql 5.7 c20987f18b13 2 years ago 448MB
mysql latest 3218b38490ce 2 years ago 516MB
通过IMAGE ID 删除 image,加空格可以删除多个,
dock rmi -f $(docker images -aq)组合命令删除全部容器
[root@iZ2vc3j8qdus28heb8myw2Z ~]# docker rmi -f 3218b38490ce
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
[root@iZ2vc3j8qdus28heb8myw2Z ~]#
(2)Docker中常见容器命令
有了镜像才可以创建容器,
新建容器并启动
docker run [可选参数] image
#可选参数说明
–name = “Name” 容器名字,用来区分容器 tomcat01 tomcat02,用来区分容器
-d 后台方式运行(如果要后台运行必须要有一个前台进程,容器启动后发现自己没有提供服务就会立刻停止)
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -P 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容落端口 (常用)
-p 容器端口
容器端口
-p 随机指定端口
[root@aliyun_docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 11 months ago 13.3kB
mysql 5.7 c20987f18b13 2 years ago 448MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@aliyun_docker ~]# docker run -it centos /bin/bash
[root@a790c5209310 /]#
可以发现进入容器后主机名称变了
退出(按三个键CTRL+p+q,退出不关闭)
[root@aliyun_docker ~]# docker run -it centos /bin/bash
[root@b8630535a0c9 /]# [root@aliyun_docker ~]#
[root@aliyun_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8630535a0c9 centos "/bin/bash" 43 seconds ago Up 43 seconds keen_kepler
[root@b8630535a0c9 /]# exit
查看当前正在运行/运行过的容器(加 -a)
[root@aliyun_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@aliyun_docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a790c5209310 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago funny_brahmagupta
2321a478186b hello-world "/hello" 4 hours ago Exited (0) 4 hours ago eager_pascal
[root@aliyun_docker ~]#
只显示编号 -q
[root@aliyun_docker ~]# docker ps -aq
a790c5209310
2321a478186b
删除容器
docker rm 容器id #删除指定容器正在运行的不能删除(加-f)
docker rm -f $(docker ps -aq) #删除所有
docker ps -a -q|xargs docker rm #通过管道删除
[root@aliyun_docker ~]# docker rm b8630535a0c9
Error response from daemon: cannot remove container "/keen_kepler": container is running: stop the container before removing or force remove
启动和停止
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器(实例仍然存在)
(3)日志查看命令
写一段shell脚本
[root@aliyun_docker ~]# docker run -d centos /bin/bash -c "while true;do echo xiakucao;sleep 1;done"
22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf
[root@aliyun_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22249409fb3c centos "/bin/bash -c 'while…" 5 seconds ago Up 3 seconds jolly_hodgkin
b8630535a0c9 centos "/bin/bash" 29 minutes ago Up 29 minutes keen_kepler
[root@aliyun_docker ~]#
显示指定行数的日志
[root@aliyun_docker ~]# docker logs -f -t --tail 10 22249409fb3c
2024-04-18T14:15:44.419231635Z xiakucao
2024-04-18T14:15:45.421276341Z xiakucao
2024-04-18T14:15:46.423269443Z xiakucao
2024-04-18T14:15:47.425443716Z xiakucao
2024-04-18T14:15:48.427495542Z xiakucao
2024-04-18T14:15:49.429472406Z xiakucao
2024-04-18T14:15:50.431600097Z xiakucao
2024-04-18T14:15:51.434124367Z xiakucao
2024-04-18T14:15:52.436540492Z xiakucao
2024-04-18T14:15:53.438632432Z xiakucao
2024-04-18T14:15:54.441042791Z xiakucao
2024-04-18T14:15:55.443170520Z xiakucao
2024-04-18T14:15:56.445296720Z xiakucao
2024-04-18T14:15:57.447415752Z xiakucao
2024-04-18T14:15:58.449874664Z xiakucao
2024-04-18T14:15:59.451981852Z xiakucao
^Z
[3]+ Stopped docker logs -f -t --tail 10 22249409fb3c
[root@aliyun_docker ~]#
查看容器中进程信息
[root@aliyun_docker ~]# docker top 22249409fb3c
UID PID PPID C STIME TTY TIME CMD
root 1769 1748 0 22:14 ? 00:00:00 /bin/bash -c while true;do echo xiakucao;sleep 1;done
root 2122 1769 0 22:19 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
docker inspect
可以看到IMAGE ID截取的是部分ID
[root@aliyun_docker ~]# docker inspect 22249409fb3c
[
{
"Id": "22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf",
"Created": "2024-04-18T14:14:13.991971662Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo xiakucao;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1769,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-04-18T14:14:14.22346875Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf/hostname",
"HostsPath": "/var/lib/docker/containers/22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf/hosts",
"LogPath": "/var/lib/docker/containers/22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf/22249409fb3c95efe4ad51637a6340abb5b5c212183531a4ee261444aef130cf-json.log",
"Name": "/jolly_hodgkin",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
31,
102
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": [],
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware",
"/sys/devices/virtual/powercap"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/46c4ebdbc616cb8d052b73a0b47a03dbef18a7f1d0ad17346a982c658d5660a0-init/diff:/var/lib/docker/overlay2/cfef35f2fd2430f54798e47b3ce3056bbd96fa29637ba2fa8144daa1c925a355/diff",
"MergedDir": "/var/lib/docker/overlay2/46c4ebdbc616cb8d052b73a0b47a03dbef18a7f1d0ad17346a982c658d5660a0/merged",
"UpperDir": "/var/lib/docker/overlay2/46c4ebdbc616cb8d052b73a0b47a03dbef18a7f1d0ad17346a982c658d5660a0/diff",
"WorkDir": "/var/lib/docker/overlay2/46c4ebdbc616cb8d052b73a0b47a03dbef18a7f1d0ad17346a982c658d5660a0/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "22249409fb3c",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true;do echo xiakucao;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b02c97f64a8992dc0f322a5acc0d2871115ff1654e680a06bdb86591659fef10",
"SandboxKey": "/var/run/docker/netns/b02c97f64a89",
"Ports": {},
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "daa0dad6de8bfdaab015d01972c6e0e7ff1518b23d839a9407c81b15c14d3564",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:11:00:03",
"NetworkID": "f9a1764ef63ab36cfd232cf326edc32a337b8722c30a9964659b044b0fecf5c4",
"EndpointID": "daa0dad6de8bfdaab015d01972c6e0e7ff1518b23d839a9407c81b15c14d3564",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
}
}
}
}
]
(4)进入容器的命令与拷贝命令
exec进入容器用bash命令,(run是创建并进入,exec仅进入容器)
exec进入容器后会开启一个新的终端,可以在里面进行操作
[root@aliyun_docker ~]# docker exec -it 22249409fb3c /bin/bash
[root@22249409fb3c /]#
attach命令 进入正在执行的终端,不会启动新的进程
[root@aliyun_docker ~]# docker attach 22249409fb3c
xiakucao
xiakucao
xiakucao
xiakucao
从容器内拷贝文件到主机上
只要容器还在,容器内的文件就不会消失
[root@aliyun_docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0eb80da8ce30 centos "/bin/bash" About a minute ago Up About a minute agitated_bhaskara
[root@aliyun_docker ~]# docker attach 0eb80da8ce30
[root@0eb80da8ce30 home]# cd /home
[root@0eb80da8ce30 home]# ls
[root@0eb80da8ce30 home]# touch test.java
[root@0eb80da8ce30 home]# exit
exit
[root@aliyun_docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0eb80da8ce30 centos "/bin/bash" 2 minutes ago Exited (0) 6 seconds ago agitated_bhaskara
[root@aliyun_docker ~]# docer cp 0eb80da8ce30:/home/test.java /home
-bash: docer: command not found
[root@aliyun_docker ~]# docker cp 0eb80da8ce30:/home/test.java /home
Successfully copied 1.54kB to /home
[root@aliyun_docker ~]#
小结
四、部署容器
部署nginx
-d后台运行
-name 给容器命名
-p 宿主机端口:容器内部端口
[root@aliyun_docker ~]# docker run -d --name nginx01 -p 3389:80 nginx
b42c715ebae889c6d180b18305f523d2abefe33f11685b3e7354ea1fd776b86e
端口原理图
测试端口
[root@aliyun_docker ~]# curl localhost:3389
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
五、dockerfile命令
DockerFile是什么
DockerFile:构建文件,定义了一切的步骤,源代码
DockerImages:通过DockerFile构建生成的镜像,最终发布和运行的产品
Docker容器:容器就是镜像运行起来提供服务器
常见命令
tips:
- 用ADD命令添加的文件会自动解压
自制一个centos镜像
scratch是一个特殊的空白镜像,它是Docker官方提供的最小的镜像,不包含任何操作系统或文件系统。它可以作为基础镜像,用于构建定制化的镜像。Docker Hub中大部分镜像都是从这个基础镜像过来的 FROM scratch没然后配置需要的软件和配置来构建
创建的镜像版本尽量和宿主机匹配我的云服务器是centos7
FROM centos:7
MAINTAINER xiakucao<1844966859@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH #默认进入根目录
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "-------end--------"
CMD /bin/bash
#注意有个点
[root@aliyun_docker dockerfile]# vim mydockerfile-centos
[root@aliyun_docker dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.2 .
[+] Building 54.7s (8/8) FINISHED
查看镜像的构建过程
[root@aliyun_docker dockerfile]# docker history 7e0c8c0fb962
IMAGE CREATED CREATED BY SIZE COMMENT
7e0c8c0fb962 3 minutes ago CMD ["/bin/sh" "-c" "/bin/bash"] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago CMD ["/bin/sh" "-c" "echo \"-------end------… 0B buildkit.dockerfile.v0
<missing> 3 minutes ago CMD ["/bin/sh" "-c" "echo $MYPATH"] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 3 minutes ago RUN /bin/sh -c yum -y install net-tools # bu… 212MB buildkit.dockerfile.v0
<missing> 3 minutes ago RUN /bin/sh -c yum -y install vim # buildkit 301MB buildkit.dockerfile.v0
<missing> 4 minutes ago WORKDIR /usr/local 0B buildkit.dockerfile.v0
<missing> 4 minutes ago ENV MYPATH=/usr/local 0B buildkit.dockerfile.v0
<missing> 4 minutes ago MAINTAINER xiakucao<1844966859@qq.com> 0B buildkit.dockerfile.v0
<missing> 2 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 years ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4… 204MB
[root@aliyun_docker dockerfile]#