docker镜像创建 2 ——实战(SSHD、LNMP)

网友投稿 270 2022-10-22

docker镜像创建 2 ——实战(SSHD、LNMP)

实验1 构建ssh镜像容器

#创建sshd的工作目录 mkdir /opt/sshd cd /opt/sshd vim Dockerfile ............................................. #第一行必须指明基于的基础镜像 FROM centos:7 #作者信息. MAINTAINER this is ssh image #镜像的操作指令 RUN yum -y update RUN yum -y install openssh* net-tools lsof telnet passwd RUN echo ' 123456' | passwd --stdin root RUN sed -i ' s/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config #不使用PAM认证 RUN sed -ri ' /^session\s+required\s+pam loginuid.so/ s/^/#/' /etc/pam.d/sshd #取消pam限制 RUN ssh- keygen -t rsa -A #生成密钥认证文件 RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh EXPOSE 22 CMD ["/usr/sbin/sshd" ,"-D"] #/usr/sbin/sshd -D用于前台启动sshd服务 .............................................................. //生成镜像,千万注意"."不要忘记了!不是多打的字符 #记得在工作目录下生成镜像哦! docker build -t sshd:centos . //启动容器并修改root密码 docker run -d -P sshd: centos docker ps -a ssh localhost -p (然后输入密码123456;我dockerfile里面设置得就行) #第一次有警告,记得yes

构建完成sshd的容器后,下面再容器中构建sshd的Systemctl镜像来查看信息状态

#创建systemctl mkdir /opt/systemctl cd /opt/systemctl vim Dockerfile ..................................... FROM sshd:centos MAINTAINER this is systemctl image ENV container docker #除了systemd-tmpfiles-setup.service,删除其它所有文件 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"] //生成镜像 docker build -t systemd:centos . //启动容器,并挂载宿主机目录挂载到容器中,和进行初始化 docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init #--privileged: 使container内的root拥有真正的root权限。 否则,container内的root只是外部的一 个普通用户权限。 docker ps -a //进入容器 docker exec -it f4a367e951b8 bash systemctl status sshd

实验 构建docker容器-LNMP(实战项目)

环境:(docker ip)192.168.100.1 nginx doc01192.168.100.2 mysql doc02192.168.100.3 php doc03默认所有机器关闭防火墙、配好yum

```html/xml首先部署nginx#部署自定义网络,部署nginx(容器IP 为 192.168.100.1#容器ip是容器ip,ens33实体网卡ip是另外一个ip,别搞混了!!!docker network create --subnet=192.168.100.0/24 --opt "com.docker.network.bridge.name"="docker1" mynetwork

#创建工作目录mkdir /opt/nginxcd /opt/nginx/

#上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz、nginx.conf配置文件 到 /opt/nginx/ 目录中

mkdir /opt/nginx/html #创建相应文件给nginx解压tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html

vim Dockerfile.........................................................

FROM centos:7MAINTAINER this is nginx image RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ makeRUN useradd -M -s /sbin/nologin nginxADD nginx-1.12.0.tar.gz /usr/local/src/WORKDIR /usr/local/src/nginx-1.12.0RUN ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-&& make && make installENV 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 CMD nginx -g "daemon off;"

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

#运行容器docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 192.168.100.1 nginx:lnmp

提示:如果没现成配置文件,自己改修改nginx配置文件(与php的docker IP对接)在本机或者其他主机安装nginx,把nginx配置文件移进本机vim nginx.conf...location / {root html;index index.html index.php;}...location ~ .php$ {root html(这行修改!) fastcgi_pass 172.18.0.30:9000; fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;include fastcgi_params;}...

首先是创建自定义网络,然后创建nginx的工作目录,然后切换进去吧nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz、nginx.conf配置文件传进去(nginx.conf自己先配好) ![1.png](https://s2./images/20220309/1646833437184496.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 随后我们创建相应文件给nginx解压,解压文件即可 ![2.png](https://s2./images/20220309/1646833506975415.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 下面是在 /opt/nginx/里创建dockerfile文件 ![3.1.png](https://s2./images/20220309/1646833556144569.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 成功创建镜像文件 ![3.png](https://s2./images/20220309/1646833587310035.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![3.2.png](https://s2./images/20220309/1646833591834327.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 运行镜像,然后就把他运行起来就行 ![4.png](https://s2./images/20220309/1646833608871090.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ```html/xml 下面我们创建mysql-docker02 192.168.100.2 #部署mysql(容器IP 为 192.168.100.2) mkdir /opt/mysqld cd /opt/mysqld 上传 mysql-boost-5.7.20.tar.gz 和mysql.conf到 /opt/mysqld 目录中 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 && 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 ........................................... 下面是进行my.cnf配置文件修改 vim my.cnf ......................................... 3.在当前目录创建mysql配置文件 vim my.cnf [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES .............................................. docker build -t mysql:lnmp . docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 192.168.100.2 mysql:lnmp /usr/sbin/init

```html/xml#部署php(容器IP 为 192.168.100.3)

mkdir /opt/phpcd /opt/php

上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

vim Dockerfile

。。。。。。。。。。。。。。FROM centos:7MAINTAINER 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 nginxADD php-7.1.10.tar.bz2 /usr/local/src/WORKDIR /usr/local/src/php-7.1.10RUN ./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 && make installENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATHADD php.ini /usr/local/php/lib/ADD php-fpm.conf /usr/local/php/etc/ADD conf /usr/local/php/etc/php-fpm.d/EXPOSE 9000CMD /usr/local/php/sbin/php-fpm -F..............................首先是php-fpm.conf修改 pid = php-fpm.conf

然后是vim php.ini修改mysqli.default_socket = /usr/local/ mysqL/mysqL.sock 修改 date. timezone = Asia/Shanghai

最后是 vim conf修改:user= nginxgroup = nginx

linten = 172.18.0.30:9000 (php)

#创建镜像docker build -t php:lnmp .#自定义网络docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 192.168.100.3 php:lnmp

最后一步:进入mysql授权docker exec -it mysql /bin/bash

mysqlcreate database wordpress;grant all privileges on wordpress. to 'wordpress'@'%' identified by '123456';grant all privileges on .* to 'root'@'%' identified by 'abc123';flush privileges;

浏览器访问:http://192.168.206.3/wordpress/index.php

![1.png](https://s2./images/20220310/1646892499795665.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 制作dockerfile文件 ![2.png](https://s2./images/20220310/1646892504767799.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 运行镜像 ![2.1.png](https://s2./images/20220310/1646892519512285.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![3.png](https://s2./images/20220310/1646892529832450.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 运行容器 ![4.png](https://s2./images/20220310/1646892535949912.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 制作mysql数据库 ![5.png](https://s2./images/20220310/1646892546480783.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 刷新数据库 ![5.1.png](https://s2./images/20220310/1646892557661782.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![5.2.png](https://s2./images/20220310/1646892565953237.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) #浏览器访问 http://192.168.206.3/wordpress/index.php 即可

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Docker 技术原理-隔离与限制(30)
下一篇:如何使用jscodeshift来自动修改公开的API接口
相关文章

 发表评论

暂时没有评论,来抢沙发吧~