Docker 笔记
Docker 为什么比VM快
-
Docker 有着比虚拟机更少的抽象层。
-
Docker 利用的是宿主机的内核,VM需要Guest OS。
Docker 常用命令
帮助命令
docker version #显示docker版本信息
docker info #显示docker的系统信息
docker 命令 --help #帮助命令
镜像命令
-
docker images 查看所有本地主机上的镜像
root@goodzjh:/home/goodzjh# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_java latest 4dbf7d84362b 2 weeks ago 902MB
ubuntu_tomcat latest 33ca6412955c 3 weeks ago 2GB
redis latest 7614ae9453d1 6 months ago 113MB
mysql 5.7 c20987f18b13 6 months ago 448MB
tutum/influxdb latest c061e5808198 5 years ago 290MB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
# 可选项
-a #列出所有镜像
-q #只显示id
-
docker search 搜索镜像
#
-
docker pull 下载镜像
#
-
docker rmi 删除镜像
docker rmi -f $(docker images -aq) #删除所有镜像
docker rmi -f 镜像id #删除某个镜像
容器命令
-
新建容器并启动
docker run [可选参数] image #参数说明 --name="name" #容器名字 -d #后台运行 -it #使用交互方式运行 -p #指定容器端口 -p #主机端口:容器端口 -P #随机指定端口
-
列出正在运行的镜像
docer ps [可选参数] #参数说明 -a #列出当前正在运行的和运行过的容器 -n=? #列出最近创建的容器 -q #只显示容器的编号
-
退出容器
exit #直接退出并停止容器 Ctrl +p +q #退出容器并继续运行
-
删除容器
docker rm 容器id #删除指定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 centos # docker ps 发现centos 停止了 # 常见问题:docker 容器使用后台运行,就必须要有一个前台应用。docker 如果发现没用应用就会自己停止。
-
查看日志
docker logs #可选参数 -tf #显示日志 --tail number #显示日志条数
-
查看容器进程信息
root@goodzjh:/home/goodzjh# docker top 7a7613d6aa59 UID PID PPID C STIME TTY TIME CMD root 31471 31451 2 Jun13 pts/0 09:27:16 java -jar /usr/local/java_jar/intelligent_control_plug.jar
-
查看容器的元数据
root@goodzjh:/home/goodzjh# docker inspect 7a7613d6aa59 [ { "Id": "7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78", "Created": "2022-06-13T08:07:55.807428041Z", "Path": "java", "Args": [ "-jar", "/usr/local/java_jar/intelligent_control_plug.jar" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 31471, "ExitCode": 0, "Error": "", "StartedAt": "2022-06-13T08:07:56.170011941Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:4dbf7d84362b922319e7fe018dc3c05c44f61ffa916e12ecf9a35ec27b9e3d56", "ResolvConfPath": "/var/lib/docker/containers/7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78/resolv.conf", "HostnamePath": "/var/lib/docker/containers/7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78/hostname", "HostsPath": "/var/lib/docker/containers/7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78/hosts", "LogPath": "/var/lib/docker/containers/7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78/7a7613d6aa593cf8bab5a7861d80af32a2429b95281609e7134f7139d96b3c78-json.log", "Name": "/control_plug", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "docker-default", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "private", "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": null, "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/b93c5667928fa38bceff5d6da54b00c014702d3fb4612c62ab29cc05cbc2f9ca-init/diff:/var/lib/docker/overlay2/c23ae2e9fb11d3c475b4c2b2389bca4ed8d87b7a8f13fed74b9729fae9636a82/diff:/var/lib/docker/overlay2/9217d4908ebfcd160b6ecda1f564bc632b0b4ddce3997e3e2af6e52c95d40276/diff:/var/lib/docker/overlay2/47d8a52fe07e88b4b02b1b2c3910f21c0b699ab7f772d7568fe826c7fca49714/diff:/var/lib/docker/overlay2/a273c778e3937d45f89cb30bb3455a4f5fcd14c256e29005466f61d9727e260a/diff:/var/lib/docker/overlay2/b88599ea8fc898540197296b92ac1ed87ca90a220d61300319671f912fad8a83/diff:/var/lib/docker/overlay2/c36acbcb40815553a144190ad9193202e3ca281eb2f4555af66d6dae46665463/diff:/var/lib/docker/overlay2/ffeaf7a0a2fc3d83d00564b8c6c4d0358867817b0b417a4f60740b70a1924cc7/diff:/var/lib/docker/overlay2/e53d789f6297ff5edcfb17fc6c869c8525030c34f817419627d96ffc449978ab/diff:/var/lib/docker/overlay2/dd79b6551b81569a753a55fd179f3526b918af18676fe9a8da8c39f0abe1b6f3/diff", "MergedDir": "/var/lib/docker/overlay2/b93c5667928fa38bceff5d6da54b00c014702d3fb4612c62ab29cc05cbc2f9ca/merged", "UpperDir": "/var/lib/docker/overlay2/b93c5667928fa38bceff5d6da54b00c014702d3fb4612c62ab29cc05cbc2f9ca/diff", "WorkDir": "/var/lib/docker/overlay2/b93c5667928fa38bceff5d6da54b00c014702d3fb4612c62ab29cc05cbc2f9ca/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "7a7613d6aa59", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "8787/tcp": {} }, "Tty": true, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java/lib:/usr/local/java/jre/lib:/usr/local/java/bin", "JAVA_HOME=/usr/local/java" ], "Cmd": [ "java", "-jar", "/usr/local/java_jar/intelligent_control_plug.jar" ], "Image": "ubuntu_java", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "2b404a3a75a50990ff07400788d7632d392b2431124f0ab648a15596a8c120dc", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "8787/tcp": null }, "SandboxKey": "/var/run/docker/netns/2b404a3a75a5", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "d07f034f2e096a9456f283387c615e5ccbd2c5ca478c5b626c2725fa6f924556", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.6", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:06", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "04bc28f6172b8b907d36b4fce7206ca9b74e59efa2ee870f4c3a9be22ac1842e", "EndpointID": "d07f034f2e096a9456f283387c615e5ccbd2c5ca478c5b626c2725fa6f924556", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.6", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:06", "DriverOpts": null } } } } ]
-
进入到运行的容器中
docker exec -it 容器id /bin/bash # 进入容器后开启新终端 docker attach 容器id # 进入容器正在执行的终端
-
容器和主机文件拷贝
docker cp 容器id:文件路径 主机目录 #容器文件拷贝到主机 docker cp 主机文件路径 容器id:目的目录
容器数据卷
什么是容器数据卷
docker的理念回顾
将应用和环境打包成一个镜像
数据如果都保存在容器中,当我们删除容器的时候数据就会丢失。容器之间可以有一个数据共享技术,可以将容器中产生的数据同步到本地。卷技术可以把容器中的目录挂载到Linux
总结:容器的持久化和同步操作,容器间可以数据共享。
使用数据卷
方式一:直接使用命令挂载 -v
docker run -it -v 主机目录:容器目录
安装持久化mysql
- 拉取镜像:
docker pull mysql:5.7 - 运行容器,需要数据挂载,设置密码
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name=mysql3310 mysql:5.7
具名和匿名挂载
- 匿名挂载(不指定主机目录,只有容器目录)
-v 容器内路径
查看一下挂载卷docker run -d -P --name nginx01 -v /etc/nginx nginx
docker volume ls
- 具名挂载(不指定主机目录,但是给卷命名)
查看一下卷的详细信息docker run -d -P --name nginx02 -v JM-nginx:/etc/nginx nginx
root@goodzjh:/home/mysql# docker volume inspect JM-nginx
[
{
"CreatedAt": "2022-07-21T09:55:53+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/JM-nginx/_data",
"Name": "JM-nginx",
"Options": null,
"Scope": "local"
}
]
```
拓展:
通过 -v 容器内路径:ro rw 改变读写权限
ro 只读 rw 可读可写
初识dockerfile
Dockerfile 就是用来构建docker镜像的构建文件,通过这个脚本可以生成镜像。
dockerfile 内容
FROM ubuntu
VOLUME ["VOLUME1","VOLUME2"]
CMD echo "----end----"
CMD /bin/bash
~
- 使用docker build 生成镜像
docker build -f dockerfile -t goodzjh/ubuntu . - 启动一个容器docker01
docker run -itd --name docker01 goodzjh/ubuntu - 启动一个容器docker02 挂载docker01 的卷
docker run -itd --name docker02 --volume-from docker01 goodzjh/ubuntu - 修改docker02 卷中的内容,docker01中随之改变。
Dockerfile
dockerfile 介绍
dockerfile 是用来构建docker镜像的文件,是一个脚本文件
构建步骤:
- 编写一个dockerfile 文件
- docker build 构建一个镜像
- docker run 运行一个镜像
- docker push 发布一个镜像
dockerfile 构建过程
基础知识
- 每个保留关键字都是大写
- 从上往下执行
-
表示注释
- 每个指令都会创建提交一个新的镜像层
DockerFile: 构建文件,如何构建一个镜像
Dockermages: 通过DockerFile 构建生成的镜像,最终发布和运行的产品
Docker 容器:容器是运行起来的镜像提供服务。
DockerFile的指令
FROM #基础镜像
MAINTAINER #镜像是谁写的 姓名加邮箱
RUN #构建的时候需要运行的命令
ADD #文件添加
WORKDIR #镜像的工作目录
VOLUME #挂载的目录
EXPOST #保留端口配置
CMD #指定容器启动时候要运行的命令,只有最后一个会生效
ENTRYPOINT #指定容器启动时候要运行的命令
ONBUILD #当构建一个被继承的Dockerfile 这个命令就会被执行
COPY #将文件拷贝到镜像中
ENV #设置环境变量
创建一个自己的centos
FROM centos:7
MAINTAINER goodzjh<906628283>
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
创建一个tomcat 镜像
Dockerfile
FROM centos:7
MAINTAINER goodzjh<906628283>
ADD jdk1.8.0_121 /usr/local/jdk1.8.0_121
ADD tomcat /usr/local/tomcat
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/tomcat
ENV CATALINA_BASH /usr/local/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
RUN chmod 777 /usr/local/tomcat/bin/*.sh
RUN chmod 777 /usr/local/jdk1.8.0_121/bin/*
CMD /usr/local/tomcat/bin/startup.sh &&tail -F /usr/local/tomcat/logs/catalina.out
通过Dockerfile 构建镜像
docker build -t test2 .
运行镜像及数据卷挂载
docker run -it -p 9090:8084 --name mytomcat1 -v /home/docker_study/tomcat_dockerfile/data/webapps:/usr/local/tomcat/webapps -v /home/docker_study/tomcat_dockerfile/data/logs:/usr/local/tomcat/logs test2
发布自己的镜像
登录阿里云Docker Registry
$ docker login --username=zjh961020 registry.cn-hangzhou.aliyuncs.com
将镜像推送到Registry
$ docker login --username=zjh961020 registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/goodzjh_study/tomcat:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/goodzjh_study/tomcat:[镜像版本号]
Docker 网络
理解Docker0
自定义网络
查看所有的网络
docker network ls
创建一个网络
docker network create --driver bridge --subnet 192.168.128.0/24 --gateway 192.168.128.1 mynet
启动镜像的时候 指定网络
docker run -d -P --name tomcat1 --net mynet tomcat
可通过容器名网络连接。
网络联通
容器连同网络
docker network connect mynet tomcat3
标签:容器,基础,usr,镜像,Docker,null,local,docker
From: https://www.cnblogs.com/goodzhangjiahui/p/16626396.html