Docker常⽤操作(⽰例详解)
⽂章⽬录
⼀、镜像的常⽤命令操作
镜像加速
默认都是使⽤公有仓库进⾏下载,在国内,我们需要进⾏镜像加速
详细步骤,可参考
搜索镜像并下载
[root@docker ~]# docker search nginx ##搜索镜像
NAME DESCRIPTION STARS ##星级 OFFICIAL AUTOMATED nginx Official build of Nginx. 13988 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1908 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 791
[OK] ...省略部分内容
[root@docker ~]# docker pull nginx ##下载镜像
Using default tag: latest
latest: Pulling from library/nginx
bb79b6b2107f: Pull complete
5a9f1c0027a7: Pull complete
b5c20b2b484f: Pull complete
166a2418f7e8: Pull complete
1966ea362d23: Pull complete
Digest: sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
镜像下载完存放在:/var/lib/docker,下载的⽂件信息:/var/lib/docker/image/overlay2
查看已有镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 4 days ago 133MB
查看镜像详细信息
[root@docker ~]# docker inspect c39a868aad02
[
{
"Id":"sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1",
"RepoTags":[
"nginx:latest"
],
...省略部分内容
为镜像添加新标签
[root@docker ~]# docker tag nginx:latest nginx:lu
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 4 days ago 133MB
nginx lu c39a868aad02 4 days ago 133MB
删除镜像
[root@docker ~]# docker rmi nginx:lu #删除标签名,删除id号也是可以的
Untagged: nginx:lu
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 4 days ago 133MB
如果有同id的别名镜像 删除id是删不掉的
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 4 days ago 133MB
nginx lu c39a868aad02 4 days ago 133MB
[root@docker ~]# docker rmi c39a868aad02
Error response from daemon: conflict: unable to delete c39a868aad02 (must be forced) - image is referenced in multiple repositories 可以加-f 强制删除
[root@docker ~]# docker rmi -f c39a868aad02
Untagged: nginx:latest
Untagged: nginx:lu
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
注:在容器⾥的镜像不能删除,需先删除容器在删除镜像
导出(存储)镜像到指定⽬录下
[root@docker ~]# docker save -o /opt/nginx nginx:latest
[root@docker ~]# cd /opt
[root@docker opt]# ll
总⽤量 133856
drwx--x--x 4 root root 28 11⽉ 9 14:56 containerd
-rw------- 1 root root 137067008 11⽉ 10 11:13 nginx
drwxr-xr-x. 2 root root 6 3⽉ 26 2015 rh
导⼊镜像
[root@docker opt]# docker rmi nginx:latest
[root@docker opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker opt]# docker load < /opt/nginx
[root@docker opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c39a868aad02 4 days ago 133MB
⼆、容器的常⽤命令操作
容器创建
[root@docker opt]# docker create -it nginx:latest /bin/bash
9cb5b59bfe8bbfa7ff6f4982e9785a76b56922018f64e0bfa5571231c77ad608
'//-i:让容器的标准输⼊保持打开'
'//-t:让docker分配⼀个伪终端'
查看容器
[root@docker opt]# docker ps -a "-a"列出所有的容器,包括未运⾏的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" 58 seconds ago Created bold_chandrasekhar 启动容器
[root@docker opt]# docker start 9cb5b59bfe8b
9cb5b59bfe8b
[root@docker opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" About a minute ago Up 2 seconds 80/tcp bold_chandrasekhar 登录容器,容器要在运⾏处于up状态
[root@docker opt]# docker exec -it 9cb5b59bfe8b /bin/bash
root@9cb5b59bfe8b:/# exit
exit#退出容器
停⽌容器
[root@docker opt]# docker stop 9cb5b59bfe8b
9cb5b59bfe8b
[root@docker opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" 9 minutes ago Exited (0) 10 seconds ago bold_chandrasekhar 启动容器并执⾏命令(执⾏完⾃动退出):docker run 镜像 环境 -c 命令(如果没镜像会⾃动下载)
[root@docker ~]# docker run centos:7 /usr/bin/bash -c ls /
Unable to find image 'centos:7' locally ##没有镜像⾃动下载
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
anaconda-post.log
bin ##执⾏ls命令
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@docker ~]# docker ps -a #发现此容器已经正常退出,此命令执⾏完成后会⾃动退出,释放资源,状态是exited(0)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e28b508ab27 centos:7 "/usr/bin/bash -c ls…" 10 seconds ago Exited (0) 9 seconds ag
o charming_rubin
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" 16 minutes ago Exited (0) 7 minutes ago bold_chandrasekhar 启动容器并执⾏命令(持续在后台运⾏)
[root@docker ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
09fef4819f6ea893ba0e6d0d0df75a6db8f46211f313712354a146059d457886
## 加了-d 在后台运⾏
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09fef4819f6e centos:7 "/bin/bash -c 'while…" 56 seconds ago Up 55 seconds charming_almeida
#状态为up状态,容器可正常停⽌开启
容器导出
[root@docker ~]# docker export 9cb5b59bfe8b > /opt/nginx01
[root@docker ~]# cd /opt
[root@docker opt]# ll
总⽤量 266024
drwx--x--x 4 root root 28 11⽉ 9 14:56 containerd
nginx停止命令-rw------- 1 root root 137067008 11⽉ 10 11:13 nginx
-rw-r--r-- 1 root root 135339008 11⽉ 10 11:40 nginx01
drwxr-xr-x. 2 root root 6 3⽉ 26 2015 rh
容器导⼊(会⽣成镜像,⽽不会创建容器)
[root@docker opt]# cat nginx01 | docker import - nginx:lu
sha256:cfd14ff0c9c4867b8799bcb2034ca183510d745d9c4c35eb186c47c8e63a33a9
[root@docker opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lu cfd14ff0c9c4 9 seconds ago 131MB
nginx latest c39a868aad02 4 days ago 133MB
centos 7 7e6257c9f8d8 3 months ago 203MB
[root@docker opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09fef4819f6e centos:7 "/bin/bash -c 'while…" 6 minutes ago Exited (137) 3 minutes ago charming_almeida
5e28b508ab27 centos:7 "/usr/bin/bash -c ls…" 8 minutes ago Exited (0) 8 minutes ago
charming_rubin
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" 24 minutes ago Exited (0) 15 minutes ago bold_chandrasekhar 删除镜像
[root@docker opt]# docker rm 09fef4819f6e
09fef4819f6e
[root@docker opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e28b508ab27 centos:7 "/usr/bin/bash -c ls…" 11 minutes ago Exited (0) 11 minutes ago charming_rubin
9cb5b59bfe8b nginx:latest "/docker-entrypoint.…" 27 minutes ago Exited (0) 18 minutes ago bold_chandrasekhar 注: 容器运⾏时 容器也不能删除
批量删除容器
[root@docker ~]# docker ps -a | awk '{print"docker rm "$1}' | bash
Error: No such container: CONTAINER
5e28b508ab27
9cb5b59bfe8b
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论