【Docker】基础教学
虚拟机和Linux容器。Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口。它是目前最流行的 Linux 容器解决方案。
官方文档 https://docs.docker.com
阮一峰docker入门教程 http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
概述
环境配置的难题是麻烦的事情,两种解决方案:虚拟机 和 Linux容器。
Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口。 它是目前最流行的 Linux 容器解决方案。
用途
Docker 的主要用途,目前有三大类。
(1)提供一次性的环境。 比如,本地测试他人的软件、持续集成的时候提供单元测试和构建的环境。Fast, consistent delivery of your applications.
(2)提供弹性的云服务。 因为 Docker 容器可以随开随关,很适合动态扩容和缩容。Responsive deployment and scaling.
(3)组建微服务架构。 通过多个容器,一台机器可以跑多个服务,因此在本机就可以模拟出微服务架构。Running more workloads on the same hardware.
架构
Docker使用客户端-服务器(c-s)架构,如下图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qTK5xHz9-1675818115029)(null)]
分层结构
安装
安装docker ce
菜鸟安装教程 https://www.runoob.com/docker/ubuntu-docker-install.html。
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
官方文档安装教程 https://docs.docker.com/engine/install/。
安装成功后,使用如下命令检验是否安装成功
docker version
# 或者
docker info
免sudo使用docker
sudo gpasswd -a ${USER} docker
sudo service docker restart
配置daemon文件
新建/etc/docker/daemon.json
文件,配置镜像、不安全仓库、容器默认ip段,如下
{
"registry-mirrors": ["https://1iopxntw.mirror.aliyuncs.com"],
"insecure-registries": ["harbor.com", "172.16.1.61:5000"],
"bip":"192.168.0.1/24"
}
安装 Docker Compose
安装命令如下
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
也可以把已存在的docker-compose
文件直接拷贝到目录/usr/local/bin/
镜像
命令
docker images
docker search
docker pull
docker rmi
docker build
docker tag
Dockerfile
Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
COPY ADD CMD ENTRYPOINT ENV ARG VOLUME EXPOSE WORKDIR
测试环境gagaServer的Dockerfile
FROM openjdk:11.0.8
LABEL maintainer=""
ENV JAVA_JAR="/code/xxx-server.jar" \
JAVA_OPTS="-server -Xmx2g -Xms2g -Xmn1g \
-XX:SurvivorRatio=6 -XX:MetaspaceSize=512m -XX:ParallelGCThreads=6 \
-XX:MaxTenuringThreshold=5 -XX:+UseG1GC -XX:ConcGCThreads=2 \
-Dspring.profiles.active=dev" \
JAVA_ARGS=""
COPY ./target/gaga-server.jar /code/
CMD ["sh", "-c", "java $JAVA_OPTS -jar $JAVA_JAR $JAVA_ARGS"]
容器
常用命令
docker ps
docker run
docker start
docker stop
docker rm
docker restart
docker exec
docker top
docker inspect
docker export
docker import
实战
docker ps
查看运行中容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
daceb39124e3 172.16.1.61:5000/xxx/xxx-core-dev:20210223.1 "sh -c 'java $JAVA_O…" 23 hours ago Up 23 hours user-core-dev
e594831e1958 172.16.1.61:5000/xxx/xxx-server-dev:20210223.1 "sh -c 'java $JAVA_O…" 24 hours ago Up 24 hours gaga-server-dev
b971259ea76e 172.16.1.61:5000/xxx/xxx-pay-server-dev:20210218.1 "sh -c 'java $JAVA_O…" 5 days ago Up 5 days gaga-pay-server-dev
772ff95a4209 172.16.1.61:5000/xxx/xxx-task-mq-dev:20210204.4 "sh -c 'java $JAVA_O…" 2 weeks ago Up 2 weeks gaga-task-mq-dev
docker ps -a
查看所有容器(运行中/已停止)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4409ca32dba8 172.16.1.61:5000/gchat/managed-multi-test/xxx:20210223.1 "sh -c 'java $JAVA_O…" 19 hours ago Exited (1) 19 hours ago managed-multi-test.gaga_sns_job
a1428f19927b 172.16.1.61:5000/gchat/managed-multi-test/xxx:20210223.1 "sh -c 'java $JAVA_O…" 19 hours ago Exited (1) 19 hours ago managed-multi-test.gaga_sns_im
d9087083d582 172.16.1.61:5000/gchat/managed-multi-test/xxx:20210223.1 "sh -c 'java $JAVA_O…" 19 hours ago Exited (1) 19 hours ago managed-multi-test.gaga_sns_web
daceb39124e3 172.16.1.61:5000/gchat/user-core-dev:20210223.1 "sh -c 'java $JAVA_O…" 23 hours ago Up 23 hours user-core-dev
e594831e1958 172.16.1.61:5000/gchat/xxx-server-dev:20210223.1 "sh -c 'java $JAVA_O…" 24 hours ago Up 24 hours gaga-server-dev
eb1d9066d0e6 172.16.1.61:5000/xxx/managed-pay-test--lh:20210222.1 "sh -c 'java $JAVA_O…" 38 hours ago Exited (1) 19 hours ago managed-pay-test--lh
a3cb008a5ffc 172.16.1.61:5000/xxx/managed-service-test:20210222.1 "sh -c 'java $JAVA_O…" 39 hours ago Exited (1) 19 hours ago managed-service-test
b971259ea76e 172.16.1.61:5000/xxx/xxx-pay-server-dev:20210218.1 "sh -c 'java $JAVA_O…" 5 days ago Up 5 days gaga-pay-server-dev
docker exec -it [容器id] bash
进入容器内部bash环境
[root@localhost ~]# docker exec -it e5 bash
root@localhost:/# ls
bin boot code data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@localhost:/# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg docker-ui.sh java-runtime tag_name_gaga-pay-server-dev.txt tag_name_gaga-task-mq-dev.txt tag_name_user-core-dev.txt
clear-junk-images.sh gaga-yapi nginx tag_name_gaga-server-dev.txt tag_name_gaga-task-mq.txt
[root@localhost ~]#
docker inspect
查看容器运行状态
[root@localhost ~]# docker inspect e5
[
{
"Id": "e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114",
"Created": "2021-02-23T01:30:09.939665043Z",
"Path": "sh",
"Args": [
"-c",
"java $JAVA_OPTS -jar $JAVA_JAR $JAVA_ARGS"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 26750,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-02-23T01:30:11.310569733Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:053dfaaf77f907359b9f5b6583061c2122e545ede7ca9fe36b9c70bcfe297784",
"ResolvConfPath": "/var/lib/docker/containers/e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114/hostname",
"HostsPath": "/var/lib/docker/containers/e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114/hosts",
"LogPath": "/var/lib/docker/containers/e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114/e594831e1958a34bd4acf4730f8de289f91330d81f0f0384410fe71422853114-json.log",
"Name": "/gaga-server-dev",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/data/log/service-api:/data/log/service-api"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "host",
"PortBindings": {},
"RestartPolicy": {
"Name": "always",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"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/34986351a8cc830c701ceca63dd51253605d2c7cc1ef7f5f9a3ffe6c202a8db3-init/diff:/var/lib/docker/overlay2/0105513f429caea561eb37333be81f6be3f6cecccbe4cd3c7f990e9d75d5093e/diff:/var/lib/docker/overlay2/b48d6114b256b17d9a298503893aef0360a5e38d0a73994028fe3c7b028926e0/diff:/var/lib/docker/overlay2/ebadd93aed99082612f30465d31a88b15055d46790ad0d7d74089aa82f9b5019/diff:/var/lib/docker/overlay2/123edda8b8568de11bc60844355ef5108caeb15f3b9223ad05761f783e120489/diff:/var/lib/docker/overlay2/8dec8049d022ee0585e51e2106ccb2a17961856c1f8f11c0c981f57a65997863/diff:/var/lib/docker/overlay2/66b63b8872a0415e178ce47c0116e3cf01e74d64b485dfb870c07471515e189a/diff:/var/lib/docker/overlay2/9cf4f10730b19d8d2a1fbe20f61d0bdfa1de53bf7cb67ab9bd9b2dc2a75c17a8/diff:/var/lib/docker/overlay2/02d4968d71f0870536a9602cb5ab5c508d4d38c3767618e280879cd0a4f382c9/diff",
"MergedDir": "/var/lib/docker/overlay2/34986351a8cc830c701ceca63dd51253605d2c7cc1ef7f5f9a3ffe6c202a8db3/merged",
"UpperDir": "/var/lib/docker/overlay2/34986351a8cc830c701ceca63dd51253605d2c7cc1ef7f5f9a3ffe6c202a8db3/diff",
"WorkDir": "/var/lib/docker/overlay2/34986351a8cc830c701ceca63dd51253605d2c7cc1ef7f5f9a3ffe6c202a8db3/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/data/log/service-api",
"Destination": "/data/log/service-api",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "localhost.localdomain",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"JAVA_OPTS=-Dspring.profiles.active=gaga,dev -Dconfig.server.path=172.16.1.67",
"PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"JAVA_HOME=/usr/local/openjdk-11",
"JAVA_VERSION=11.0.8",
"JAVA_JAR=/code/gaga-server.jar",
"JAVA_ARGS="
],
"Cmd": [
"sh",
"-c",
"java $JAVA_OPTS -jar $JAVA_JAR $JAVA_ARGS"
],
"Image": "172.16.1.61:5000/gchat/gaga-server-dev:20210223.1",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "zhangduanyang@zoneyet.com"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "62fe5c050baad0e436c57f891120d5ff67277fae3f3cb96fb2dfbf9a29ac941f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "849859dba7569ab5f04eb8dc55baff9b61f444ebbc988ff417a4c0d0752c0300",
"EndpointID": "b29cb139700886e68e8edf881515c8d039f51f62929144ebca93124388cd38d9",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
一般需要关注的部分
- Mounts
- 宿主机(本机)与容器的文件夹连接.
- Config
- Env
- 容器环境变量
- Cmd
- 容器启动命令
- Image
- 容器使用的镜像
- Env
一个小技巧
- 容器id 可只输入前几位,保证不重复即可, 如上例中gaga server 可只输入e5
网络模式networking
网络模式有 bridge host overlay macvlan none。https://docs.docker.com/network/
bridge又分user-defined bridges和the default bridge。User-defined bridges provide automatic DNS resolution between containers.
docker network
对docker的网络进行操作。
数据存储storage
存储的方式有3中:卷Volumes, 绑定挂载Bind mounts, 内存型tmpfs。https://docs.docker.com/storage/
推荐使用Volumes方式。
Docker Compose
常用命令
docker-compose up
docker-compose exec
docker-compose down
docker-compose kill
docker-compose rm
实战
#!/bin/bash
set -e
if [[ $1 == 'mysql' || $1 == 'mongo' || $1 == 'redis' || $1 == 'redisPC' || $1 == 'activemq' ]]; then
docker-compose -f mysql-mongo-redis.yml kill $1
docker-compose -f mysql-mongo-redis.yml rm -f $1
docker-compose -f mysql-mongo-redis.yml up -d $1
elif [[ $1 == 'solr' ]]; then
docker-compose -f mysql-mongo-redis.yml kill $1
docker-compose -f mysql-mongo-redis.yml rm -f $1
docker-compose -f mysql-mongo-redis.yml up -d $1
docker-compose -f mysql-mongo-redis.yml exec -u solr solr solr create -c userCore -d /opt/solr/server/solr/configsets/userCore/conf
docker-compose -f mysql-mongo-redis.yml exec -u solr solr solr create -c groupCore -d /opt/solr/server/solr/configsets/groupCore/conf
elif [[ $1 == 'all' ]]; then
docker-compose -f mysql-mongo-redis.yml down
docker-compose -f mysql-mongo-redis.yml up -d
docker-compose -f mysql-mongo-redis.yml exec -d -u solr solr solr create -c userCore -d /opt/solr/server/solr/configsets/userCore/conf
docker-compose -f mysql-mongo-redis.yml exec -d -u solr solr solr create -c groupCore -d /opt/solr/server/solr/configsets/groupCore/conf
else
echo '参数错误'
fi
仓库
官方仓库 https://hub.docker.com/
搭建私有仓库 https://hub.docker.com/r/joxit/docker-registry-ui
更多推荐
所有评论(0)