简单的Docker案例
- 刚安装Docker,启动容器时,可能会提示你没有开启IPV4转发功能,如图:
WARNING: IPv4 forwarding is disabled. Networking will not work.
不开启的话,容器内部是无法与另一个容器通信的,此时你需要修改配置文件开启这个功能:
- 临时开启:
- 永久开启:
docker run -d -p 18888:80 --restart=always --name=httpd docker.io/httpd:2.4
- 只要docker服务启动,那么这个容器就会启动
docker run -d -p 18888:80 -v /httpd/html:/usr/local/apache2/htdocs/ --name=httpd docker.io/httpd:2.4
- 把容器的目录映射到本机目录上,如果本机目录挂载一个卷,可实现永久存储
- -v 左边是本机目录(不存在会自动创建),右边是容器目录
docker cp httpd:/usr/local/apache2/conf/httpd.conf /tmp/
- 把容器里面的文件拷贝到本机,反过来也可以,本机目录不存在需要手动创建
docker run -d -p 667:80 -v /httpd/html:/usr/local/apache2/htdocs/ --name=webnginx docker.io/nginx:1.12
- 运行nginx服务
- httpd镜像的index.html文件路径:/usr/local/apache2/htdocs/index.html
- nginx镜像的index.html文件路径:/usr/share/nginx/html/index.html
docker run -d -p 33060:3306 -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_USER=xia -e MYSQL_PASSWORD=xia -v /xiamysql:/var/lib/mysql --name=mysql docker.io/mysql:5.5
- 运行mysql服务,第一个-e时设置mysql数据库管理员(默认是root)的密码,第二个和第三个时设置普通用户的用户名密码,-v是目录映射
- 然后在本机上安装mysql,用命令连上去mysql -h192.168.242.128 -P33060 -uroot -predhat
- -h本机IP -P端口 -u用户名 -p密码
- 如果MySQL版本较高,可能会报错
解决办法:此时进入mysql容器内部:
[root@node2 tmp]# docker exec -it mysql bash
进入MySQL数据库:
root@55347cf5e701:/# mysql -uroot -predhat
修改root密码:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'redhat'(BY后面的引号里为密码)
此时再连接即可连上:
[root@node2 ~]# mysql -h192.168.242.132 -P33060 -uroot -predhat
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 13
- 也可以用刚刚创建的普通用户和密码登陆
- 映射后可看见
docker run -it --name=centos --link=mysql docker.io/centos:6.9
容器互连,centos容器调用mysql
- 可看到
[root@3f1ec73c9958 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 mysql 55347cf5e701
172.17.0.2 3f1ec73c9958
容器IP 容器名 容器ID
- 安装MySQL,连接数据库:[root@3f1ec73c9958 /]# mysql -h192.168.242.132 -P33060 -uroot -predhat
- 我们也可以创建新的容器网络进行容器互连
[root@node2 ~]# docker network create -d bridge test-net
5fb06c8746e1af23205baed0036b695a3168fe0413c76dfd546ff57d5ee7ca35
-d:指定Docker网络类型,有bridge、overlay
查看容器网络
[root@node2 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c3339ce6af52 bridge bridge local
65fd27fc68c4 host host local
229c435c6fd1 none null local
5fb06c8746e1 test-net bridge local
运行一个容器test1
[root@node2 ~]# docker run -itd --name test1 --network test-net centos bash
528743b381051f247057acf9de780524779aa60094825c4c105e194d3a3d0c56
运行一个容器test2
[root@node2 ~]# docker run -itd --name test2 --network test-net centos bash
f610f6215ae5cd791deafc22516c2579c5c1bee530ae891fd84b669c2beb9f1d
[root@node2 ~]# docker exec -it test1 bash
[root@528743b38105 /]# ping test2
PING test2 (172.18.0.3) 56(84) bytes of data.
64 bytes from test2.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from test2.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from test2.test-net (172.18.0.3): icmp_seq=3 ttl=64 time=0.086 ms
64 bytes from test2.test-net (172.18.0.3): icmp_seq=4 ttl=64 time=0.158 ms
docker inspect mysql | grep IP #可查看容器IP
- 生产中一般是做一个本地镜像仓库,客户端的docker指向这个仓库,从这个仓库下载镜像
首先修改每个主机的hosts表,Master节点也当作registry
然后下载一些常用的镜像
tag打标签:把docker.io/debian:8.10打上registry:5000/qinimages/debian:8.10标签
docker push registry:5000/qinimages/debian
vim /etc/docker/daemon.json
说明:镜像地址,安全凭证,有了安全凭证,其他主机才能从本机拉取镜像
systemctl restart docker
然后再push,见图片
然后在别的主机上也写/etc/docker/daemon.json,内容相同
其他主机可直接获取registry里面的镜像