1、创建容器常用选项
1.1、创建容器常用指令
【创建容器常用指令】
【创建容器是限制资源】
1.2、创建容器应用
【运行一个容器】
[root@VM_190_147_centos ~]# docker container run -itd --name bs busybox
#-i表示交互式,-t表示伪终端,-d表示后台运行,--name表示指定一个名字
226ef405c0dd90bad98c5912a673df931e1621a24a3b7a0b1ff1f8812e0037a4
【进入容器】
[root@VM_190_147_centos ~]# docker container attach bs
#
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sh
7 root 0:00 ps -ef
/ # ip addr
【如何进入一个容器退出之后让其继续运行?】
[root@VM_190_147_centos ~]# docker container attach bs
/ # mount
/dev/vda1 on /etc/resolv.conf type ext4 (rw,noatime,data=ordered) #dns解析的文件
/dev/vda1 on /etc/hostname type ext4 (rw,noatime,data=ordered)
/dev/vda1 on /etc/hosts type ext4 (rw,noatime,data=ordered)
注意:每一次容器重启的时候,都需要重新挂载/etc/resolv、/etc/hostname、/etc/hosts三个文件。
【设置环境变量】
[root@VM_190_147_centos ~]# docker container run -it -d -e wzy=wanzhuang --name bs1 busybox
1bb43c06c2d0831c0a6ece44bbb9160df242049c967dd5729c5824f4f4923dd1
[root@VM_190_147_centos ~]# docker exec -it bs1 sh
/ # echo $wzy
wanzhuan
【端口映射】
[root@VM_190_147_centos ~]# docker container run -itd -p 8080:80 --name nginx01 nginx
查看是否能访问
[root@VM_190_147_centos ~]# curl http://localhost:8080
【查看容器日志的输出】
[root@VM_190_147_centos ~]# docker logs nginx01
172.17.0.1 - - [29/Nov/2018:07:15:03 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@VM_190_147_centos ~]# ls /var/lib/docker/containers/18c554b3d5b2c5b460da7fc60796e394401ce8c51de753b1118d4c6dcd19e3ac/
18c554b3d5b2c5b460da7fc60796e394401ce8c51de753b1118d4c6dcd19e3ac-json.log hostconfig.json mounts
checkpoints hostname resolv.conf
config.v2.json hosts resolv.conf.hash
【容器退出时重启策略】
[root@VM_190_147_centos ~]# docker container run -itd -p 8082:80 --restart=always --name nginx03 nginx
18cf7094bcc47df969d26c3a30e99f3bfcd4821330e110fcb9f9e7ee71f50eb3
【限制cpu/memory】
[root@VM_190_147_centos ~]# docker container run --help |grep cpu
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
[root@VM_190_147_centos ~]# docker container run -itd --cpus 1 --name nginx04 nginx
8de8f68f92269db19c98129390a2bc5f2976d53c67514b144391241b1b900dab
[root@VM_190_147_centos ~]# docker container run -itd --memory 256m --name nginx05 nginx
查看资源利用率:docker container stats 容器名称
[root@VM_190_147_centos ~]# docker container stats nginx05
2、管理容器常用指令
2.1、管理容器常用指令
2.2、管理容器应用
【列出容器】
#查看命令
[root@aliyun205 ~]# docker container ls --help
Usage: docker container ls [OPTIONS]
List containers
Aliases:
ls, ps, list
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
#列出所有容器
[root@aliyun205 ~]# docker container ls -a
#列出容器的id
[root@aliyun205 ~]# docker container ls -q
【显示容器的详细信息】
[root@aliyun205 ~]# docker container inspect -s nginx01
#其中-s参数表示显示容器的大小信息
【在容器中执行命令】
[root@aliyun205 ~]# docker container exec --help
Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
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
-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
其中-e参数表示设置环境变量,-u参数设置用户名或uid,-w参数设置容器的工作目录。
[root@aliyun205 ~]# docker container exec -it nginx01 bash
root@9380a317a264:/# ls
root@9380a317a264:/# pwd
/
【把容器提交为一个新镜像】
[root@aliyun205 ~]# docker container commit --help
Usage: docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
其中-a参数表示作者信息,-p表示提交过程中暂停容器运行,-c参数将DoCKFrm指令应用到创建的图像。
[root@aliyun205 ~]# docker container commit -a xiaozhu nginx01 my_nginx
sha256:aa7b5e25adde4ad8de42ac86753593894637655f0c3bd6ee9175cc9e02e27216
#其中ngxin01是已有镜像的名称,my_nginx为新镜像的名称
【把文件拷贝到镜像】
[root@aliyun205 ~]# docker container cp --help
Usage: docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
其中-a表示归档模式并拷贝uid和gid信息,-L表示连接信息
#把当前目录下的war文件拷贝到容器nginx01的root目录下
[root@aliyun205 ~]# docker container cp ./weberp-0.0.1-SNAPSHOT.jar nginx01:/root
#进入nginx01容器
[root@aliyun205 ~]# docker container exec -it nginx01 bash
#查看文件是否拷贝成功
root@9380a317a264:/# ls /root/
weberp-0.0.1-SNAPSHOT.jar
【查看容器的日志信息】
[root@aliyun205 ~]# docker container logs nginx01
【列出容器的所有端口信息】
[root@aliyun205 ~]# docker container port nginx01
80/tcp -> 0.0.0.0:87
【显示容器的资源使用信息】
[root@aliyun205 ~]# docker container stats nginx01 -a
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
9380a317a264 nginx01 0.00% 1.367MiB / 3.702GiB 0.04% 0B / 0B 0B / 0B 2
【显示一个容器运行进程】
[root@aliyun205 ~]# docker container top nginx01
【更新一个容器配置】
[root@aliyun205 ~]# docker container update --help
Usage: docker container update [OPTIONS] CONTAINER [CONTAINER...]
Update configuration of one or more containers
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--restart string Restart policy to apply when a container exits
#设置容器nginx01的cpus为2
[root@aliyun205 ~]# docker container update --cpus 2 nginx01
nginx01
【启动停止容器】
[root@aliyun205 ~]# docker container start nginx01
nginx01
[root@aliyun205 ~]# docker container stop nginx01
nginx01
[root@aliyun205 ~]# docker container restart nginx01
nginx01
【删除一个容器】
[root@aliyun205 ~]# docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
其中-f参数表示强制删除,-v删除卷,-l删除链接
[root@aliyun205 ~]# docker container rm -f nginx02
nginx02