Docker小操作
1.更换自己的阿里云镜像源
阿里云镜像可以自己去https://promotion.aliyun.com/ntms/act/kubernetes.html 注册
- 登陆阿里云开发者平台
- 点击控制台
- 选择容器镜像服务
- 镜像工具--镜像加速器
sudo mkdir -p /etc/docker
sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
sudo systemctl daemon-reload
sudo systemctl restart docker
PS:当然也可以搜一下其他的镜像源
2.常用命令
docker 命令官方文档
https://docs.docker.com/reference/
2.1帮助类启动命令
- 启动docker:
systemctl start docker
- 停止docker:
systemctl stop docker
- 重启docker:
systemctl restart docker
- 查看docker状态:
systemctl status docker
- 开机启动:
systemctl status docker
- 查看docker概要信息:
docker info
- 查看docker总体帮助文档:
docker --help
- 查看docker命令帮助文档:
docker 具体命令 --help
2.2镜像命令
2.2.1 docker images
列出本地主机上的镜像。
OPTIONS说明:
-a :列出本地所有的镜像(含历史映像层)
-q :只显示镜像ID
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
各个选项说明:
- REPOSITORY:表示镜像的仓库源
- TAG:镜像的标签
- IMAGE ID:镜像ID
- CREATED:镜像创建时间
- SIZE:镜像大小
2.2.2 docker search
查找镜像。可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
也可以使用命令:docker search [OPTIONS] 镜像名字
OPTIONS说明:
--limit : 只列出N个镜像,默认25个
docker search --limit 5 redis
$ docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 10891 [OK]
bitnami/redis Bitnami Redis Docker Image 214 [OK]
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 36 [OK]
bitnami/redis-cluster 31
circleci/redis CircleCI images for Redis 12 [OK]
ubuntu/redis Redis, an open source key-value store. Long-… 10
bitnami/redis-exporter 6
google/guestbook-python-redis A simple guestbook example written in Python… 4
clearlinux/redis Redis key-value data structure server with t… 4
ibmcom/ibm-cloud-databases-redis-operator Container image for the IBM Operator for Red… 1
bitnami/redis-sentinel-exporter 1
ibmcom/redis-ha 1
ibmcom/ibm-cloud-databases-redis-operator-bundle Bundle image for the IBM Operator for Redis 1
ibmcom/ibm-cloud-databases-redis-catalog Catalog image for the IBM Operator for Redis 1
rancher/redislabs-ssl 0
blackflysolutions/redis Redis container for Drupal and CiviCRM 0
ibmcom/redisearch-ppc64le 0
drud/redis redis 0 [OK]
ibmcom/redis-ppc64le 0
vmware/redis-photon 0
cimg/redis 0
newrelic/k8s-nri-redis New Relic Infrastructure Redis Integration (… 0
astronomerinc/ap-redis Redis service for Airflow's celery executor … 0
drud/redis_exporter 0
ibmcom/opencontent-redis-3 Docker image for CloudPakOpenContent Redis v… 0
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
2.2.3 docker pull
拉取镜像 docker pull 镜像名字[:TAG]
没有TAG就是最新版。等价于docker pull 镜像名字:latest
$ docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete
1ed3521a5dcb: Pull complete
5999b99cee8f: Pull complete
3f806f5245c9: Pull complete
f8a4497572b2: Pull complete
eafe3b6b8d06: Pull complete
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0.8 16ecd2772934 18 months ago 104MB
$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete
c7a4e4382001: Pull complete
4044b9ba67c9: Pull complete
c8388a79482f: Pull complete
413c8bb60be2: Pull complete
1abfd3011519: Pull complete
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 4 months ago 113MB
redis 6.0.8 16ecd2772934 18 months ago 104MB
2.2.4 docker system df
查看镜像/容器/数据卷所占的空间
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 0 216.9MB 216.9MB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
2.2.5 docker rmi
删除镜像
docker rmi [OPTIONS] 某个XXX镜像名字ID
OPTIONS:
-f : 强制删除
删除单个:docker rmi -f 镜像ID
删除多个:docker rmi -f 镜像名1:TAG 镜像名2:TAG
删除全部:docker rmi -f $(docker images -qa)
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 4 months ago 113MB
redis 6.0.8 16ecd2772934 18 months ago 104MB
$ docker rmi 16ecd2772934
Untagged: redis:6.0.8
Untagged: redis@sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Deleted: sha256:16ecd277293476392b71021cdd585c40ad68f4a7488752eede95928735e39df4
Deleted: sha256:3746030fff867eb26a0338ad9d3ab832e6c19c7dc008090bcfa95c7b9f16f505
Deleted: sha256:1274ec54ad17d15ec95d2180cb1f791057e86dfcdfcc18cd58610a920e145945
Deleted: sha256:18d156147e54edec9a927080fdc0a53c4a8814b0c717b36dc62e637363c1a98d
Deleted: sha256:a8f09c4919857128b1466cc26381de0f9d39a94171534f63859a662d50c396ca
Deleted: sha256:2ae5fa95c0fce5ef33fbb87a7e2f49f2a56064566a37a83b97d3f668c10b43d6
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 4 months ago 113MB
2.3 容器命令
先下个ubuntu的镜像docker pull ubuntu
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 4 months ago 113MB
ubuntu latest ba6acccedd29 6 months ago 72.8MB
2.3.1 新建+启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS说明(常用)
OPTIONS说明(常用):有些是一个减号,有些是两个减号
--name="容器新名字" 为容器指定一个名称;
-d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行);
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
也即启动交互式容器(前台有伪终端,等待交互);
-P: 随机端口映射,大写P
-p: 指定端口映射,小写p
启动交互式容器(前台命令行)
#使用镜像ubuntu:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it ubuntu /bin/bash
参数说明:
-i: 交互式操作。
-t: 终端。
ubuntu: ubuntu镜像。
/bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
要退出终端,直接输入 exit:
[dark@EndeavourOS01 ~]$ docker run -it ubuntu /bin/bash
root@feca0185b2ba:/# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:11 pts/0 00:00:00 /bin/bash
root 9 1 0 03:11 pts/0 00:00:00 ps -ef
root@feca0185b2ba:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@feca0185b2ba:/# exit
exit
[dark@EndeavourOS01 ~]$ docker run -it --name=ub01 ubuntu /bin/bash
root@eb32d7ab8ff3:/#
2.3.2 列出当前所有正在运行的容器
docker ps [OPTIONS]
OPTIONS说明(常用):
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb32d7ab8ff3 ubuntu "/bin/bash" 27 seconds ago Up 26 seconds ub01
[dark@EndeavourOS01 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb32d7ab8ff3 ubuntu "/bin/bash" 35 seconds ago Up 34 seconds ub01
feca0185b2ba ubuntu "/bin/bash" 12 minutes ago Exited (0) 3 minutes ago intelligent_mccarthy
[dark@EndeavourOS01 ~]$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb32d7ab8ff3 ubuntu "/bin/bash" 43 seconds ago Up 42 seconds ub01
[dark@EndeavourOS01 ~]$ docker ps -n 5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eb32d7ab8ff3 ubuntu "/bin/bash" 9 minutes ago Up 9 minutes ub01
feca0185b2ba ubuntu "/bin/bash" 20 minutes ago Exited (0) 12 minutes ago intelligent_mccarthy
[dark@EndeavourOS01 ~]$ docker ps -q
eb32d7ab8ff3
2.3.3 退出容器
两种退出方式:
- exit run进去容器,exit退出,容器停止
- ctrl+p+q run进去容器,ctrl+p+q退出,容器不停止
2.3.4 启动已停止运行的容器
docker start 容器ID或者容器名
2.3.5 重启容器
docker restart 容器ID或者容器名
2.3.6 停止容器
docker stop 容器ID或者容器名
2.3.7 强制停止容器
docker kill 容器ID或容器名
2.3.8 删除已停止的容器
docker rm 容器ID
一次性删除多个容器实例:
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm
-f:强制删除
2.3.9 启动守护式容器(后台服务器)
在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以过 -d 指定容器的后台运行模式。
docker run -d 容器名
#使用镜像ubuntu:latest以后台模式启动一个容器
docker run -d ubuntu
问题:然后docker ps -a 进行查看, 会发现容器已经退出
很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程.
容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。
这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,
我们配置启动服务只需要启动响应的service即可。例如service nginx start
但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,
这样的容器后台启动后,会立即自杀因为他觉得他没事可做了.
所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行,
常见就是命令行模式,表示我还有交互操作,别中断,O(∩_∩)O哈哈~
redis 前后台启动演示case:
-
前台交互式启动
docker run -it redis
-
后台守护式启动
docker run -d redis
2.3.10 查看容器日志
docker logs 容器ID
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6873ff4a1873 redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 6379/tcp interesting_beaver
eb32d7ab8ff3 ubuntu "/bin/bash" 24 minutes ago Up 24 minutes ub01
[dark@EndeavourOS01 ~]$ docker logs 6873ff4a1873
1:C 08 May 2022 03:47:12.639 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 08 May 2022 03:47:12.639 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 08 May 2022 03:47:12.639 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 08 May 2022 03:47:12.640 * monotonic clock: POSIX clock_gettime
1:M 08 May 2022 03:47:12.640 * Running mode=standalone, port=6379.
1:M 08 May 2022 03:47:12.640 # Server initialized
1:M 08 May 2022 03:47:12.640 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 08 May 2022 03:47:12.641 * Ready to accept connections
[dark@EndeavourOS01 ~]$
2.3.11 查看容器内运行的进程
docker top 容器ID
[dark@EndeavourOS01 ~]$ docker top 6873ff4a1873
UID PID PPID C STIME TTY TIME CMD
999 4919 4898 0 11:47 ? 00:00:00 redis-server *:6379
[dark@EndeavourOS01 ~]$
2.3.12 查看容器内部细节
docker inspect 容器ID
[dark@EndeavourOS01 ~]$ docker inspect 6873ff4a1873
[
{
"Id": "6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d",
"Created": "2022-05-08T03:47:11.866223005Z",
"Path": "docker-entrypoint.sh",
"Args": [
"redis-server"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4919,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-05-08T03:47:12.5874105Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631",
"ResolvConfPath": "/var/lib/docker/containers/6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d/hostname",
"HostsPath": "/var/lib/docker/containers/6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d/hosts",
"LogPath": "/var/lib/docker/containers/6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d/6873ff4a1873b99fd5eebcea7afebf7c6684380a9394627beb33ead083e3a20d-json.log",
"Name": "/interesting_beaver",
"RestartCount": 0,
"Driver": "btrfs",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"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": null,
"Name": "btrfs"
},
"Mounts": [
{
"Type": "volume",
"Name": "7f4f3b77a178dcd8ee09b3d7fd4515124c623f5e21dd29029d8c7527d5f47644",
"Source": "/var/lib/docker/volumes/7f4f3b77a178dcd8ee09b3d7fd4515124c623f5e21dd29029d8c7527d5f47644/_data",
"Destination": "/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "6873ff4a1873",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"6379/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"REDIS_VERSION=6.2.6",
"REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.2.6.tar.gz",
"REDIS_DOWNLOAD_SHA=5b2b8b7a50111ef395bf1c1d5be11e6e167ac018125055daa8b5c2317ae131ab"
],
"Cmd": [
"redis-server"
],
"Image": "redis",
"Volumes": {
"/data": {}
},
"WorkingDir": "/data",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "39986f3c830253e678d2c32b6abf9b184965acfa5d063434c1649d261ee60fd0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"6379/tcp": null
},
"SandboxKey": "/var/run/docker/netns/39986f3c8302",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "5bb651b306bf8055cb3f13fc75b4b097b0abae49899c697ce55e4159be6dd8fb",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "3c74bee0ac80741c4daed74965799258b9a2c077ab7306c773ec7bba4c3593bc",
"EndpointID": "5bb651b306bf8055cb3f13fc75b4b097b0abae49899c697ce55e4159be6dd8fb",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
[dark@EndeavourOS01 ~]$
2.3.13 进入正在运行的容器并以命令行交互
-
docker exec -it 容器ID bashShell
[dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 6379/tcp interesting_beaver eb32d7ab8ff3 ubuntu "/bin/bash" 35 minutes ago Up 35 minutes ub01 [dark@EndeavourOS01 ~]$ docker exec -it eb32d7ab8ff3 /bin/bash root@eb32d7ab8ff3:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@eb32d7ab8ff3:/# exit exit [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 16 minutes ago Up 16 minutes 6379/tcp interesting_beaver eb32d7ab8ff3 ubuntu "/bin/bash" 40 minutes ago Up 40 minutes ub01 [dark@EndeavourOS01 ~]$
-
重新进入docker attach 容器ID
[dark@EndeavourOS01 ~]$ docker attach eb32d7ab8ff3 root@eb32d7ab8ff3:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@eb32d7ab8ff3:/# exit exit [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 6379/tcp interesting_beaver
上述两个区别:
- attach 直接进入容器启动命令的终端,不会启动新的进程用exit退出,会导致容器的停止。
- exec 是在容器中打开新的终端,并且可以启动新的进程用exit退出,不会导致容器的停止。
推荐使用 docker exec 命令,因为退出容器终端,不会导致容器的停止。
用之前的redis容器实例进入
-
docker exec -it 容器ID /bin/bash
[dark@EndeavourOS01 ~]$ docker exec -it 6873ff4a1873 /bin/bash root@6873ff4a1873:/data# ls root@6873ff4a1873:/data# redis-cli -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> exit root@6873ff4a1873:/data# exit exit [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 6379/tcp interesting_beaver [dark@EndeavourOS01 ~]$
-
docker exec -it 容器ID redis-cli
[dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 6379/tcp interesting_beaver [dark@EndeavourOS01 ~]$ docker exec -it 6873ff4a1873 redis-cli 127.0.0.1:6379> ping PONG 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> set k2 v2 OK 127.0.0.1:6379> get k2 "v2" 127.0.0.1:6379> exit [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 28 minutes ago Up 28 minutes 6379/tcp interesting_beaver
一般用-d后台启动的程序,再用exec进入对应容器实例
2.3.14 从容器内拷贝文件到主机上
容器→主机
docker cp 容器ID:容器内路径 目的主机路径
先启动并进入容器,随便在某个目录下创建一个文件
[dark@EndeavourOS01 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6873ff4a1873 redis "docker-entrypoint.s…" 32 minutes ago Up 32 minutes 6379/tcp interesting_beaver
380f6bdb7983 redis "docker-entrypoint.s…" 32 minutes ago Exited (0) 32 minutes ago mystifying_bouman
a55be9c5b52d ubuntu "bash" 34 minutes ago Exited (0) 34 minutes ago eloquent_ishizaka
eb32d7ab8ff3 ubuntu "/bin/bash" 56 minutes ago Exited (0) 13 minutes ago ub01
feca0185b2ba ubuntu "/bin/bash" About an hour ago Exited (0) 59 minutes ago intelligent_mccarthy
[dark@EndeavourOS01 ~]$ docker restart ub01
ub01
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6873ff4a1873 redis "docker-entrypoint.s…" 32 minutes ago Up 32 minutes 6379/tcp interesting_beaver
eb32d7ab8ff3 ubuntu "/bin/bash" 56 minutes ago Up 5 seconds ub01
[dark@EndeavourOS01 ~]$ docker exec -it eb32d7ab8ff3 /bin/bash
root@eb32d7ab8ff3:/# pwd
/
root@eb32d7ab8ff3:/# cd /tmp/
root@eb32d7ab8ff3:/tmp# pwd
/tmp
root@eb32d7ab8ff3:/tmp# touch a.txt
root@eb32d7ab8ff3:/tmp# ll
total 0
drwxrwxrwt 1 root root 10 May 8 04:21 ./
drwxr-xr-x 1 root root 174 May 8 03:22 ../
-rw-r--r-- 1 root root 0 May 8 04:21 a.txt
root@eb32d7ab8ff3:/tmp#
然后在另外的主机终端下执行命令
[dark@EndeavourOS01 ~]$ ll fileh/
总计 0
drwxr-xr-x 1 dark dark 0 5月 8日 12:27 .
[dark@EndeavourOS01 ~]$ docker cp eb32d7ab8ff3:/tmp/a.txt /home/dark/fileh/
[dark@EndeavourOS01 ~]$ ll fileh/
总计 0
drwxr-xr-x 1 dark dark 10 5月 8日 12:27 .
-rw-r--r-- 1 dark dark 0 5月 8日 12:21 a.txt
[dark@EndeavourOS01 ~]$
2.3.15 导入和导出容器
-
export 导出容器的内容留作为一个tar归档文件[对应import命令]
docker export 容器ID > 文件名.tar
[dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 43 minutes ago Up 43 minutes 6379/tcp interesting_beaver eb32d7ab8ff3 ubuntu "/bin/bash" About an hour ago Up 11 minutes ub01 [dark@EndeavourOS01 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 fileh [dark@EndeavourOS01 ~]$ docker export eb32d7ab8ff3 > abc.tar [dark@EndeavourOS01 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 abc.tar fileh [dark@EndeavourOS01 ~]$
-
import 从tar包中的内容创建一个新的文件系统再导入为镜像[对应export]
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
[dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 46 minutes ago Up 46 minutes 6379/tcp interesting_beaver eb32d7ab8ff3 ubuntu "/bin/bash" About an hour ago Up 14 minutes ub01 [dark@EndeavourOS01 ~]$ docker rm -f eb32d7ab8ff3 eb32d7ab8ff3 [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 47 minutes ago Up 47 minutes 6379/tcp interesting_beaver [dark@EndeavourOS01 ~]$ ls 公共 模板 视频 图片 文档 下载 音乐 桌面 abc.tar fileh [dark@EndeavourOS01 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 7614ae9453d1 4 months ago 113MB ubuntu latest ba6acccedd29 6 months ago 72.8MB [dark@EndeavourOS01 ~]$ cat abc.tar | docker import - dark/ubuntu:1.0.1 sha256:df0d945a3ac2dfc2b98b9f4b2c450844bfe8c0064f6d8fa513f3b6aefe782539 [dark@EndeavourOS01 ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE dark/ubuntu 1.0.1 df0d945a3ac2 2 seconds ago 72.8MB redis latest 7614ae9453d1 4 months ago 113MB ubuntu latest ba6acccedd29 6 months ago 72.8MB [dark@EndeavourOS01 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6873ff4a1873 redis "docker-entrypoint.s…" 50 minutes ago Up 50 minutes 6379/tcp interesting_beaver [dark@EndeavourOS01 ~]$ docker run -it df0d945a3ac2 /bin/bash root@c592577175d2:/# ll /tmp/ total 0 drwxrwxrwt 1 root root 10 May 8 04:21 ./ drwxr-xr-x 1 root root 174 May 8 04:38 ../ -rw-r--r-- 1 root root 0 May 8 04:21 a.txt root@c592577175d2:/#
2.4 Docker镜像commit操作
docker commit提交容器副本使之成为一个新的镜像
docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]
案例,在原来没有vim
命令的ubuntu容器里,安装vim
然后commit
[dark@EndeavourOS01 ~]$ docker run -it --name=ub01 ubuntu /bin/bash
root@ba27ab86075f:/# ls
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@ba27ab86075f:/# vim
bash: vim: command not found
root@ba27ab86075f:/# apt-get update
`````````````省略`````````````
root@ba27ab86075f:/# apt-get -y install vim
`````````````省略`````````````
root@ba27ab86075f:/# vim a.txt
root@ba27ab86075f:/# cat a.txt
hhhh
root@ba27ab86075f:/#
然后另起终端
[dark@EndeavourOS01 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 4 months ago 113MB
ubuntu latest ba6acccedd29 6 months ago 72.8MB
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba27ab86075f ubuntu "/bin/bash" 16 minutes ago Up 16 minutes ub01
6873ff4a1873 redis "docker-entrypoint.s…" 6 hours ago Up 6 hours 6379/tcp interesting_beaver
[dark@EndeavourOS01 ~]$ docker commit -m="install vim cmd" -a="dark" ba27ab86075f dark/my-ub:1.0.1
sha256:b6248af4b99881493bb5b8a04e972dbfb0c32cd4d5f40de116cae2c7508b7110
[dark@EndeavourOS01 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dark/my-ub 1.0.1 b6248af4b998 4 seconds ago 176MB
redis latest 7614ae9453d1 4 months ago 113MB
ubuntu latest ba6acccedd29 6 months ago 72.8MB
[dark@EndeavourOS01 ~]$
#停掉原来ub01的容器,然后用使用原ubuntu镜像和新commit出来的dark/my-ub镜像运行容器对比
[dark@EndeavourOS01 ~]$ docker stop ba27ab86075f
ba27ab86075f
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6873ff4a1873 redis "docker-entrypoint.s…" 6 hours ago Up 6 hours 6379/tcp interesting_beaver
[dark@EndeavourOS01 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dark/my-ub 1.0.1 b6248af4b998 2 minutes ago 176MB
redis latest 7614ae9453d1 4 months ago 113MB
ubuntu latest ba6acccedd29 6 months ago 72.8MB
[dark@EndeavourOS01 ~]$ docker run -it --name=ub02 ubuntu /bin/bash
root@3ba80780b71a:/# vim
bash: vim: command not found
root@3ba80780b71a:/# [dark@EndeavourOS01 ~]$
[dark@EndeavourOS01 ~]$
[dark@EndeavourOS01 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ba80780b71a ubuntu "/bin/bash" 17 seconds ago Up 15 seconds ub02
6873ff4a1873 redis "docker-entrypoint.s…" 6 hours ago Up 6 hours 6379/tcp interesting_beaver
[dark@EndeavourOS01 ~]$ docker run -it --name=ub03 b6248af4b998 /bin/bash
root@b9963b74dd34:/# ls
a.txt bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
root@b9963b74dd34:/# vim a.txt
`````可以看到有之前的a.txt,随便追加内容`````
root@b9963b74dd34:/# cat a.txt
hhhh
asdasd
root@b9963b74dd34:/#
3. 本地镜像发布到阿里云
3.1 本地镜像发布到阿里云流程
3.2 镜像的生成方法
1.前面提到过的docker commit基于当前容器创建一个新的镜像,新功能增强
docker commit [OPTIONS] 容器ID [REPOSITORY[:TAG]]
OPTIONS说明:
-a :提交的镜像作者;
-m :提交时的说明文字;
2.Dockerfile
暂时略
3.3 将本地镜像推送到阿里云
-
登录阿里云平台https://promotion.aliyun.com/ntms/act/kubernetes.html
-
创建仓库镜像
-
选择控制台,进入容器镜像服务
-
选择个人实例
-
命名空间--创建命名空间--仓库类型公有
-
镜像仓库--创建镜像仓库--选择刚才创建的命名空间--仓库类型公有
-
进入管理界面获得脚本
1. 登录阿里云Docker Registry
$ docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
--username是用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。
您可以在访问凭证页面修改凭证密码。
2. 从Registry中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/yyy/zzz:[镜像版本号]
yyy是你创建的命名空间,zzz是你在yyy命名空间下创建的镜像仓库名
实际的在你自己的阿里云里复制就好了
3.将镜像推送到Registry
$ docker login --username=xxx registry.cn-hangzhou.aliyuncs.com $ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/yyy/zzz:[镜像版本号] $ docker push registry.cn-hangzhou.aliyuncs.com/yyy/zzz[镜像版本号]
请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。
--username是用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码
yyy是你创建的命名空间,zzz是你在yyy命名空间下创建的镜像仓库名
实际的在你自己的阿里云里复制就好了
-
-
将镜像推送到阿里云