`docker - version`
fs-user@fs-user-PC:~$ docker -v
Docker version 19.03.8, build 1b4342cd4c
root@root-PC:~$ sudo docker pull openjdk:8
8: Pulling from library/openjdk
001c52e26ad5: Pulling fs layer
d9d4b9b6e964: Pulling fs layer
2068746827ec: Pulling fs layer
9daef329d350: Waiting
d85151f15b66: Waiting
52a8c426d30b: Waiting
8754a66e0050: Waiting
error pulling image configuration: Get "https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/b2/b273004037cc3af245d8e08cfbfa672b93ee7dcb289736c82d0b58936fb71702/data?verify=1679455442-zqLn%2BvizTb9PKB15ESEGJPvcqos%3D": net/http: TLS handshake timeout
原因:使用docker拉取镜像失败。
解决方法:在/etc/resolv.conf文件中新增一个参数
nameserver 8.8.8.8
保存,重启docker
service docker redtart
重新拉取镜像。
root@root-PC:/etc$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 904b8cb13b93 2 weeks ago 142MB
openjdk 8 b273004037cc 7 months ago 526MB
使用 docker rmi命令即可删除指定镜像,强制删除加 -f
docker rmi java
删除所有镜像
docker rmi $(docker images -q)
使用以下docker run命令即可新建并启动一个容器,该命令是最常用的命令,它有很多选项,下面将列举一些常用的选项。
-d选项:表示后台运行
-P选项:随机端口映射
-p选项:指定端口映射,有以下四种格式。
– ip:hostPort:containerPort
– ip::containerPort
– hostPort:containerPort
– containerPort
–net选项:指定网络模式,该选项有以下可选参数:
–net=bridge:默认选项,表示连接到默认的网桥。
–net=host:容器使用宿主机的网络。
–net=container:NAME-or-ID:告诉 Docker让新建的容器使用已有容器的网络配置。
–net=none:不配置该容器的网络,用户可自定义网络配置。
sudo docker run -d -p 91:80 nginx
fs-user@fs-user-PC:/etc$ sudo docker run -d -p 91:80 nginx
b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4
这样就能启动一个 Nginx容器。在本例中,为 docker run添加了两个参数,含义如下:
-d 后台运行
-p 宿主机端口:容器端口 #开放容器端口到宿主机端口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YFHFGvyM-1679535258324)(/home/fs-user/.config/Typora/typora-user-images/image-20230322111235693.png)]
sudo docker ps
fs-user@fs-user-PC:/etc$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b644707b5f96 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:91->80/tcp gallant_dubinsky
sudo docker stop b644707b5f96
fs-user@fs-user-PC:/etc$ sudo docker stop b644707b5f96
b644707b5f96
sudo docker kill 39b85cd1ab22
fs-user@fs-user-PC:/etc$ sudo docker kill 39b85cd1ab22
sudo docker start b644707b5f96
fs-user@fs-user-PC:/etc$ sudo docker start b644707b5f96
b644707b5f96
sudo docker inspect b644707b5f96
fs-user@fs-user-PC:/etc$ sudo docker inspect b644707b5f96
[
{
"Id": "b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4",
"Created": "2023-03-22T03:12:17.429285446Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 37395,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-03-22T03:16:47.76452428Z",
"FinishedAt": "2023-03-22T03:14:57.806904696Z"
},
"Image": "sha256:904b8cb13b932e23230836850610fa45dce9eb0650d5618c2b1487c2a4f577b8",
"ResolvConfPath": "/var/lib/docker/containers/b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4/hostname",
"HostsPath": "/var/lib/docker/containers/b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4/hosts",
"LogPath": "/var/lib/docker/containers/b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4/b644707b5f9616eb42291a0dd8dcc40549bf24964486693c837559554bb077a4-json.log",
"Name": "/gallant_dubinsky",
"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": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "91"
}
]
},
"RestartPolicy": {
"Name": "no",
"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/52cb2ff553520cd7597f347c48d68c3e3f165915b1851377e2673f058371f692-init/diff:/var/lib/docker/overlay2/bfb72da2f128a1409e9a787e3cfeaf88c8df98f1780fba07ea6773fec9cc0f2e/diff:/var/lib/docker/overlay2/4b9564de37ed2c242756ac01529a8009a946819fcd7c8be6ae2440fff6ae079c/diff:/var/lib/docker/overlay2/da6d7f613906f4eaee23afabb53c77d31f85cdee80891f65fb62b14740aaa45c/diff:/var/lib/docker/overlay2/f507ac876b5852ce44371d0d82f404e9696e7b08787433737e3d3fcdca4ed2a3/diff:/var/lib/docker/overlay2/4689ce17a70d63660c0ef3f065d890ead7e30484f9779013121786db9a51edde/diff:/var/lib/docker/overlay2/a5be3373ff3e01fb41fcd1adedd7ed0d77aac4e3ee4df848976d488eb9cbcb20/diff",
"MergedDir": "/var/lib/docker/overlay2/52cb2ff553520cd7597f347c48d68c3e3f165915b1851377e2673f058371f692/merged",
"UpperDir": "/var/lib/docker/overlay2/52cb2ff553520cd7597f347c48d68c3e3f165915b1851377e2673f058371f692/diff",
"WorkDir": "/var/lib/docker/overlay2/52cb2ff553520cd7597f347c48d68c3e3f165915b1851377e2673f058371f692/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "b644707b5f96",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.23.3",
"NJS_VERSION=0.7.9",
"PKG_RELEASE=1~bullseye"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers "
},
"StopSignal": "SIGQUIT"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "87c486b8a2e42155d6cc36288f2cd51dd1b52287cfb94cb29e82ba3d8f4a6bc1",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "91"
}
]
},
"SandboxKey": "/var/run/docker/netns/87c486b8a2e4",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2d0a3b3bd1b9751a2b7d4190bf369e1ca8e660ac1072524341a2e86ea4bd42f4",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "cf25ae835549ce48dcbe36fc6c9db2df3d5a2349abc1e6621fc0cbf48b20407c",
"EndpointID": "2d0a3b3bd1b9751a2b7d4190bf369e1ca8e660ac1072524341a2e86ea4bd42f4",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
sudo docker container logs b644707b5f96
sudo docker top b644707b5f96
fs-user@fs-user-PC:/etc$ sudo docker top b644707b5f96
UID PID PPID C STIME TTY TIME CMD
root 37395 37376 0 11:16 ? 00:00:00 nginx: master process nginx -g daemon off;
systemd+ 37439 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37440 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37441 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37442 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37443 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37444 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37445 37395 0 11:16 ? 00:00:00 nginx: worker process
systemd+ 37446 37395 0 11:16 ? 00:00:00 nginx: worker process
从容器里面拷文件到宿主机:
docker cp 容器id:要拷贝的文件在容器里面的路径 宿主机的相应路径
从宿主机拷文件到容器里面:
docker cp 要拷贝的宿主机文件路径 容器id:要拷贝到容器里面对应的路径
使用docker exec命令用于进入一个正在运行的docker容器。如果docker run命令运行容器的时候,没有使用-it参数,就要用这个命令进入容器。一旦进入了容器,就可以在容器的 Shell 执行命令了
apt-get update apt-get install vim #安装vim
apt-get install iputils-ping #安装ping
apt-get install net-tools #安装ifconfig
docker exec -it f0b1c8ab3633 /bin/bash (有的容器需要把 /bin/bash 换成 sh)
使用 docker rm命令即可删除指定容器
docker rm f0b1c8ab3633
该命令只能删除已停止的容器,如需删除正在运行的容器,可使用-f参数
强制删除所有容器
docker rm -f $(docker ps -a -q)
sudo docker cp ~/Desktop/cmgde2eas-sr-rs.jar b644707b5f96:/usr/share/
sudo docker exec b644707b5f96 ls -al
fs-user@fs-user-PC:~/Desktop$ sudo docker exec b644707b5f96 ls -al
total 92
drwxr-xr-x 1 root root 4096 Mar 22 04:24 .
drwxr-xr-x 1 root root 4096 Mar 22 04:24 ..
-rwxr-xr-x 1 root root 0 Mar 22 03:12 .dockerenv
drwxr-xr-x 2 root root 4096 Feb 27 00:00 bin
drwxr-xr-x 2 root root 4096 Dec 9 19:15 boot
drwxr-xr-x 5 root root 340 Mar 22 03:16 dev
drwxr-xr-x 1 root root 4096 Mar 1 18:43 docker-entrypoint.d
-rwxrwxr-x 1 root root 1616 Mar 1 18:42 docker-entrypoint.sh
drwxr-xr-x 1 root root 4096 Mar 22 03:12 etc
drwxr-xr-x 2 root root 4096 Dec 9 19:15 home
drwxr-xr-x 1 root root 4096 Feb 27 00:00 lib
drwxr-xr-x 2 root root 4096 Feb 27 00:00 lib64
drwxr-xr-x 2 root root 4096 Feb 27 00:00 media
drwxr-xr-x 2 root root 4096 Feb 27 00:00 mnt
drwxr-xr-x 2 root root 4096 Feb 27 00:00 opt
dr-xr-xr-x 474 root root 0 Mar 22 03:16 proc
drwx------ 2 root root 4096 Feb 27 00:00 root
drwxr-xr-x 1 root root 4096 Mar 22 03:16 run
drwxr-xr-x 2 root root 4096 Feb 27 00:00 sbin
drwxr-xr-x 2 root root 4096 Feb 27 00:00 srv
dr-xr-xr-x 13 root root 0 Mar 22 03:16 sys
drwxrwxrwt 1 root root 4096 Mar 1 18:43 tmp
drwxr-xr-x 1 root root 4096 Feb 27 00:00 usr
drwxr-xr-x 1 root root 4096 Feb 27 00:00 var
sudo docker exec b644707b5f96 ls -al /usr/share
fs-user@fs-user-PC:~/Desktop$ sudo docker exec b644707b5f96 ls -al /usr/share
total 98644
drwxr-xr-x 1 root root 4096 Mar 22 04:24 .
drwxr-xr-x 1 root root 4096 Feb 27 00:00 ..
drwxr-xr-x 3 root root 4096 Mar 1 18:43 X11
drwxr-xr-x 2 root root 4096 Feb 27 00:00 adduser
drwxr-xr-x 2 root root 4096 Feb 27 00:00 base-files
drwxr-xr-x 2 root root 4096 Feb 27 00:00 base-passwd
drwxr-xr-x 3 root root 4096 Jun 10 2021 bash-completion
drwxr-xr-x 1 root root 4096 Mar 1 18:43 bug
drwxr-xr-x 3 root root 4096 Mar 1 18:42 ca-certificates
-rw-r--r-- 1 1000 1000 100820645 Mar 22 03:37 cmgde2eas-sr-rs.jar
在 Spring Boot 项目的根目录下创建一个 Dockerfile 文件,内容如下:
# Docker image for springboot file run
# VERSION 0.0.1
# Author:
# 基础镜像使用java
FROM openjdk:8
# 作者
MAINTAINER ycz
# VOLUME 指定了临时文件目录为/tmp。
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为xx.jar
ENV LANG=en_US.UTF-8
ENV LC_CTYPE="en_US.UTF-8"
ENV LC_NUMERIC="en_US.UTF-8"
ENV LC_TIME="en_US.UTF-8"
ENV LC_COLLATE="en_US.UTF-8"
ENV LC_MONETARY="en_US.UTF-8"
ENV LC_MESSAGES="en_US.UTF-8"
ENV LC_PAPER="en_US.UTF-8"
ENV LC_NAME="en_US.UTF-8"
ENV LC_ADDRESS="en_US.UTF-8"
ENV LC_TELEPHONE="en_US.UTF-8"
ENV LC_MEASUREMENT="en_US.UTF-8"
ENV LC_IDENTIFICATION="en_US.UTF-8"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
&& echo $TZ > /etc/timezone
ADD cmgde2eas-sr-rs.jar cmgde2eas-sr-rs.jar
# 运行jar包
RUN bash -c 'touch /cmgde2eas-sr-rs.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/cmgde2eas-sr-rs.jar"]
在命令行中进入到 Dockerfile 所在目录,执行以下命令构建 Docker 镜像:
sudo docker build . -t test-demo:v1.0
执行以下命令,运行 Docker 容器:
sudo docker run -d -p 8080:8080 --name test-demo test-demo:v1.0
sudo docker logs -f --tail=1000 test-demo
sudo docker stop test-demo
sudo docker start test-demo
fs-user@fs-user-PC:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1fdceb13212 test-demo:v1.0 "java -Djava.securit…" 13 minutes ago Exited (143) 5 minutes ago test-demo
b644707b5f96 nginx "/docker-entrypoint.…" 4 hours ago Exited (0) 4 minutes ago gallant_dubinsky
fs-user@fs-user-PC:~$ sudo docker rm b1fdceb13212
b1fdceb13212
fs-user@fs-user-PC:~$ sudo docker rm b644707b5f96
b644707b5f96
fs-user@fs-user-PC:~/Desktop/springboot$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b644707b5f96 nginx "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:91->80/tcp gallant_dubinsky
fs-user@fs-user-PC:~/Desktop/springboot$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1fdceb13212 test-demo:v1.0 "java -Djava.securit…" 8 minutes ago Exited (143) 21 seconds ago test-demo
b644707b5f96 nginx "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:91->80/tcp gallant_dubinsky
sudo docker image
sudo docker rmi 3671947e3fc1
fs-user@fs-user-PC:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test-demo v1.0 3671947e3fc1 24 minutes ago 728MB
nginx latest 904b8cb13b93 2 weeks ago 142MB
openjdk 8 b273004037cc 7 months ago 526MB
fs-user@fs-user-PC:~$ sudo docker rmi 3671947e3fc1
Untagged: test-demo:v1.0
Deleted: sha256:3671947e3fc1e0dc5734e4bede48a91392c9210f38b473d4f6af6a9ef506a65f
Deleted: sha256:46ddcfd85d90801a4e964365a335660b51274cf7806ad404ae0cbe007efd02dc
Deleted: sha256:85937548756fb08d9e5d0aa6a3f365a8572a2254487177887d16a5d3a3fc950e
Deleted: sha256:d6d8176c1deca00677af08cd35c98f86a6d30671ea89e956f929f8b4a0719ddd
Deleted: sha256:24f39a586f5b3b65d97d50c0b07a703d8cbfd39e5ecc0ae6531a5e7cb463ca10
Deleted: sha256:20e862f75807b4d38abc786aa0c5dfc9e142ba1dfa928a3ebd5c1ffa406e977f
Deleted: sha256:efcbcc8463d2e09a4398feb28e60753de83ba0a8e6a77c4f84bb4bf422cabb03
Deleted: sha256:fea189a6ea98c975cba26a2c2888d4b6dcd8236cabeb6a098cdd29b5bf78d94d
Deleted: sha256:2f049e8b4ab48e912a9fe389d36c8b033582bcad2922c4ff907212d4207c70de
Deleted: sha256:c5ebe74f16a80edc892b373159d36dbbd28debe1863de038e856ad94373126d6
Deleted: sha256:cd28272746d281f27bebc14168135606704c63d9825b4323e7929eb79ba6ef7e
Deleted: sha256:7bdc072bc387892019d05ef838cb5d08fa1e6ef886dd424d6b0dd4f9fca72f42
Deleted: sha256:03599e3a340b79347944e3498a468c16a85aa29c262ae9212f096bd3328140a3
Deleted: sha256:33d0b4cf437f913a7ad55bc2977cd183f7a3ab10c29d453f52f6a3b7eb70fc6e
Deleted: sha256:479f42d80b8c6f8ccc2bc115df08d04c9fc8056a48cf62572fb5cdd6ae3e617d
Deleted: sha256:e1b4aef1f5c9325cd7b5662f99125235b0b64f96314c32506a0328afbbd6d71c
Deleted: sha256:d32b00243500c9e043b2d0dafefdd29f2cddcc2c457199c630d202446bd72ef0
Deleted: sha256:38d7e369783904d4a83d16d1dfe2639f00de3f7d0845987fa7b5d61a6477ab28
Deleted: sha256:e4c692f43dbceb52f610470e5c93385c1f34f1f00e894c445ba64d630ea793ec
Deleted: sha256:e0d34b1dd4d79f9361127763271e7d3f758c5ccdf8e8491687e89c07274931cf
Deleted: sha256:0d327e9facdf32bc4beadd182d46c272a9a6446c8fcb0585b40eaf33cca3a2b9
Deleted: sha256:6a021a38f8100f5d1666217e4dc3fd555700ac25fde14df0f286ad5c0ee7e8f4
Deleted: sha256:865f98a0dd76a2a75b6f7750fdeb6da8d713b1da6c32c29502564285e9fb0348