Docker入门

Docker

Docker概述

Docker为什么会出现?

问题一:

发布一个项目(jar+Redis+MySql+ jkd+ES)项目不能带上环境安装打包

传统:开发jar,运维部署

现在:开发打包部署上线,一套流程做完

解决方法:

Docker模式:

java --- jar(环境)--- 打包项目带上环境(镜像) --- (Docker仓库:商店)--- 下载发布的镜像 --- 直接运行即可

问题二:

多个应用交叉(端口占用)

解决方法:

Docker通过隔离机制,打包装箱,每个箱子都是隔离的

VM和Docker的对比

技术隔离措施性能备注
VM开启多个虚拟机内存占用几个G,启动时间几分钟VM会搭建Guest OS
Docker本身就是隔离的小巧,内存占用最小几MB,秒级启动Docker利用宿主机的内核,更少的抽象层

Docker的作用

  1. 应用更快速交付和部署
  2. 更便捷的升级和扩缩容
  3. 更简单的系统运维
  4. 更高效的计算资源利用

Docker安装

镜像(image)

镜像就好比模板,一个模板类,通过镜像可以创建多个容器

容器(container)

Docker利用容器技术,独立运行一个或一组应用,通过镜像来创建

启动,停止,删除

仓库(repository)

仓库就是存放镜像的地方

仓库分为公有仓库和私有仓库

#拉取docker helloworld镜像
# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

#运行docker hello-world
# docker run hello-world
This message shows that your installation appears to be working correctly.
.....

#查看docker镜像
# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   3 months ago   13.3kB
alpine/git    latest    a939554ad0d0   4 months ago   25.1MB
zookeeper     3.5.8     222518b8670a   4 months ago   261MB

Docker常用命令

镜像命令

docker images #查看所有镜像,-a全部信息 -q只显示id
docker search mysql #搜索docker hub的所有镜像
docker pull mysql #下载镜像
docker rmi -f	容器id #删除置顶容器
docker rmi -f $(docker images -aq)	#删除所有镜像

容器命令

docker pull centos #拉取镜像

#容器运行
docker run [可选参数] image
#参数说明
--name="Name"		容器名字
-d							后台方式运行
-it							使用交互运行,进入容器查看内容
-p							指定容器端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
-P							随机指定端口

docker run -it centos /bin/bash

#退出并停止容器
exit

#列出所有的容器
docker ps	#-a列出当前正在运行的容器+带出历史运行过的容器;-n显示最近创建的容器;-q值显示容器编号


#删除容器
docker rm 容器id

#删除所有容器
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 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

# 查看日志
docker log -tf --tail 10 容器id

# 查看容器元数据
docker inspect 容器id

# 查看容器进程信息
docker top 容器id
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2949                2922                0                   07:14               
root                3047                2949                0                   07:15             

# 进入正在运行的容器
docker exec -it 容器id
# 例子
docker exec -it 0da2de9664ea /bin/bash

docker attach 容器id
#例子
docker attach 0da2de9664ea


# 拷贝容器文件到主机文件
docker cp 容器id:容器内路径 目的主机路径

命令汇总

image-20210704153008657

Docker练习

部署Nginx

# 拉取nginx镜像
docker pull nginx
# 运行nginx容器,本机8080映射到容器80
docker run -d -p 8080:80 --name nginx01 nginx
# 测试
curl 127.0.0.1:8080
# 结果
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

部署Tomcat

