Devops:Docker常用命令
Posted yy3b2007com
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Devops:Docker常用命令相关的知识,希望对你有一定的参考价值。
列出镜像列表(docker images)
[root@master docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 7 months ago 1.84kB [root@master docker]#
各个指标项说明::
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
启动镜像(docker run)
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。
语法
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
参数说明:
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-P: (将容器内部使用的网络端口映射到我们使用的主机上)随机端口映射,容器内部端口随机映射到主机的高端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="nginx-lb": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
-h "mars": 指定容器的hostname;
-e username="ritchie": 设置环境变量;
--env-file=[]: 从指定文件读入环境变量;
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值;
--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
--link=[]: 添加链接到另一个容器;
--expose=[]: 开放一个端口或一组端口;
--volume , -v: 绑定一个卷
实例
使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。
docker run --name mynginx -d nginx:latest
使用镜像nginx:latest以后台模式启动一个容器,并将容器的80端口映射到主机随机端口。
docker run -P -d nginx:latest
使用镜像 nginx:latest,以后台模式启动一个容器,将容器的 80 端口映射到主机的 80 端口,主机的目录 /data 映射到容器的 /data。
docker run -p 80:80 -v /data:/data -d nginx:latest
绑定容器的 8080 端口,并将其映射到本地主机 127.0.0.1 的 80 端口上。
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
使用镜像nginx:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
root@master docker]# docker run -it nginx:latest /bin/bash
root@b8573233d675:/#
使用镜像centos 以交互模式启动一个容器,在容器内执行/bin/bash命令。
root@master docker]# docker run --name mydocker -t -i centos /bin/bash Unable to find image ‘centos:latest‘ locally latest: Pulling from library/centos d8d02d457314: Pull complete Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb Status: Downloaded newer image for centos:latest [root@1d8a5ab31830 /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11820 1896 pts/0 Ss 15:00 0:00 /bin/bash root 15 0.0 0.0 51740 1732 pts/0 R+ 15:01 0:00 ps aux [root@1d8a5ab31830 /]# [root@1d8a5ab31830 /]# exit exit [root@master docker]#
#创建一个文件目录,用于挂载到容器内,做oracle数据备份时数据存放的位置,保证备份数据不丢失。 mkdir /usr/local/data_temp chmod 777 /usr/local/data_temp docker run -d --name oracle12c -p 1521:1521 -v /usr/local/data_temp:/home/oracle/data_temp sath89/oracle-12c
备注:oracle12c是名称,-p是端口映射,-v是将宿主机的/usr/local/data_temp 目录映射到容器内的 /home/oracle/data_temp内。
列出容器列表(docker ps )
语法
docker ps [OPTIONS]
OPTIONS说明:
-a :显示所有的容器,包括未运行的。
-f :根据条件过滤显示的内容。
--format :指定返回值的模板文件。
-l :显示最近创建的容器。
-n :列出最近创建的n个容器。
--no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。
root@master docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32098924b2e0 hello-world "/hello" 6 minutes ago Exited (0) 6 minutes ago hopeful_northcutt
b07906c995f3 hello-world "/hello" 7 minutes ago Exited (0) 7 minutes ago keen_goldstine
[root@master docker]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32098924b2e0 hello-world "/hello" 8 minutes ago Exited (0) 8 minutes ago hopeful_northcutt
查询最后一次创建的容器
[root@master docker]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@master docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99d7fbb94565 hello-world "/hello" 3 seconds ago Exited (0) 3 seconds ago gracious_panini
1d8a5ab31830 centos "/bin/bash" 34 minutes ago Exited (137) 4 minutes ago mydocker
[root@master docker]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99d7fbb94565 hello-world "/hello" 21 seconds ago Exited (0) 21 seconds ago gracious_panini
[root@master docker]#
进入容器方法
一、attach(不建议使用)
注意:此方法当exit退出容器后,进程也就结束了,容器就关闭了。不建议使用
[root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago mydocker 32098924b2e0 hello-world "/hello" 16 minutes ago Exited (0) 16 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 17 minutes ago Exited (0) 17 minutes ago keen_goldstine [root@master docker]# docker start 1d8a5ab31830 #再次将容器启动 1d8a5ab31830 [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 5 minutes ago Up 6 seconds mydocker 32098924b2e0 hello-world "/hello" 16 minutes ago Exited (0) 16 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 18 minutes ago Exited (0) 18 minutes ago keen_goldstine [root@master docker]# docker attach 1d8a5ab31830 [root@1d8a5ab31830 /]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [root@1d8a5ab31830 /]# exit exit [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 8 minutes ago Exited (0) 3 seconds ago mydocker 32098924b2e0 hello-world "/hello" 19 minutes ago Exited (0) 19 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 20 minutes ago Exited (0) 20 minutes ago keen_goldstine [root@master docker]#
attach总结:一个容器指定一个进程,这个容器退出了。他的生成周期也就结束了,也就退出了。
二、exec
注意:此方法当exit退出容器后,进程不结束,容器也不关闭。
docker exec -it 1d8a5ab31830 /bin/bash #其中 1d8a5ab31830 为容器ID号
docker exec :在运行的容器中执行命令
OPTIONS说明:
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端
[root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 8 minutes ago Exited (0) 3 seconds ago mydocker 32098924b2e0 hello-world "/hello" 19 minutes ago Exited (0) 19 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 20 minutes ago Exited (0) 20 minutes ago keen_goldstine [root@master docker]# docker exec -it 1d8a5ab31830 /bin/bash Error response from daemon: Container 1d8a5ab31830dc16e3129a6e123a5c16bc10989aeced01a3be62d236ee76cb0d is not running [root@master docker]# docker start 1d8a5ab31830 1d8a5ab31830 [root@master docker]# docker exec -it 1d8a5ab31830 /bin/bash [root@1d8a5ab31830 /]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [root@1d8a5ab31830 /]# exit exit [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 10 minutes ago Up 23 seconds mydocker 32098924b2e0 hello-world "/hello" 21 minutes ago Exited (0) 21 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago keen_goldstine [root@master docker]#
三、nsenter(推荐使用这个命令进入容器)
注意:当退出容器后,还能让容器再后台运行。推荐使用这个命令进入容器。
[root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 10 minutes ago Up 23 seconds mydocker 32098924b2e0 hello-world "/hello" 21 minutes ago Exited (0) 21 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago keen_goldstine [root@master docker]# docker stop 1d8a5ab31830 1d8a5ab31830 [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 13 minutes ago Exited (137) 4 seconds ago mydocker 32098924b2e0 hello-world "/hello" 24 minutes ago Exited (0) 24 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago keen_goldstine [root@master docker]# docker start 1d8a5ab31830 #要先启动容器,才能进入容器里边 1d8a5ab31830 [root@master docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 13 minutes ago Up 5 seconds mydocker [root@master docker]# docker inspect --format ".State.Pid" 1d8a5ab31830 #查看容器的进程ID 2841 [root@master docker]# nsenter -t 2841 -u -i -n #进入容器 [root@1d8a5ab31830 docker]# [root@1d8a5ab31830 docker]# exit #退出容器,进程还在 logout [root@master docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 15 minutes ago Up About a minute mydocker [root@master docker]#
可以写一个脚本方便进容器:
[root@master docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 21 minutes ago Up 7 minutes mydocker [root@master docker]# echo ‘‘ > shelldocker.sh [root@master docker]# [root@master docker]# vi shelldocker.sh #!/bin/bash PID=$(docker inspect --format ".State.Pid" $1) nsenter -t $PID -u -i -n ~ "shelldocker.sh" 3L, 88C written [root@master docker]# [root@master docker]# ls shelldocker.sh [root@master docker]# chmod +x shelldocker.sh [root@master docker]# [root@master docker]# ./shelldocker.sh 1d8a5ab31830 [root@1d8a5ab31830 docker]# exit logout [root@master docker]#
查找镜像(docker search)
使用 docker search 命令来从 Docker Hub 网站来搜索镜像
docker search nginx(查找镜像)
[root@master docker]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 11852 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1641 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 735 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 73 bitnami/nginx Bitnami nginx Docker Image 70 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 51 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 23 nginxdemos/hello NGINX webserver that serves a simple page co… 23 [OK] nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20 jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 20 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 [OK] crunchgeek/nginx-pagespeed Nginx with PageSpeed + GEO IP + VTS + more_s… 13 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 11 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 10 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 9 sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 5 [OK] nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 5 1science/nginx Nginx Docker images that include Consul Temp… 5 [OK] mailu/nginx Mailu nginx frontend 3 [OK] pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK] travix/nginx NGinx reverse proxy 2 [OK] centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK] wodby/nginx Generic nginx 0 [OK]
拉取镜像(docker pull )
docker pull nginx(拉取镜像)
[root@master docker]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 1ab2bdfe9778: Pull complete a17e64cfe253: Pull complete e1288088c7a8: Pull complete Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9 Status: Downloaded newer image for nginx:latest [root@master docker]#
从容器创建一个新的镜像(docker commit )
语法
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
OPTIONS说明:
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
实例
将容器a404c6c174a2 保存为新的镜像,并添加提交人信息和说明信息。
[root@master docker]# docker commit -a "runoob.com(提交的镜像作者)" -m "my apache(提交是的说明文字)" a404c6c174a2 mymysql:v1
sha256:37af1236adef1544e8886be23010b66577647a40bc02c0885a6600b33ee28057
[root@master docker]# docker images mymysql:v1
REPOSITORY TAG IMAGE ID CREATED SIZE
mymysql v1 37af1236adef 15 seconds ago 329 MB
推送镜像(docker push)
TODO....
暂时参考《通过容器提交镜像(docker commit)以及推送镜像(docker push)笔记》
容器内部的标准输出(docker logs)
docker logs [ID或者名字] 可以查看容器内部的标准输出
$ docker logs 353e6c82b2fc
$ docker logs -f 353e6c82b2fc
-f:让 dokcer logs 像使用 tail -f 一样来输出容器内部的标准输出。
docker logs [ID或者名字]
[root@master docker]# docker logs 1d8a5ab31830 [root@1d8a5ab31830 /]# ps aux bash: 平ps: command not found [root@1d8a5ab31830 /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11820 1896 pts/0 Ss 15:00 0:00 /bin/bash root 15 0.0 0.0 51740 1732 pts/0 R+ 15:01 0:00 ps aux [root@1d8a5ab31830 /]# [root@1d8a5ab31830 /]# exit exit [root@1d8a5ab31830 /]# echo PATH PATH [root@1d8a5ab31830 /]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [root@1d8a5ab31830 /]# exit exit [root@1d8a5ab31830 /]# [root@master docker]#
docker logs -f [ID或者名字]
root@master docker]# docker logs -f 1d8a5ab31830 [root@1d8a5ab31830 /]# ps aux bash: 平ps: command not found [root@1d8a5ab31830 /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 11820 1896 pts/0 Ss 15:00 0:00 /bin/bash root 15 0.0 0.0 51740 1732 pts/0 R+ 15:01 0:00 ps aux [root@1d8a5ab31830 /]# [root@1d8a5ab31830 /]# exit exit [root@1d8a5ab31830 /]# echo PATH PATH [root@1d8a5ab31830 /]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [root@1d8a5ab31830 /]# exit exit [root@1d8a5ab31830 /]#
停止应用容器(docker stop)
$ docker stop [cid|names] (可以使用cid和容器名)
[root@master docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 29 minutes ago Up 15 minutes mydocker [root@master docker]# docker stop mydocker mydocker [root@master docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@master docker]#
移除应用容器(docker rm)
$ docker rm [CID|NAMES] 可以使用cid和容器名)
$ docker rm -f CID|NAMES
[root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 31 minutes ago Exited (137) About a minute ago mydocker 32098924b2e0 hello-world "/hello" 42 minutes ago Exited (0) 42 minutes ago hopeful_northcutt b07906c995f3 hello-world "/hello" 44 minutes ago Exited (0) 44 minutes ago keen_goldstine [root@master docker]# docker rm hopeful_northcutt hopeful_northcutt [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 31 minutes ago Exited (137) About a minute ago mydocker b07906c995f3 hello-world "/hello" 44 minutes ago Exited (0) 44 minutes ago keen_goldstine [root@master docker]# docker rm b07906c995f3 b07906c995f3 [root@master docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d8a5ab31830 centos "/bin/bash" 32 minutes ago Exited (137) 2 minutes ago mydocker [root@master docker]#
Docker删除容器与镜像(不建议使用,使用要注意)
停止所有的container,这样才能够删除其中的images:
docker stop $(docker ps -a -q) docker kill $(docker ps -a -q)
如果想要删除所有container的话再加一个指令:
docker rm $(docker ps -a -q)
删除images,通过image的id来指定删除谁
docker rmi <image id>
想要删除untagged images,也就是那些id为的image的话可以用
docker rmi $(docker images | grep "^<none>" | awk "print $3")
要删除全部image的话
docker rmi $(docker images -q)
强制删除全部image的话
docker rmi -f $(docker images -q)
参考:《2、CentOS7 Docker 安装以及基本使用,链接:https://blog.csdn.net/weixin_41515615/article/details/81349688》
以上是关于Devops:Docker常用命令的主要内容,如果未能解决你的问题,请参考以下文章