构建Docker容器实战
一:案例--构建nginx容器
1:拉取centos镜像或将镜像导入到docker中(如果是在线状态,此步骤可省掉)
[root@localhost ~]# docker pull 192.168.1.149:8000/centos1804
或
[root@localhost ~]# docker load --input centos7.3.tar
d69483a6face: Loading layer 209.5MB/209.5MB
Loaded image: centos:latest
2:创建dockerfile工作目录
[root@localhost ~]# mkdir /opt/nginx
[root@localhost ~]# cd /opt/nginx
3:创建dockerfile
[root@localhost nginx]# vim dockerfile
FROM 192.168.1.149:8000/centos1804
RUN rm -rf /etc/yum.repos.d/*
ADD myrepo.repo /etc/yum.repos.d/myrepo.repo
RUN yum clean all
#RUN yum makecache
RUN yum -y install pcre-devel zlib-devel zlib gcc* make openssl-devel
ADD nginx-1.12.0.tar.gz /opt
WORKDIR /opt/nginx-1.12.0
RUN ./configure --prefix=/usr/local/nginx && make && make install
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
WORKDIR /root/nginx
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]
4:将163的yum源文件拷贝到dockerfile的nginx目录中
5:编写nginx启动脚本
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
6:用dockerfile创建镜像
[root@localhost nginx]# docker build -t mynginx .
7:启动容器
[root@localhost nginx]# docker run -d -i -p 8080:80 mynginx
注意:此处不带-t选项,容器中有一个run.sh的脚本,带上-t选项,这个脚本就不执行了
或者
[root@localhost nginx]# docker run -d -it -p 8080:80 mynginx /bin/bash -c /run.sh
此处使用bash执行run.sh的脚本
或者
[root@localhost nginx]# docker run -dit -p 8081:80 -v /www:/web mynginx
此处为容器创建了一个容器卷,并挂在到docker主机的/www目录下
[root@localhost ~]# mkdir /nginx
[root@localhost ~]# cp /opt/nginx/nginx.conf /nginx
[root@localhost nginx]# docker run -dit -p 8000:80 -v /www:/web -v /opt/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf mylamp /bin/bash -c /run.sh
8:访问nginx网站
http://192.168.10.105:8080
二:案例—构建Tomcat容器
1:创建工作目录
[root@localhost ~]# mkdir /opt/tomcat/
[root@localhost ~]# cd /opt/tomcat/
2:将jdk解压,并移动到工作目录
[root@localhost ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /opt/tomcat/
3:创建dockerfile文件
[root@localhost tomcat]# vim dockerfile
FROM 192.168.1.149:8000/centos1804
ADD jdk1.8.0_91 /usr/local/jdk-8u91
ENV JAVA_HOME /usr/local/jdk-8u91
ENV JAVA_BIN /usr/local/jdk-8u91
ENV JRE_HOME /usr/local/jdk-8u91
ENV PATH $PATH:/usr/local/jdk-8u91/bin:/usr/local/jdk-8u91/jre/bin
ENV CLASSPATH /usr/local/jdk-8u91/jre/bin:/usr/local/jdk-8u91/lib:/usr/local/jdk-8u91/jre/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /
RUN mv /apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]
4:创建启动脚本
[root@localhost tomcat]# vim run.sh
#!/bin/bash
/usr/local/tomcat/bin/startup.sh
tailf /run
5:用dockerfile生成镜像
[root@localhost tomcat]# docker build -t mytomcat .
6:运行容器
[root@localhost tomcat]# docker run -d -i -p 8080:8080 mytomcat
7:访问tomcat网站
http://192.168.10.105:8080
三:案例---构建mysql容器
1:创建工作目录
[root@localhost ~]# mkdir /opt/mysql
2:创建dockerfile文件
[root@localhost ~]# cd /opt/mysql/
[root@localhost mysql]# vim dockerfile
FROM 192.168.1.149:8000/centos1804
RUN rm -rf /etc/yum.repos.d/*
ADD myrepo.repo /etc/yum.repos.d/myrepo.repo
RUN yum clean all
RUN yum -y install mariadb mariadb-server
RUN chown -R mysql:mysql /var/lib/mysql
ADD init.sh /init.sh
RUN chmod 775 /init.sh
RUN /init.sh
EXPOSE 3306
CMD ["mysqld_safe"]
3:编写mysql初始化脚本
[root@localhost mysql]# vim init.sh
#!/bin/bash
mysql_install_db --user=mysql
sleep 3
mysqld_safe &
sleep 3
mysqladmin -u "root" password "123456"
mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';"
mysql -uroot -p123456 -e "flush privileges;"
3:生成镜像
[root@localhost mysql]# docker build -t mysql .
4:创建容器
[root@localhost mysql]# docker run -id -p 3306:3306 mysql
四:构建php
1:创建工作目录
2:创建dockerfile(方法一:源码包安装)
FROM 192.168.1.148:8000/centos1804
MAINTAINER jacker
ENV VERSION=5.5.38
RUN rm -rf /etc/yum.repos.d/*
ADD myrepo.repo /etc/yum.repos.d/myrepo.repo
RUN yum clean all
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel \
libmcrypt-devel libxslt-devel libtidy-devel autoconf \
iproute net-tools telnet wget curl && \
yum clean all && \
rm -rf /var/cache/yum/*
COPY php-${VERSION}.tar.gz /
RUN cd / && \
tar zxf php-${VERSION}.tar.gz && \
mv php-${VERSION} /usr/local/ && \
cd /usr/local/php-${VERSION} && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --enable-opcache \
--with-mysql --with-mysqli --with-pdo-mysql \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-freetype-dir \
--enable-mbstring --with-mcrypt --enable-hash && \
make -j 4 && make install && \
cp php.ini-production /usr/local/php/etc/php.ini && \
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
mkdir /usr/local/php/log && \
cd / && rm -rf php* && \
cd /usr/local/ && rm -rf php-${VERSION} && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin
#COPY php.ini /usr/local/php/etc/
#COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]
2:创建dockerfile(方法二:yum安装安装)
FROM centos
MAINTAINER jacker
RUN rm -rf /etc/yum.repos.d/*
ADD myrepo.repo /etc/yum.repos.d/myrepo.repo
RUN yum clean all
RUN yum install epel-release -y && \
yum install -y php php-fpm php-common php-mysql
EXPOSE 9000
CMD ["php-fpm"]
3:生成镜像
[root@localhost php]# docker build -t myphp .
4:启动容器
[root@localhost php]# docker run -d -i -p 9000:9000 myphp
##############################################################################################
docker run -d --name mysql -v /var/lib/mysql -p 3306:3306 mysql //启动mysql
docker run -d --name php --volumes-from mysql -v /web:/web -p 9000:9000 myphp //启动php
docker run -d --name nginx --link php:php -p 80:80 -v /web/:/web mynginx //启动nginx
mkdir /nginx
cp /opt/nginx/nginx.conf /nginx
docker run -d --name nginx --link php:php -p 80:80 -v /web/:/web -v /nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf mynginx
mkdir /web
docker run -itd --name lnmp_php --network lnmp -p 9000:9000 --mount type=bind,src=/web,dst=/web myphp
docker run -itd --name lnmp_nginx --network lnmp -p 80:80 --mount type=bind,src=/web,dst=/web mynginx /bin/bash