Docker (5) Practical case

1. Build an SSH image

##开启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

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-6s3IFrv5-1647703370730) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \1.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-RLeySeoa-1647703370731) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \2.bmp)]

2. Build the systemctl image

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

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-GI27i6Ab-1647703370732) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \4.bmp)]

3. Build 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 <zqh>

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 '<h1>this is my dockerfile test web</h1>' > 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/

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-KBjf4rWT-1647703370733) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \5.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-wKxBRtLE-1647703370733) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \6.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-LxJjMs2m-1647703370734) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \7.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-Sv7C3V5y-1647703370735) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \8.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-se2oOozy-1647703370735) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \9.bmp)]

4. Build 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 <zqh>
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/

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-w3wZIrfz-1647703370736) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \10.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-bmNQnnnh-1647703370737) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \11.bmp)]

5. Docker builds LNMP

1. Turn off firewall, file protection, custom network

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. Deploy nginx (the container IP is 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 <zww>
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


[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-8FFUOjlc-1647703370738) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \12.bmp)]

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-q7uC9xfP-1647703370738) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \13.bmp)]

3. Build mysql (the container IP is 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 <zww>
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


[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-18aeloYc-1647703370739) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \14.bmp)]

4. Build php (the container IP is 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 <zww>
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;

[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-ozMpR0o7-1647703370739) (C:\Users\zhuquanhao\Desktop\Screenshot command collection\linux\Docker\Docker case \15.bmp)]

5. Browser access

http://192.168.100.135/wordpress/index.php

insert image description here

Guess you like

Origin blog.csdn.net/weixin_54059979/article/details/123605468