生成docker镜像
一.docker镜像
1.基于运行的容器生成镜像
配置路由
[root@centos ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
检查
[root@centos ~]# sysctl -p
(1)通过镜像运行容器
[root@centos ~]# docker run -it --name httpd centos07:v1.0 /bin/bash
[root@centos ~]# docker start httpd
(2)在运行容器中安装apache服务
[root@centos ~]# docker exec -it httpd /bin/bash
[root@8474099aab9b /]# yum -y install httpd
[root@a57754a463d6 /]# echo “www.benet.com” > /var/www/html/index.html
启动服务
[root@a57754a463d6 /]# httpd
(3)通过容器生成镜像
[root@centos ~]# docker commit -m “apache” -a “bob” httpd httpd:v1.0
(4)查看生成的镜像
[root@centos ~]# docker images
(5)通过生成的镜像运行容器暴漏端口
[root@centos ~]# docker run -it -d --name apache -p 192.168.100.10:80:80/tcp httpd:v1.0
查看
[root@centos ~]# docker ps
[root@a57754a463d6 /]# yum -y install elinks
启动服务
[root@centos ~]# docker exec -it apache /bin/bash
[root@c0e9af3a847e /]# httpd
[root@a57754a463d6 /]# elinks http://192.168.100.10
二.Dockerfile
1.使用dockerfile配置安装tomcat
(1)创建dockerfile
[root@centos ~]# mkdir tomcat
(2)创建dockerfile(Linux光盘)
[root@centos ~]# cp /mnt/apache-tomcat-7.0.54.tar.gz ./tomcat/
[root@centos ~]# cp /mnt/jdk-7u65-linux-x64.gz ./tomcat/
[root@centos ~]# vim ./tomcat/Dockerfile
from centos07:v1.0
MAINTAINER 2037677202:@qq.com
ADD apache-tomcat-7.0.54.tar.gz /usr/src
ADD jdk-7u65-linux-x64.gz /usr/src
ENV JAVA_HOME /usr/src/jdk1.7.0_65
ENV PATH PATH
EXPOSE 8080
ENTRYPOINT /usr/src/apache-tomcat-7.0.54/bin/startup.sh && tailf /usr/src/apache-tomcat-7.0.54/logs/catalina.out
(3)使用dockerfile生成镜像
[root@centos tomcat]# docker build -t tomcat:v1.0 .
(4)查看镜像
[root@centos tomcat]# docker images
(5)通过镜像运行服务
[root@centos ~]# docker run -it -d --name tomcat -p 192.168.100.10:8080:8080/tcp tomcat:v1.0
2.使用dockerfile安装apache
(1)创建apache目录
[root@centos ~]# mkdir apache
(2)创建dockerfile
[root@centos ~]# vim ./apache/Dockerfile
from centos07:v1.0
MAINTAINER 2037677202:@qq.com
RUN rm -rf /etc/yum.repos.d/Cent* && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && yum clean all && yum makecache
RUN yum -y install httpd && echo “www.benet.com” >/var/www/html/index.html
CMD [“httpd”,"-DFOREGROUND"]
(3)生成镜像
[root@centos ~]# cd apache/
[root@centos apache]# docker build -t pache:v1.0 .
(4)生成容器
[root@centos ~]# docker run -it -d --name apache -p 192.168.100.10:80:80/tcp pache:v1.0
3.使用dockerfile安装nginx
(1)创建nginx目录
[root@centos ~]# mkdir nginx
(2)创建nginx的dockerfile
[root@centos ~]# cp /mnt/nginx-1.6.0.tar.gz ./nginx/
[root@centos ~]# vim ./nginx/Dockerfile
FROM centos07:v1.0
MAINTAINER 821409986@qq.com
RUN rm -rf /etc/yum.repos.d/Cent* && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.
repo && yum clean all && yum makecacheRUN yum -y install epel-release pcre-devel zlib-devel gcc
ADD nginx-1.6.0.tar.gz /usr/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/src/nginx-1.6.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module && make && make install
RUN echo “daemon off;” >> /usr/local/nginx/conf/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD [“nginx”]
(3)生成镜像
[root@centos ~]# cd nginx/
[root@centos nginx]# docker build -t nginx:v1.0 .
(4)生成容器
[root@centos ~]# docker run -it -d --name nginx -p 192.168.100.10:8081:80 nginx:v1.0
4.使用dockerfile安装mysql
(1)创建mysql目录
[root@centos ~]# mkdir mysql
(2)创建mysql的dockerfile
[root@centos ~]# cp /mnt/mysql-5.5.22.tar.gz ./mysql/
[root@centos mysql]# vim Dockerfile
FROM centos07:v1.0
MAINTAINER 821409986@qq.com
RUN rm -rf /etc/yum.repos.d/Cent* && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.
repo && yum clean all && yum makecacheRUN yum -y install ncurses-devel libstdc+±devel gcc-c++ cmake && groupadd mysql && useradd -M -s /sbin/nologin -g mysq
l mysql
ADD mysql-5.5.22.tar.gz /usr/src
WORKDIR /usr/src/mysql-5.5.22
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EX
TRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make installRUN cp support-files/my-medium.cnf /etc/my.cnf && cp support-files/mysql.server /etc/init.d/mysqld && chmod +x /etc/in
it.d/mysqld && chkconfig --add mysqld && chkconfig --level 35 mysqld onRUN ln -s /usr/local/mysql/bin/* /usr/local/bin/
RUN /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
ADD mysql.sh /usr/local/mysql/
RUN chmod +x /usr/local/mysql/mysql.sh
EXPOSE 3306
CMD “/usr/local/mysql/mysql.sh” && tail -f /dev/null
(3)编写启动mysql服务脚本
[root@centos ~]# vim ./mysql/mysql.sh
#!/bin/bash
/etc/init.d/mysqld start
(4)生成mysql镜像
[root@centos ~]# cd mysql/
[root@centos mysql]# docker build -t mysql:v1.0 .
(5)生成容器
[root@centos ~]# docker run -it -d -p 192.168.100.10:3306:3306/tcp --name mysql mysql:v1.0
[root@centos ~]# docker exec -it mysql /bin/bash
[root@3d7bdde4aff6 mysql-5.5.22]# mysql -uroot -p
授权账户
mysql> grant all on . to ‘bob’@‘192.168.100.%’ identified by ‘pwd@123’;
客户端验证:
安装mariadb
[root@centos ~]# yum -y install mariadb
[root@centos ~]# mysql -ubob -ppwd@123 -h 192.168.100.10 -P3306
MySQL [(none)]> create database benet;
MySQL [(none)]> show databases;
服务器查看:
mysql> show databases;
三.docker网络管理
1.docker网络使用
(1)使用bridge模式通信
[root@centos ~]# docker run -it -d --net=bridge --name centos01 httpd:v1.0
登录进容器
[root@centos ~]# docker exec -it centos01 /bin/bash
(2)配置none不需要容器连接网络
[root@centos ~]# docker run -it -d --net=none --name centos02 httpd:v1.0
登录进容器
[root@centos ~]# docker exec -it centos02 /bin/bash
(3)容器网络和宿主机保持同步
[root@centos ~]# docker run -it -d --name apache --net=host pache:v1.0
[root@centos ~]# elinks http://192.168.100.10
进入容器
[root@centos ~]# docker exec -it apache /bin/bash
2.修改docker0网桥信息
(1)停止docker服务
[root@centos ~]# systemctl stop docker
(2)停止docker0网桥
[root@centos ~]# ip link set dev docker0 down
(3)删除docker0网桥
[root@centos ~]# brctl delbr docker0
(4)创建新的docker0网桥
[root@centos ~]# brctl addbr docker0
(5)新网桥设置Ip地址
[root@centos ~]# ip addr add 192.168.10.1/24 dev docker0
(6)启动docker0网桥
[root@centos ~]# ip link set dev docker0 up
(7)修改docker配置文件加载docker0网桥
[root@centos ~]# vim /etc/docker/daemon.json
{
“bip”:“192.168.10.1/24”
}
(8)重新启动服务
[root@centos ~]# systemctl daemon-reload
[root@centos ~]# systemctl start docker
查看
[root@centos ~]# docker run -it -d --name centos03 pache:v1.0
[root@centos ~]# docker exec -it centos03 /bin/bash
3.docker网络隔离
(1)手动创建网络隔离空间名字
[root@centos ~]# docker network create -d bridge lamp
(2)创建容器连接到lamp网络名称空间
[root@centos ~]# docker run -it -d --name centos04 --network=lamp pache:v1.0
[root@centos ~]# docker run -it -d --name centos05 --network=lamp pache:v1.0
登录容器查看IP
[root@centos ~]# docker exec -it centos04 /bin/bash
[root@centos ~]# docker exec -it centos05 /bin/bash
ping测试
4.容器跨docker宿主机通信
新开一台docker
[root@centos ~]# cd /etc/yum.repos.d/
[root@centos yum.repos.d]# rm -rf CentOS-*
[root@centos yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos yum.repos.d]# yum clean all
[root@centos yum.repos.d]# yum makecache
[root@centos yum.repos.d]# yum -y install docker
(1)将宿主机网卡桥接到br0网桥上
[root@centos ~]# cd /etc/sysconfig/network-scripts/
[root@centos network-scripts]# vim ifcfg-ens33
BRIDGE=br0
(2)生成br0网桥
[root@centos ~]# cd /etc/sysconfig/network-scripts/
[root@centos network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@centos network-scripts]# vim ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
ONBOOT=yes
NAME=br0
DEVICE=br0
IPADDR=192.168.100.10
NETMASK=255.255.255.0
(3)重新启动网卡
[root@centos ~]# systemctl restart network
(4)git下载网络管理工具
[root@centos ~]# git clone https://github.com/jpetazzo/pipework.git
[root@centos ~]# cp pipework/pipework /usr/local/bin/
[root@centos ~]# chmod +x /usr/local/bin/pipework
(5)运行容器
[root@centos ~]# docker run -it -d --name centos6 --net=none pache:v1.0
(6)运行容器配置IP地址信息
[root@centos ~]# pipwork bro centos6 192.168.100.30/24
Centos02: