一、镜像操作
1.搜索镜像
docker search 镜像名称
docker search 镜像名称:标签名称
2.下载镜像
docker pull 镜像名称
[root@localhost docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a330b6cecb98: Pull complete
5ef80e6f29b5: Pull complete
f699b0db74e3: Pull complete
0f701a34c55e: Pull complete
3229dce7b89c: Pull complete
ddb78cb2d047: Pull complete
Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3.镜像打标签
docker tag 镜像名称:旧标签名 镜像名称:新标签名
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 32 hours ago 133MB
[root@localhost docker]# docker tag nginx:latest nginx:lnmp
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 32 hours ago 133MB
nginx lnmp 822b7ec2aaf2 32 hours ago 133MB
4.查看镜像信息
docker inspect 镜像id
docker images -q
q:代表过滤;只过滤容器ID
[root@localhost docker]# docker inspect 822b7ec2aaf2
[root@localhost docker]# docker images -q
822b7ec2aaf2
822b7ec2aaf2
5.删除镜像
docker rm **
docker rmi 镜像ID (或者 镜像名称:标签名)
rm i:rm image
[root@localhost docker]# docker rmi 822b7ec2aaf2
Error response from daemon: conflict: unable to delete 822b7ec2aaf2 (must be forced) - image is referenced in multiple repositories
[root@localhost docker]# docker rmi nginx:latest
Untagged: nginx:latest
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp 822b7ec2aaf2 33 hours ago 133MB
docker rmi nginx:lnmp
[root@localhost docker]# docker rmi nginx:lnmp
Untagged: nginx:lnmp
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 33 hours ago 133MB
6.运行镜像
docker run 镜像名
[root@docker ~]# docker run nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a330b6cecb98: Pull complete
......
2021/09/03 08:52:57 [notice] 1#1: exit
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 822b7ec2aaf2 33 hours ago 133MB
nginx lnmp 822b7ec2aaf2 33 hours ago 133MB
7.镜像的导出、导入
docker save -o 文件名镜像名
docker load < 文件名镜像名
导出
[root@localhost docker]# docker save -o nginx:latest nginx:latest
[root@localhost docker]# ls
daemon.json key.json nginx:latest
导入
[root@localhost docker]# docker load < nginx\:latest
Loaded image: nginx:latest
使用场景,有的生产环境,企业不直接使用docker 私有仓库,而是存放在一个ftp服务器中,按需上传下载。 scp hello-world root@192.168.35.10:/opt ##传到其他docker的内容
二、容器操作
1.查看所有容器(包括非启动状态的容器)
docker ps -a
a:all;全部
docker ps -aq
q:代表过滤;只过滤容器ID
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de9a0588ca0b 822b7ec2aaf2 "/docker-entrypoint.…" 23 minutes ago Exited (0) 21 minutes ago quizzical_volhard
2ee799de4e0c nginx "/docker-entrypoint.…" 24 minutes ago Exited (0) 23 minutes ago admiring_germain
9cc37e301d66 nginx "/docker-entrypoint.…" 26 minutes ago Exited (0) 25 minutes ago sharp_ritchie
2.删除容器
docker rm 容器ID ##删除容器
docker rm -f 容器ID ##强制删除容器(正在运行的容器)
docker ps -a | awk '{print "docker rm " $1'} | bash ##批量删除容器(正则匹配);$1:是容器的id
for i in `docker ps -a | grep -i exit | awk '{print$1}'`; do docker rm -f $i;done ##批量删除"exit"状态(指定状态)的容器
docker rm -f `docker ps -q` ##强制批量删除非up状态的容器;不建议使用,若需要删除删选出id进行删除
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
177891c75c4b nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp eager_tharp
075c017bdb40 nginx:latest "/docker-entrypoint.…" 4 minutes ago Exited (127) 4 minutes ago loving_lewin
[root@localhost ~]# docker rm 075c017bdb40
075c017bdb40
强制删除
[root@localhost ~]# docker rm -f 177891c75c4b
177891c75c4b
3.启动运行容器
1. 使容器开启并持续性运行
创建容器
docker create -it nginx:latest /bin/bash
-i:让容器的标准输入保持打开
-t:分配一个伪终端
-d:后台守护进程的方式运行
启动容器
docker start 容器id
[root@localhost docker]# docker create -it nginx:latest /bin/bash
b2ebf19d65ac6562fdefa67c68fbce0f2f9f5832e7c337dccd3836cf06443b84
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2ebf19d65ac nginx:latest "/docker-entrypoint.…" 24 seconds ago Created(创建状态) eloquent_mccarthy
[root@localhost docker]# docker start b2ebf19d65ac
b2ebf19d65ac
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2ebf19d65ac nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 15 seconds 80/tcp eloquent_mccarthy
2. 启动一次性运行容器
持续性运行浪费资源,那么一次性执行如下操作:
docker run centos:7 /usr/bin/bash -c ls /
-c:传递命令参数
[root@localhost docker]# docker run centos:7 /usr/bin/bash -c ls /
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac0d0db77d6b centos:7 "/usr/bin/bash -c ls…" 42 seconds ago Exited (0) 41 seconds ago frosty_darwin
3.容器的开启、关闭
docker start 容器id
docker stop 容器id
4. 持续后台运行
docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello; done" ##后台运行centos:7的容器
while true;do echo hello; done:代表给予一个死循环
[root@localhost docker]# docker run -d centos:7 /usr/bin/bash -c "while turn;do echo hello; done"
c7858bb06fa80ecf09b820c20c971820f1ec68a90d40b0cb54376061f1bb0e43
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7858bb06fa8 centos:7 "/usr/bin/bash -c 'w…" 29 seconds ago Exited (0) 28 seconds ago tender_zhukovsky
ac0d0db77d6b centos:7 "/usr/bin/bash -c ls…" 2 minutes ago Exited (0) 2 minutes ago frosty_darwin
4.进入/退出容器
1.进入容器
run
docker run -it centos:7 /bin/bash
进入容器没有systemctl 命令解决:添加–privileged=true;指定此容器是否为特权容器,使用此参数,则不能用attach
[root@localhost docker]# docker run -it centos:7 /bin/bash
[root@1b89f40baa26 /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@1b89f40baa26 /]# exit
exit
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b89f40baa26 centos:7 "/bin/bash" About a minute ago Exited (127) 20 seconds ago goofy_joliot
c7858bb06fa8 centos:7 "/usr/bin/bash -c 'w…" 9 minutes ago Exited (0) 9 minutes ago tender_zhukovsky
ac0d0db77d6b centos:7 "/usr/bin/bash -c ls…" 11 minutes ago Exited (0) 11 minutes ago frosty_darwin
exec
docker exec -it 容器ID /bin/bash
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
837a4c69cd81 centos:7 "/bin/bash" 21 seconds ago Up 20 seconds heuristic_almeida
[root@localhost ~]# docker exec -it 837a4c69cd81 /bin/bash
[root@837a4c69cd81 /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@837a4c69cd81 /]# yum -y install httpd
[root@6acc38b198d6 /]# systemctl start httpd
[root@837a4c69cd81 /]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: d
Active: active (running) since Sun 2021-09-05 02:43:18 UTC; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 175 (httpd)
Status: "Processing requests..."
CGroup: /docker/6acc38b198d60a8e841ab6dd25eeca9c422fe5ce811f9984fb5d1a2e3ca5b239/
├─175 /usr/sbin/httpd -DFOREGROUND
├─176 /usr/sbin/httpd -DFOREGROUND
├─177 /usr/sbin/httpd -DFOREGROUND
├─178 /usr/sbin/httpd -DFOREGROUND
├─179 /usr/sbin/httpd -DFOREGROUND
└─180 /usr/sbin/httpd -DFOREGROUND
? 175 /usr/sbin/httpd -DFOREGROUND
Sep 05 02:43:18 6acc38b198d6 systemd[1]: Starting The Apache HTTP Server...
Sep 05 02:43:18 6acc38b198d6 httpd[175]: AH00558: httpd: Could not reliably determin
Sep 05 02:43:18 6acc38b198d6 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
注意点:
在进入centos容器时,systemctl命令用不了。 使用 systemctl start 启动某个服务可能会出现如下错误 : Failed to get D-Bus connection: Operation not permitted 需要给容器提权添加 --privileged=true参数,并将 cmd 或者 entrypoint 设置为 /usr/sbin/init
[root@localhost ~]# docker run -itd --name centos7-v3 --privileged=true centos:7 /sbin/init
6acc38b198d60a8e841ab6dd25eeca9c422fe5ce811f9984fb5d1a2e3ca5b239
弄好之后进入centos容器就可以使用systemctl命令操作了
2. 退出容器
ctrl+d 或者 exit
5.容器导出、导入
容器导出
docker export 容器ID >文件名
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1965c614d66 centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago compassionate_maxwell
6acc38b198d6 centos:7 "/sbin/init" 3 hours ago Exited (137) 5 minutes ago centos7-v3
[root@localhost docker]# docker export e1965c614d66 > centos7
[root@localhost docker]# ls
centos7 daemon.json key.json
容器导入 容器导入(生成镜像)2种方式
docker import 导出的文件名(容器) 指定镜像名称(打上标签)
[root@localhost docker]# docker import centos7 centos7:new
sha256:c18a7e8988bf2ecdc22702f4381a623f324672c468db112da9fe16a238e89b63
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7 new c18a7e8988bf 32 seconds ago 204MB
php latest 1ecee270c294 37 hours ago 480MB
nginx latest 822b7ec2aaf2 47 hours ago 133MB
centos 7 8652b9f0cb4c 9 months ago 204MB
cat 文件名(容器) | docker import - hello-world:latest
[root@localhost ~]# cat ng | docker import - nginx1:new
sha256:826ee37d5806c05de24d45f2410941ce74fc3c04007d4006b431d40d30a9724a
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx1 new 826ee37d5806 10 seconds ago 132MB
|