CentOS installation docker, and its basic operation
A. Installation docker
Docker required to run on Centos 7, the system of claim 64, kernel Version 3.10
1. uname -an view the current system version
2. yum -y install docker download and install docker
3. Service docker Start start docker Service
4. docker Version check whether the installation was successful docker
When you see the information in the figure below, is expressed native docker has been successfully installed, very simple
II. Mirroring operation
To create a container operation in mirror-based, so talk about the next docker mirror
Search Mirror
docker images to see whether the unit and have mirrored
Currently the machine has not been mirrored, to Docker Hub Download (Mirror can also be custom, will not elaborate here)
docker search java, but also specify a specific version download,
For example: docker search Ubuntu: 1.2.5.4, can be found docker Hub lists a lot of mirrors
Download image
docker pull docker.io/nginx download
Download to search out the local docker Hub larger than the mirror, because the download process automatically extract, looking at a list of mirrors there just downloaded image
The list contains the warehouse name, version label, image ID, creation time and space occupied
Remove Mirror
Delete unwanted Mirror Mirror id docker rmi
III. Creating and managing mirrored
前边我们已经下载好了Nginx的镜像,接下来我们就创建一个只有Nginx应用的容器docker run -i -t <IMAGE_ID> /bin/bash:-i:标准输入给容器 -t:分配一个虚拟终端 /bin/bash:执行bash脚本,
docker run -idt --name container_nginx -p 8080:80 docker.io/nginx
启动一个使用镜像docker.io/nginx,名字container_nginx的容器,-p 8080:80表示将容器的80端口映射到主机的8080端口,这样我们只要访问主机的8080端口就可以访问到容器的服务了。
注意:name前面是两个-, 端口前面有-p, docker.io/nginx是镜像名,8080是主机的端口,80是Nginx应用的端口
主机上的一个端口只能映射一个容器端口,不可以多个容器端口对应一个主机端口(如果容器安装的centos类的系统,那么容器端口随便设定,但如果容器内只是单纯的应用,那么容器端口要是应用自身的端口)
这样我们就创建并启动了一个容器!
exit 退出容器
docker ps 查看运行中的容器
docker ps -a 查看运行中和非运行中的所有容器
docker exec -it container_nginx /bin/bash 进入容器
如果容器还未启动 执行docker start container_nginx
进入容器后启动Nginx
whereis nginx 找Nginx的启动目录
[root@iz2zehzeir87zi8q99krk1z ~]# docker start container_nginx
container_nginx
[root@iz2zehzeir87zi8q99krk1z ~]# docker exec -it container_nginx /bin/bash
root@84683e425116:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@84683e425116:/# /usr/sbin/nginx
此时在浏览器访问 http://51.110.218.9:8080/ 就可以直接访问容器内的Nginx
如果访问不成功,可能是主机端口的防火墙开着,执行下边的命令关闭
/ sbin / iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
删除容器
容器删除之前先将容器停止
docker stop container_nginx 或者是容器的id
docker rm -f container_nginx 容器删除
docker start 与 docker run 的区别
docker start name 启动一个已经创建的容器
docker run 创建并启动一个容器
docker run 命令其实是 docker create 和 docker start 的命令组合,先执行docker create 创建一个容器 再接着docker start启动
主机和容器文件相互复制
从主机复制到容器 sudo docker cp host_path containerID:container_path
从容器复制到主机 sudo docker cp containerID:container_path host_path
请注意,以上这两个命令都是在主机中执行的,不能再容器中执行
docker cp container_nginx:/usr/local/xin.txt /usr/local/software/ 容器向主机复制文件
docker cp /usr/local/xinzhifu.txt container_nginx:/usr/local/ 主机向容器复制文件
这样一个基础的docker容器就创建完了 。。。。。。。。。。。。
参考资料:https://blog.csdn.net/xinzhifu1/article/details/83579256
宿主机访问docker容器
问题描述:初学Docker,主机Windows 10,虚拟机Cent OS7 运行了一个docker容器(Tomcat7),宿主机无法访问容器运行的服务。
问题原因:宿主机无法访问docker容器ip。
问题解决:1、虚拟机命令:ifconfig
查看docker容器的网段、centos的IP地址:
2、管理员身份打开宿主机powershell:执行命令: ROUTE -p add 172.17.0.1 mask 255.255.0.0 192.168.102.129
Docker容器内部端口映射到外部宿主机端口。
可以ping通容器ip了!
再次在宿主机中访问:http://172.17.0.1:8080/,又见小橘猫 ~^ _ ^~
参考资料:https://www.cnblogs.com/hongkejidan/p/10418835.html
docker容器无法访问宿主机-No route to host
也表现为docker间通过宿主机IP + 宿主机 端口 通信时报错: No route to host
解决方案1 : (推荐) :
在宿主机上将要访问的端口号添加到 public 的zone 区域:
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --reload
解决方案2 : (不推荐) :
直接关闭防火墙,会引起安全问题。以及docker 查新启动报 iptables no chain 之类的错误.
systemctl stop firewalld
注意: 问题貌似只出现在centos上,所以还是推荐ubuntu 貌似毛病少点。