# 拉取tomcat镜像
docker pull tomcat
# 运行容器
docker run -d -p 8081:8080 --name tomcat01 tomcat
# 进入tomecat容器中
docker exec -it tomcat01 /bin/bash
cp -r webapps.dist/* webapps
# 测试
curl 127.0.0.1:8081

部署可视化工具

docker run -d -p 9000:9000 --name portainer --restart=always  -v /var/run/docker.sock:/var/run/docker.sock -v  portainer_data:/data portainer/portainer 

image-20210704162840644

Docker镜像

Docker镜像加载原理,通俗易懂

Docker镜像加载原理

在这里插入图片描述

  • bootfs(boot file system) 主要包含bootloader和kernel, bpotloader 主要是引导加载kernel,当我们加载镜像的时候,会通过bootloader加载kernal,Docker镜像最底层是bootfs,当boot加载完成后整个kernal内核都在内存中了,bootfs也就可以卸载,值得注意的是,bootfs是被所有镜像共用的,许多镜像images都是在base image(rootfs)基础上叠加的
  • rootfs (root file system),在bootfs之 上.包含的就是典型Linux系统中的/dev, /proc, /bin, /etc等标准目录和文件。rootfs就是 各种不同的操作系统发行版,比如Ubuntu, Centos等等 。

分层理解

在这里插入图片描述

真正干活的是docker上的容器,不同容器都有一个共同的镜像,这个镜像有不同层级,每个层级都有自己的内容,可以共用,docker根据不同的需求来叠加镜像,从而对外暴露一个整体的镜像,每个容器都是精简版的linux系统,bootfs相当于鲸鱼的背,各容器内核kernal共用

Commit镜像

docker commit -m=提交信息 -a=作者 容器id 目标镜像铭:[TAG]

# 例子
docker commit -m='add webapp' -a='ch' 484c0ce002ba tomcat02:1.0
# 效果
(base) chennianzuisuideMacBook-Air:~ chennianzuisui$ docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
tomcat02              1.0       0296b33fdceb   13 seconds ago   672MB
tomcat                latest    36ef696ea43d   37 hours ago     667MB
nginx                 latest    4f380adfc10f   11 days ago      133MB
portainer/portainer   latest    580c0e4e98b0   3 months ago     79.1MB
hello-world           latest    d1165f221234   4 months ago     13.3kB
alpine/git            latest    a939554ad0d0   4 months ago     25.1MB
zookeeper             3.5.8     222518b8670a   4 months ago     261MB
centos                latest    300e315adb2f   6 months ago     209MB

容器数据卷

使用数据卷

容器之间有一个数据共享的技术,Docker容器中产生的数据,同步到本地

这就是卷技术,即目录的挂载,将我们的容器目录挂载在本机的目录上

卷技术:容器的持久化和同步操作,容器间也可以数据共享

# docker run -it -v 主机目录:容器内目录
# 注意mac下的共享目录需要在dokcer desktop中修改
docker run -it -v /Users/chennianzuisui/ceshi:/home centos /bin/bash
# 查看挂载是否成功,Mounts/Source,Destination
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ docker inspect 23648730c6fe
[
    {
        "Id": "23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469",
        "Created": "2021-07-06T04:43:06.3536085Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 14249,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-06T04:43:07.4777907Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469/hostname",
        "HostsPath": "/var/lib/docker/containers/23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469/hosts",
        "LogPath": "/var/lib/docker/containers/23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469/23648730c6fe45e013da3e82d98e371e78d140fff31a75b4eceea6737adf3469-json.log",
        "Name": "/boring_joliot",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/chennianzuisui/ceshi:/home"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "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",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "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"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/affa313e5e6f9d1903c6e2f1fef5b84b071c346a2b99bbad8981043af0fbc59b-init/diff:/var/lib/docker/overlay2/7e28025e45d3cb2d3ccd44b84416550d52175a599fb76c4843c878843a80c15b/diff",
                "MergedDir": "/var/lib/docker/overlay2/affa313e5e6f9d1903c6e2f1fef5b84b071c346a2b99bbad8981043af0fbc59b/merged",
                "UpperDir": "/var/lib/docker/overlay2/affa313e5e6f9d1903c6e2f1fef5b84b071c346a2b99bbad8981043af0fbc59b/diff",
                "WorkDir": "/var/lib/docker/overlay2/affa313e5e6f9d1903c6e2f1fef5b84b071c346a2b99bbad8981043af0fbc59b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/chennianzuisui/ceshi",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "23648730c6fe",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "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": "f08d44ef786f0f044872d0e02aedfe4105c058927da39de974d10c19f0a1e298",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/f08d44ef786f",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "3d88e3b44cfd67a35073c86ab1feaf03824d4ec0c45a66e89be2345f95c197ce",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.5",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:05",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "4e43df4d3e2df430fd0ccb2faf00eb38df1359ab8a852a4d92299518b308798c",
                    "EndpointID": "3d88e3b44cfd67a35073c86ab1feaf03824d4ec0c45a66e89be2345f95c197ce",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]

# centos中操作,mac同步
## centos中操作
cd ./home
touch test.java


## mac中操作
cd /ceshi
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ ls
test.java


# mac操作,centos同步
## mac中操作
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ ls
test.java
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ rm -rf test.java 
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ ls
(base) chennianzuisuideMacBook-Air:ceshi chennianzuisui$ 

## centos中操作
[root@23648730c6fe home]# ls
[root@23648730c6fe home]# 

安装MySQL

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×