Copyright ©github:Byron4j | CSDN: https://blog.csdn.net/zixiao217. All Rights Reserved
[] 表示可选项
#命令:docker search [镜像名]
# 示例: 搜索 nginx
docker search nginx
# 命令:docker pull [镜像名]:[镜像tag版本号]
# 示例: 拉取 nginx,默认会使用latest最新,也可以指定tag
docker pull nginx
# 示例:指定tag
docker pull tomcat:9.0
# 命令:docker images
# 示例:使用pull命令拉取镜像后,可以查看本地的镜像;可以查看到镜像id
docker images
# 命令:docker rmi -f [镜像id]
# -f 表示强制删除
# 示例:强制删除本地镜像id为ca546a17a287的镜像
docker rmi -f ca546a17a287
# 命令:docker run --name 容器名称 镜像id:tag
# 运行容器,-it 进入交互式命令终端 最后可以指定命令 bash、sh 等
# 命令:docker run -it --name 容器名称 镜像id:tag /bin/bash
# 示例:
docker run -it --name centos792009 centos:7.9.2009 /bin/bash
[root@9456272d7386 /]#
# 交互式进入容器内部后可以使用linux命令; 退出时可以使用 ctrl+p+q 三键退出
# 命令:docker ps
# 示例:查看正在运行的容器 可以使用 -a 查看所有容器包括已经停止的容器
docker ps
docker ps -a
# 命令:docker rm -f [容器id]
# -f 表示强制
# 示例:删除容器 9456272d7386, 前提是容器已经停止了。不然只能强制删除了
docker rm 9456272d7386
# -d 后台启动,但是对于未提供服务的容器而言,启动后回立即停止。
# 所以可以增加手动改脚本循环执行,来使得容器一直存活
# 容器删除后数据没了,所以这里可以使用过 -v 挂载数据卷: -v 宿主机目录:容器目录, 这样二者会同步数据
docker run -d --name centos7 -v /docker/centos7/opt:/opt centos:7.9.2009 /bin/bash -c "while true; do echo hello docker; sleep 1; done"
# 命令:docker exec -it [容器id] bash命令
# 示例:-it交互式 进入容器
docker exec -it 081efec13d71 bash
# 进入容器后,查看 opt 目录是空的
[root@081efec13d71 /]# ls opt
[root@081efec13d71 /]#
# 也可以看到虚拟机宿主机的 /docker/centos7/opt 也是空的; 这里crf是我的虚拟机的用户名
cd /docker/centos7/opt
crf@crf:/docker/centos7/opt$ ls
crf@crf:/docker/centos7/opt$
# 切换到容器的 /opt 目录创建一个文件,输入内容
[root@081efec13d71 opt]# echo "hello, docker">demo.txt
[root@081efec13d71 opt]# cat demo.txt
hello, docker
#切换到虚拟机主机可以看到 /docker/centos7/opt 目录也同步多了一个demo.txt 文件,这就是-v命令:数据卷挂载
crf@crf:/docker/centos7/opt$ ls
demo.txt
crf@crf:/docker/centos7/opt$ cat demo.txt
hello, docker
# 命令:docker cp [主机文件路径目录] [容器id]:[容器内部存放文件的路径]
# 示例:
crf@crf:/docker/centos7/opt$ docker cp /etc/passwd 081efec13d71:/opt
Successfully copied 4.61kB to 081efec13d71:/opt
#容器中可以看到该文件
[root@081efec13d71 opt]# ls
demo.txt passwd
# 命令:docker cp [容器id]:[容器内部存放文件的路径] [主机文件路径目录]
# 示例:略
# 命令:docker logs [容器id]
crf@crf:~$ docker logs cf937b17d
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
# 示例:查看10行日志 -n 数字
crf@crf:~$ docker logs cf937b17d -n 10
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
hello docker
# 示例:查-f 实时日志
docker logs cf937b17d -f
# 示例:-t 显示时间戳
crf@crf:~$ docker logs cf937b17d -n 10 -t
2024-10-10T05:39:20.068083119Z hello docker
2024-10-10T05:39:21.069233666Z hello docker
2024-10-10T05:39:22.071136333Z hello docker
2024-10-10T05:39:23.072402137Z hello docker
2024-10-10T05:39:24.073867140Z hello docker
2024-10-10T05:39:25.075356512Z hello docker
2024-10-10T05:39:26.076942077Z hello docker
2024-10-10T05:39:27.078471687Z hello docker
2024-10-10T05:39:28.079989730Z hello docker
2024-10-10T05:39:29.081181485Z hello docker
# 命令:docker start|stop|restart 容器id
# 启动、停止、重启容器
# 示例:
crf@crf:~$ docker stop cf937b17d1cc
cf937b17d1cc
crf@crf:~$ docker start cf937b17d1cc
cf937b17d1cc
crf@crf:~$ docker restart cf937b17d1cc
cf937b17d1cc
# 命令:docker inspect 容器id
# 示例:
crf@crf:~$ docker inspect cf937b17d1cc
[
{
"Id": "cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72",
"Created": "2024-10-10T05:38:25.649793517Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true; do echo hello docker; sleep 1; done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1998395,
"ExitCode": 0,
"Error": "",
"StartedAt": "2024-10-10T05:42:51.70213046Z",
"FinishedAt": "2024-10-10T05:42:51.593823572Z"
},
"Image": "sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9",
"ResolvConfPath": "/var/lib/docker/containers/cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72/hostname",
"HostsPath": "/var/lib/docker/containers/cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72/hosts",
"LogPath": "/var/lib/docker/containers/cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72/cf937b17d1ccee28858564a05ac79d429e1b3aaf53722e8a9c0136365b68df72-json.log",
"Name": "/centos7",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/docker/centos7/opt:/opt"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "bridge",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
58,
213
],
"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",
"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": null,
"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/85e908740ba25dd112b8d9c9ab7928c8f18f6c28cdfcff779d8c104f246cc38a-init/diff:/var/lib/docker/overlay2/81fd88686851eb75f26f2f05319de87870cd9a1985fb41bafdd0f78d3c63dd4c/diff",
"MergedDir": "/var/lib/docker/overlay2/85e908740ba25dd112b8d9c9ab7928c8f18f6c28cdfcff779d8c104f246cc38a/merged",
"UpperDir": "/var/lib/docker/overlay2/85e908740ba25dd112b8d9c9ab7928c8f18f6c28cdfcff779d8c104f246cc38a/diff",
"WorkDir": "/var/lib/docker/overlay2/85e908740ba25dd112b8d9c9ab7928c8f18f6c28cdfcff779d8c104f246cc38a/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/docker/centos7/opt",
"Destination": "/opt",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "cf937b17d1cc",
"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 hello docker; sleep 1; done"
],
"Image": "centos:7.9.2009",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201113",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-11-13 00:00:00+00:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e20cd3c6fa174bf7ac6915b928e416559f78f024f45b018f24727168e8799b75",
"SandboxKey": "/var/run/docker/netns/e20cd3c6fa17",
"Ports": {},
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b9367bb1f2cd5698649bb338497bddc5f429270bdcd0d8ce2d05e3f46500772f",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null,
"NetworkID": "6064601b3cc3be8ca73ed7244437498f11dcb08b50df5959b2f5f7df94d040de",
"EndpointID": "b9367bb1f2cd5698649bb338497bddc5f429270bdcd0d8ce2d05e3f46500772f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DNSNames": null
}
}
}
}
]
crf@crf:~$
可以看到我们在运行容器时指定的命令:
"Args": [
"-c",
"while true; do echo hello docker; sleep 1; done"
]
还可以看到挂载的数据卷目录 /docker/centos7/opt:
"Mounts": [
{
"Type": "bind",
"Source": "/docker/centos7/opt",
"Destination": "/opt",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
docker pull mysql:5.7.41
# 查看镜像
crf@crf:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 ca546a17a28c 21 hours ago 470MB
mywebsocket 1.0 764b19b707a1 26 hours ago 131MB
tomcat 9.0 1b746e2e0bd8 40 hours ago 464MB
mysql 5.7 5107333e08a8 10 months ago 501MB
centos 7.9.2009 eeb6ee3f44bd 3 years ago 204MB
centos latest 5d0da3dc9764 3 years ago 231MB
mongo 4.1 d93e3cc722d8 5 years ago 360MB
# 下载 mysql:5.7.41
crf@crf:~$ docker pull mysql:5.7.41
5.7.41: Pulling from library/mysql
e83e8f2e82cc: Pull complete
0f23deb01b84: Pull complete
f5bda3b184ea: Pull complete
ed17edbc6604: Pull complete
33a94a6acfa7: Pull complete
3686cf92b89d: Pull complete
f81535a6a8bf: Pull complete
4bffb03ea5e2: Pull complete
49348ef8dcaa: Pull complete
509d665d0cf5: Pull complete
adc919b937fd: Pull complete
Digest: sha256:bf18020f32cc5d8f5e2add516d52fbf3afc3de431457076340e938596c528171
Status: Downloaded newer image for mysql:5.7.41
docker.io/library/mysql:5.7.41
crf@crf:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.0 ca546a17a28c 21 hours ago 470MB
mywebsocket 1.0 764b19b707a1 26 hours ago 131MB
tomcat 9.0 1b746e2e0bd8 40 hours ago 464MB
mysql 5.7 5107333e08a8 10 months ago 501MB
mysql 5.7.41 3f3447deacaa 18 months ago 455MB
centos 7.9.2009 eeb6ee3f44bd 3 years ago 204MB
centos latest 5d0da3dc9764 3 years ago 231MB
mongo 4.1 d93e3cc722d8 5 years ago 360MB
因为是存储型对象,需要持久化,所以进行一些数据卷挂载。
mkdir -p /docker/mysql/var/lib/mysql
docker run -d -p 3306:3306 \
--priviledged=true \
-v /docker/mysql/var/lib/mysql:/var/lib/myql \
-e MYSQL_ROOT_PASSWORD=111111 \
--name mysql mysql:5.7.41
# 解释说明:
# -p 3306:3306。 -p 宿主机ip:容器ip,端口映射
# --priviledged=true 服务容器root权限
# -v 宿主机目录:容器目录。 挂载数据卷
# -e 环境变量=value
# --name 容器名
Docker自定义文件默认名为 Dockerfile, 也可以使用 docker build -f
命令指定目标文件。
关键字(大写) | 描述 | 示例 |
---|---|---|
FROM | 一般是第一条指令。用于指定使用的基础镜像。 | FROM python:3.9 |
MAINNTAINER | 指定镜像的维护者信息。 | MAINNTAINER zszs@qq.com |
RUN | 镜像构建过程中需要执行的指令。比如在构建过程中需要安装net-tools包: | RUN apt-get install net-tools -y |
WORKDIR | 指定镜像的工作目录,默认交互式进入容器时会进入到该命令指定的目录。 | WORKDIR /opt/ |
COPY | 拷贝文件到镜像中。可以使用容器绝对路径,也可以使用相对于WORKDIR的相对路径 | CP /target/spring-demo.jar /opt/app.jar |
ADD | 拷贝文件到镜像中。压缩文件则会自动解压。 | ADD proj.tar.gz /opt |
VOLUME | 用于将镜像中的目录挂载出来。可以同时挂载多个目录。但是不像-v命令一样可以指定宿主机中的目录,在Dockfile中使用VOLUME命令,只能将镜像目录挂载出来,在启动容器时挂载到宿主机时默认是一个随机字符串的目录。 | VOLUME [“/data”, “/docker”] |
EXPOSE | 暴露端口。这并不是真正暴露端口,只是一个提示作用。真正使用-p来暴露端口。 | EXPOSE 8080 |
ENV | 定义构建过程中的环境变量。定义后,后面的命令可以使用过${变量名}来获取值 | ENV JAVA_HOME /opt/local/jdk1.8 |
USER | 用于指定容器的用户。指定用户前最好创建用户。 | RUN useradd -ms /bin/bash test USER test |
ARG | 用于定义构建参数变量。可以实现在执行docker build时动态向Dockfile文件传递变量。比如构建时使用-build-arg传入一个VERSION变量,默认值为latest。 | ARG VERSION=latest实际构建时:docker build -build-arg VERSION=1.0 |
CMD | 指定容器启动时默认执行的命令。**注意,镜像中若存在多个CMD命令,则只有最后一个CMD会执行。**另外,在运行容器时又指定了要执行的命令,则只会执行运行时的命令,也就是Dockerfile中的CMD会被替换掉。 | CMD [“cd”, “/opt”]CMD echo “hello word”docker run -it 镜像id /bin/bash ls(这里仅仅会执行ls命令) |
ENTRYPOINT | 作用同CMD,也是在容器启动时执行的命令。只不过ENTRYPOINT的命令都会执行,且最后的命令会被追加执行。 | ENTRYPOINT cd /optENTRYPOINT lsdocker run -it 镜像id /bin/bash -l(这里相当于最后执行了ls -l) |
将当前的一个springboot项目构建为一个docker镜像。
FROM openjdk:8-jdk-alpine
WORKDIR /app
EXPOSE 8080
COPY springboot-websocket-0.0.1-SNAPSHOT.jar app.jar
CMD ["java", "-jar", "app.jar"]
解释:
# 基于 openjdk:8-jdk-alpine 的基础镜像
# 工作目录设置为 /app
# 提示暴露端口 8080
# 拷贝当前jar包到
# 运行容器时,执行jar,运行程序
docker build -f Dockerfile -t websocket:1.0 .
这里使用腾讯云的容器镜像服务。
# 登录
docker login ccr.ccs.tencentyun.com --username=你的用户名或者id
# 打 tag
sudo docker tag websocket:1.0 ccr.ccs.tencentyun.com/组织名/websocket:1.0
docker push ccr.ccs.tencentyun.com/组织名/websocket:1.0
企业开发中,可能多个服务,每个服务都是容器化,所以管理多个容器就比较重要了。可以使用docker-compose来管理一组容器。
docker-compose -version curl -L "https://github.com/docker/compose/release/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose -version
------------------------------------------------------------------------
# 或者 ubuntu中使用:
crf@crf:~$ sudo apt install docker-compose
# 查看版本
crf@crf:~$ docker-compose -version
docker-compose version 1.29.2, build unknown