Docker容器运行、使用、管理
docker container [COMMAND]
命令:
**exec 在容器中执行命令**
export 将容器的文件系统导出为tar归档文件(和docker save的区别在于,save会记录有所有修改,体制较大,export只是当时overlay层)
kill 杀死一个或多个正在运行的容器
** logs 查看容器内应用的运行日志**
-f 实时追踪日志
** ls 列出所有容器(可以用docker ps代替,相同的命令)**
rename 重命名容器
【容器启动、删除】
rm 移除一个或多个已经停止运行的容器
prune 删除所有已经停止运行的容器
start 启动一个或多个已停止的容器
restart 重新启动一个或多个容器
stop 停止一个或多个正在运行的容器
pause 暂停一个或多个容器中的所有进程
【容器资源状态获取】
stats 显示容器资源使用情况
--no-stream 非实时流,用于执行后马上返回,然后可以写脚本获取容器的资源情况。
top 显示指定容器的进程的使用率
port 列出容器的端口映射或特定映射
docker exec -it mynginx bash
docker port mynginx
docker stop mynginx
docker rm mynginx
启动容器docker run
Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
**# 后台运行容器并打印容器的id**
-d, --detach Run container in background and print container ID
**# 非交换式,通常和-t一起使用**
-i, --interactive Keep STDIN open even if not attached
**# 分配一个伪TTY**
-t, --tty Allocate a pseudo-TTY
**# 设定容器的主机名(注意是主机名hostname,不是容器的名字)**
-h, --hostname
**# 指定容器的IP地址**
--ip string IPv4 address (e.g., 172.30.100.104)
**# 将容器连接到指定的网络模式(默认有brigde、host、container、none,也可以自己创建网络模式)**
--network network Connect a container to a network
**# 设置容器的名称,如果不设置则随机一个名称,建议设置,方便管理**
--name string
**# 发布容器的端口到宿主机中,也就是端口映射。****格式:宿主机端口:容器内需要暴露的应用端口,比如你nginx内部是80端口,但是你想将访问宿主机的888映射到容器内部的80,那就是888:80**
-p, --publish list Publish a container's port(s) to the host
**# 发布容器中exposed指定的暴露端口到宿主机中,但是宿主机的映射端口为随机的。**
-P, --publish-all Publish all exposed ports to random ports
**# 设置容器退出时的重启策略,默认为no,可选项有:[always|on-failure]**
# always在你docker重启后自动重启容器。
--restart string Restart policy to apply when a container exits
(default "no")
**# 挂载宿主机的某个目录到容器中,****<宿主机路径>:<容器内路径>**** **** **
-v, --volume list Bind mount a volume
**# 设置环境变量给容器使用。容器中的应用可以根据变量实现不同场景的应用,比如java的spring boot指定配置文件。**
-e, --env list
**##### 资源限制类常用选项:#####**
**# 限制CPU核数**
--cpus decimal Number of CPUs
**# 限制内存,如1G,500M**
-m, --memory bytes Memory limit
示例:以后台运行的形式,运行一个nginx,容器名设置为mynginx,使用-p手动指定对外暴露的端口和容器内应用端口的关系。
docker run -d nginx --name mynginx -p 8888:80 # 80是容器内的应用端口,8888是外部访问的端口
# 访问应用
curl 宿主机ip:8888
列出容器
docker ps 等价于此命令。
docker container ls [选项]
-a, --all 显示所有容器,默认情况下执行ls只会显示正在运行的容器
-n, --last int Show n last created containers (includes all states) (default -1) 显示最近N个新创建的容器(包括所有状态)
-l, --latest 显示最新创建的那个容器的状态(包括所有状态)
--no-trunc Don't truncate output
-q, --quiet 显示所有容器的id,通常可以用来获取容器,然后批量操作
-s, --size Display total file sizes
进入容器中执行命令
docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
docker exec -it nginx bash
查看容器内应用日志
docker logs [-f] 容器ID|容器名
例:
[root@k8s-master ~]# docker logs mynginx
/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: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: 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: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/11/23 03:50:39 [notice] 1#1: using the "epoll" event method
2022/11/23 03:50:39 [notice] 1#1: nginx/1.21.5
2022/11/23 03:50:39 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/11/23 03:50:39 [notice] 1#1: OS: Linux 3.10.0-1127.el7.x86_64
2022/11/23 03:50:39 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/11/23 03:50:39 [notice] 1#1: start worker processes
2022/11/23 03:50:39 [notice] 1#1: start worker process 31
2022/11/23 03:50:39 [notice] 1#1: start worker process 32
2022/11/23 03:50:39 [notice] 1#1: start worker process 33
2022/11/23 03:50:39 [notice] 1#1: start worker process 34
docker top
docker ps -l Show the latest created container (includes all states)
docker ps -a -a是显示所有的容器,默认只显示正在运行的
docker ps -q Only display container IDs。只显示容器的ID,一般可以来做写脚本,for循环读取。
docker container
ls 和docker ps一样的效果
inspect 查看指定容器的底层信息
exec
commit
cp
logs 查看指定容器的日志
port 查看指定容器暴露的端口
top 查看容器的运行状态
stats --no-stream
rm -f
purge 清除那些不运行的容器
stop/start/restart 停止/启动/重启 指定的容器
# 进入容器
docker exec -it 容器 命令
本文来自博客园,作者:蕝戀,转载请注明原文链接:https://www.cnblogs.com/juelian/p/17782012.html