一、容器初体验
[root@localhost ~]# docker container
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
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
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
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
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
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
注:通过以上命令可以查看docker容器支持的所有命令
(1)docker run -it --name="test_vim" 3fe2fe0dab2e /bin/bash #运行容器命令
(2)docker container ls #查看正在运行的容器
1、交互式容器的启动
[root@docker~]# docker run -it --name"testcentos"centos:6.9 /bin/bash #主要是针对于工具类的容器,一旦exit容器,容器就自动关闭
2、守护式容器的启动
(1)[root@docker~]# docker run -d --name "testcentos" centos:6.9
(2).死循环
C -d nginx /bin/sh -c "while true ;do echo hello world; sleep 1;done"
3、容器的应用场景
(1)交互式容器又称为工具类容器,适用于开发、测试、临时性的任务等场景。
[root@docker~]# docker run -it --name"testcentos" centos:6.9 --rm testcentos #退出后自动删除容器
(2)守护式容器 运行网络服务
[root@docker~]# docker run -d -p 8080:80 --name "mynginx" nginx:1.14
4、容器的启动、关闭及连接
(1)[root@docker~]# docker stop "testcentos" #容器的关闭
(2)[docker~]# docker start "testcentos" #启动容器,把容器丢到后台了。
(3)[docker~]# docker start -i "testcentos" #容器的启动后自动进入容器交互环境
注:交互式容器可以使用ctrl+d或exit或docker stop 命令停掉容器
(4)docker attach cestcentos #登录被丢到后台的容器,或正在运行的守护式容器
(5)docker exec -it cestcentos #以子进程的方式登录(在已有工作容器中生成子进程,做登录,退出时也不影响原来容器)
5、容器的后台及前台运行
(1).交互式启动容器Ctrl+p+q将容器丢到后台去
[root@docker ~]# docker run -it --name "testnginx" nginx /bin/bash
加ctrl+p+q
[root@docker ~]# docker attach testnginx
(2).死循环
docker run --name testnginx1 -d nginx /bin/sh -c "while true ;do echo hello world; sleep 1;done"
(3)让程序一直在前台运行(hung在前台)是制作守护式容器时常用的方法。-d选项相当于ctrl+P+Q
如:sshd -D
nginx -g ""
6、容器的网络访问
指定映射(docker 会自动添加一条iptables规则来实现端口映射)
-p hostPort:containerPort
-p ip:hostPort:containerPort #只映射到指定ip的某个端口上
-p ip::containerPort(随机端口32768-60999) #映射到指定ip的随机端口上
-p hostPort:containerPort/udp
-p 81:80 –p 443:443 #多端口映射
随机映射
docker run -P 80(随机端口)可以通过docker container ls -a 看到映射情况
注:cat /etc/os-release命令查看系统的版本号
注:netstat -tulnp|grep 8080 #查看8080端口的状态
注:随机端口从32768-60999之间
7、docker的其他管理
docker ps -a -q -l
docker top testxx
docker inspect
docker attach 容器ID|容器名称(工具类)配合ctrl+p+q
docker exec -i -t 容器ID|容器名称 /bin/bash(服务类),一般是做服务类容器调试用
[root@docker ~]# docker exec -it centos6.9 /bin/bash
[root@oldboy docker]# docker logs testxx
[root@oldboy docker]# docker logs -t testxx
[root@oldboy docker]# docker logs -tf testxx
[root@oldboy docker]# docker logs -tf --tail 10 testxx
[root@oldboy docker]# docker logs -tf --tail 0 testxx