Docker (五)实战案例

文章目录

  • 一.构建SSH镜像
  • 二.构建systemctl 镜像
  • 三.构建nginx
  • 四.构建tomcat
  • 五.Docker构建LNMP

一.构建SSH镜像

##开启ip转发功能
vim /etc/sysctl.conf 

net.ipv4.ip_forward = 1

sysctl -p

systemctl restart docker

1.cd /opt
mkdir sshd

2.vim Dockerfile

FROM centos:7

MAINTAINER this is sshd image <2022-3-9-14:19-zqh>

RUN yum install -y openssh* net-tools lsof telnet passwd

RUN echo 'abc123' | passwd --stdin root

RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config

RUN sed -i -r '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd

RUN ssh-keygen -t rsa -A

RUN mkdir -p /root/.ssh && chown root.root /root && chown 700 /root/.ssh

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]

3.生成镜像
docker build -t sshd:centos .

4.启动容器并修改root密码
docker run -d -P sshd:centos
docker ps -a
ssh localhost -p 49153

二.构建systemctl 镜像

1.  mkdir /opt/systemctl
    cd systemctl/
    vim Dockerfile
    
FROM sshd:centos

MAINTAINER this is systemctl image <2022-3-9-zqh>

ENV container docker

RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;

VOLUME [ "/sys/fs/cgroup" ]

CMD ["/usr/sbin/init"]

2.生成镜像
docker build -t systemctl:centos .

3.启动容器,并挂载宿主机目录挂载到容器中,进行初始化
docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemctl:sshd init

4.登入容器
docker exec -it 84eff8690073 bash

5.启动sshd
systemctl start sshd

Docker (五)实战案例_第1张图片

三.构建nginx

1.mkdir /opt/nginx
cd /opt/nginx/
把nginx-1.12.0.tar.gz安装包移进来

2.vim Dockerfile

FROM centos:7

MAINTAINER this is nginx image 

RUN yum -y update

RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make

RUN useradd -M -s /sbin/nologin nginx

ADD nginx-1.12.0.tar.gz /opt/

WORKDIR /opt/nginx-1.12.0

RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install

ENV PATH /usr/local/nginx/sbin:$PATH

EXPOSE 80
EXPOSE 443
ENTRYPOINT ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

3.创建新镜像
docker build -t nginx:centos .

4.启动容器,并查看网页
docker run -d -P nginx:centos7

5.准备网页文件
cd /opt/nginx/
echo '

this is my dockerfile test web

' > index.html 6.重创建容器并将宿主机目录挂载到容器 docker run -d -P -v /opt/nginx/index.html:/usr/local/nginx/html/index.html nginx:centos7 7.浏览器验证 http://192.168.100.142:49158/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wKxBRtLE-1647703370733)(C:\Users\zhuquanhao\Desktop\截图命令集合\linux\Docker\Docker案例\6.bmp)]

Docker (五)实战案例_第2张图片

Docker (五)实战案例_第3张图片

Docker (五)实战案例_第4张图片

四.构建tomcat

1.mkdir /opt/tomcat
cd /opt/tomcat
把jdk-8u91-linux-x64.tar.gz和apache-tomcat-8.5.16.tar.gz移动到本目录

2.vim Dockerfile

FROM centos:7
MAINTAINER this is tomcat image 
ADD jdk-8u91-linux-x64.tar.gz /usr/local/

ENV JAVA_HOME /usr/local/jdk1.8.0_91
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:${JRE_HOME}/bin:$PATH

ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 tomcat
EXPOSE 8080
EXPOSE 8443

CMD ["/usr/local/tomcat/bin/catalina.sh","run"]

3.创建新镜像
docker build -t tomcat:centos .

4.启动容器
docker run -d -p 8080:8080 tomcat:centos

5.网页访问
http://192.168.100.142:8080/

Docker (五)实战案例_第5张图片

五.Docker构建LNMP

1.关闭防火墙,文件防护,自定义网络

systemctl stop firewalld
setenforce 0
docker pull centos:7

 docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

2.部署nginx(容器IP为172.18.0.10)

mkdir /opt/nginx/html

把nginx-1.12.0.tar.gz  和wordpress-4.9.4-zh_CN.tar.gz ,nginx.conf上传到nginx目录中

1.解压到html目录中
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html/


2.vim Dockerfile

FROM centos:7
MAINTAINER this is nginx image 
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

3.创建镜像
docker build -t nginx:lnmp .

4.启动容器

docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp


Docker (五)实战案例_第6张图片

3.搭建mysql(容器IP为172.18.0.20)

1.mkdir /opt/mysqld
  cd /opt/mysqld
  
2. 把my.cnf 和 mysql-boost-5.7.20.tar.gz都上传到 mysql目录中

3.vim Dockerfile

FROM centos:7

MAINTAINER this is mysql image 
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN systemctl enable mysqld
VOLUME [ "/usr/local/mysql" ]
CMD /usr/sbin/init

4.创建镜像
docker build -t mysql:lnmp .

4.创建容器,并指定ip和端口

docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp /usr/sbin/init


Docker (五)实战案例_第7张图片

4.搭建php(容器IP为172.18.0.30)

1. mkdir /opt/php
   cd /opt/php
   
上传php-7.1.10.tar.bz2  php-fpm.conf  php.ini  www.conf
四个文件到 php目录中

2.vim DOckerfile

FROM centos:7
MAINTAINER this is php image 
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]


3.创建镜像
docker build -t php:lnmp .

4.创建容器,并指定ip和端口
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

5.进去mysql容器
docker exec -it mysql /bin/bash

6.登入 mysql数据库 并且创建数据库
进入mysql,回车。
mysql
创建数据库
create database wordpress;
授予wordpress权限
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'
再给wordpress,root权限
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
刷新
flush privileges;

5.浏览器访问

http://192.168.100.135/wordpress/index.php

你可能感兴趣的:(docker,容器)