文章目录
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.4.2-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out guides at https://docs.docker.com/go/guides/
Docker简介
Docker是一个开源的应用容器引擎,让开发者可以打包应用及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上。使用Docker可以更方便地打包、测试以及部署应用程序。
Docker环境安装
https://blog.csdn.net/BThinker/article/details/123358697
- 安装yum-utils 依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
- 为yum源添加docker仓库位置
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@VM-0-15-centos ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
- 安装docker服务
yum install docker-ce
- 启动docker服务
systemctl start docker
systemctl restart docker
systemctl top docker
[root@VM-0-15-centos ~]# systemctl start docker
[root@VM-0-15-centos ~]# ps -ef|grep docker
root 14515 1 4 20:36 ? 00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 14647 9565 0 20:36 pts/1 00:00:00 grep --color=auto docker
Docker镜像常用命令
查看docker服务状态
systemctl status docker
查看docker基本信息
docker version查看docker版本
docker info查看docker详细信息
docker --help查看docker命令
[root@VM-0-15-centos ~]# docker -v
Docker version 20.10.0, build 7287ab3
搜索镜像
从Docker Hub上查找nginx镜像
docker search nginx
docker search -s 30 nginx从Docker Hub上查找关注度大于30的nginx镜像
[root@VM-0-15-centos ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14133 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1921 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 796 [OK]
查看镜像版本
由于docker search命令只能查找出是否有该镜像,不能找到该镜像支持的版本,所以我们需要通过Docker Hub来搜索支持的版本。
- 进入Docker Hub的官网,地址:https://hub.docker.com
然后搜索需要的镜像:
下载镜像
docker pull nginx:1.17.0
- 下载最新版本
docker pull nginx 等价于 docker pull nginx:latest
- 指定版本号下载
[root@VM-0-15-centos ~]# docker pull nginx:1.17.0
1.17.0: Pulling from library/nginx
fc7181108d40: Pull complete
c4277fc40ec2: Pull complete
780053e98559: Pull complete
Digest: sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
Status: Downloaded newer image for nginx:1.17.0
docker.io/library/nginx:1.17.0
列出镜像
docker images -a列出本地所有的镜像
docker images -p只显示镜像ID
docker images --digests显示镜像的摘要信息
docker images --no-trunc显示完整的镜像信息
PEPOSITORY:镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一个仓库源可以有多个TAG,表示这个仓库源的不同版本,我们使用REPOSITORY:TAG来定义不同的镜像。如果不指定一个镜像的版本标签,例如只使用tomcat,docker将默认使用tomcat:latest镜像
[root@VM-0-15-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.17.0 719cd2e3ed04 18 months ago 109MB
删除镜像
- 指定名称删除镜像:
docker rmi nginx:1.17.0
[root@VM-0-15-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.17.0 719cd2e3ed04 18 months ago 109MB
[root@VM-0-15-centos ~]# docker rmi nginx:1.17.0
Untagged: nginx:1.17.0
Untagged: nginx@sha256:bdbf36b7f1f77ffe7bd2a32e59235dff6ecf131e3b6b5b96061c652f30685f3a
Deleted: sha256:719cd2e3ed04781b11ed372ec8d712fac66d5b60a6fb6190bf76b7d18cb50105
Deleted: sha256:e9b6506fb887de50972aefd99d7c5eb56b1a8e757ed953cdfecb86b5359bcb22
Deleted: sha256:55d9d9692a9615a28d183a42bc3881a72a39393feba3664e669e7affb78daa76
Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e
[root@VM-0-15-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
- 指定名称删除镜像(强制)
docker rmi -f nginx:1.17.0
- 删除所有没有引用的镜像:
docker rmi `docker images | grep none | awk '{print $3}'`
- 强制删除所有镜像:
docker rmi -f $(docker images)
列出容器
docker ps 列出当前所有正在运行的容器
docker ps -f status=exited 查看停止的容器
docker ps -a 列出所有的容器
docker ps -l 列出最近创建的容器
docker ps -n 3列出最近创建的3个容器
docker ps -q 只显示容器ID
docker ps --no-trunc 显示当前所有正在运行的容器完整信息
停止容器
docker stop nginx
#或者
docker stop c5f5d5125587
强制停止容器
docker kill nginx
启停容器
docker start 容器ID或容器名称启动容器
docker restart 容器ID或容器名称重新启动容器
docker stop 容器ID或容器名称停止容器
docker kill 容器ID或容器名称强制停止容器
创建容器
docker create --name nginx-test07 nginx:1.17.0 // (不常用)
创建并启动容器
在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以过 -d 指定容器的运行模式。
[root@VM-0-15-centos ~]# docker run -d --name nginx-test05 nginx:1.17.0
56cf467f28f1a33869e8f3509d1caae5abd2a5bdae1d555c6ce0f156b045a4c8
[root@VM-0-15-centos ~]# docker run -d -p 8070:80 --name nginx-test06 nginx:1.17.0
dbf432726f3ad1b8c1bf60e68770af2ca6a90a92755fc8272f99eaca401d700f
[root@VM-0-15-centos ~]# docker run --name nginx-test -p 8080:80 -d nginx
3fb0b956db8f92c7bb5d4153b6cebab0ae0e43287b6dae0164eae6cfe6ddd8fa
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fb0b956db8f nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp nginx-test
-i:以交互模式运行容器,通常与-t同时使用
-t:为容器重新分配一个伪输入终端,通常与-i同时使用
-P:随机端口映射
–name nginx-test:容器名称。
-p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
-d nginx: 后台运行容器,并返回容器ID,也即启动守护式容器
-e:设置容器的环境变量,设置时区等;
-v:将宿主机上的文件挂载到宿主机上,格式为:宿主机文件目录:容器文件目录;
进入容器
- 先查询出容器的pid:
docker inspect --format "{{.State.Pid}}" nginx-test
[root@VM-0-15-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fb0b956db8f nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:8080->80/tcp nginx-test
[root@VM-0-15-centos ~]# ps -ef|grep nginx
root 3050 32003 0 22:18 pts/2 00:00:00 grep --color=auto nginx
root 32555 32535 0 22:03 ? 00:00:00 nginx: master process nginx -g daemon off;
101 32615 32555 0 22:03 ? 00:00:00 nginx: worker process
[root@VM-0-15-centos ~]# docker inspect --format "{{.State.Pid}}" nginx-test
32555
- 根据容器的pid进入容器:
[root@VM-0-15-centos ~]# nsenter --target "32555" --mount --uts --ipc --net --pid
mesg: ttyname failed: No such device
root@3fb0b956db8f:/#
- 常用进入容器命令
[root@localhost ~]# docker exec -it test2 /bin/bash
[root@localhost ~]# docker attach test2
区别:
exec进入的方式需要添加-i,-t选项,后面还需要给容器一个shell环境,但attach就不需要这么麻烦
exec进入的方式: 通过exit可以退出容器终端,但不会停止容器的运行, 如果exit退出,容器仍然保持运行
attach进入的方式:通过exit可以退出容器终端并停止容器的运行 ,如果执行exit退出,容器会被关闭,如果想要保持容器不被关闭,可以使用键盘:ctrl+p ctrl+q可以实现
本质上区别:
exec进入的方法,会产生新的进程
attach进入的方法,不会产生新的进程
退出容器
exit 退出并停止容器
Ctrl+p+q 只退出容器,不停止容器
查看容器的日志
docker logs -f -t --since --tail 容器ID或容器名称查看容器日志
如:docker logs -f -t --since=”2018-09-10” --tail=10 f9e29e8455a5
-f : 查看实时日志
-t : 查看日志产生的日期
--since : 此参数指定了输出日志开始日期,即只输出指定日期之后的日志
--tail=10 : 查看最后的10条日志
- 查看容器产生的全部日志:
docker logs nginx-test
[root@VM-0-15-centos ~]# docker logs nginx-test
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
83.97.20.30 - - [12/Dec/2020:14:05:15 +0000] "GET / HTTP/1.0" 200 612 "-" "-" "-"
124.64.19.225 - - [12/Dec/2020:14:10:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
- 动态查看容器产生的日志:
docker logs -f nginx-test
docker logs -f -t --since='2020-01-01' --tail=10 nginx-test
docker logs -f -t --since='2020-01-01' --tail=10 3fb0b956db8f
[root@VM-0-15-centos ~]# docker logs -f nginx-test
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
查看容器内部细节
docker inspect nginx-test07
docker inspect dbf432726f3a
删除容器
- 删除指定容器:
docker rm $ContainerName
- 按名称通配符删除容器,比如删除以名称mall-开头的容器:
docker rm `docker ps -a | grep nginx-* | awk '{print $1}'`
- 强制删除所有容器;
docker rm -f $(docker ps -a -q)
- 删除所有停止状态的容器
docker container prune
查看docker 资源网络磁盘cpu等状态
[root@VM-0-15-centos ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 132.9MB 0B (0%)
Containers 1 1 1.191kB 0B (0%)
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
[root@VM-0-15-centos ~]# docker stats -a
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
3fb0b956db8f nginx-test 0.00% 5.312MiB / 1.796GiB 0.29% 10.8kB / 10.8kB 8.02MB / 8.19kB 2
[root@VM-0-15-centos ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
83d3cbc127de bridge bridge local
f0c1524a1b8f host host local
6db84b6f1e83 none null local
docker 安装nginx
https://blog.csdn.net/BThinker/article/details/123507820
参考:
https://blog.csdn.net/lizhiqiang1217/article/details/89070075
https://blog.csdn.net/QianLiStudent/article/details/107021449
https://blog.csdn.net/weixin_40533111/article/details/84847861
https://blog.opskumu.com/docker.html
https://www.runoob.com/docker/docker-container-usage.html
https://blog.csdn.net/leilei1366615/article/details/106267225