docker镜像创建 2 ——实战(SSHDLNMP)

万事须己运,他得非我贤。这篇文章主要讲述docker镜像创建 2 ——实战(SSHDLNMP)相关的知识,希望能为你提供帮助。
实验1构建ssh镜像容器

#创建sshd的工作目录 mkdir /opt/sshd cd /opt/sshd vim Dockerfile............................................. #第一行必须指明基于的基础镜像 FROM centos:7#作者信息. MAINTAINER this is ssh image < lp> #镜像的操作指令 RUN yum -y update RUN yum -y install openssh* net-tools lsof telnet passwd RUN echo123456 | passwd --stdin root RUN sed -is/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: centosdocker ps -assh localhost -p (然后输入密码123456;我dockerfile里面设置得就行) #第一次有警告,记得yes

创建sshd的工作目录
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

编写dockerfile文件
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

然后在/opt/sshd目录当中创建镜像
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

显示创建镜像成功
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

运行这个镜像用ps -a 检查是否启动
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

使用ssh localhost -p 登录sshd容器
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

构建完成sshd的容器后,下面再容器中构建sshd的Systemctl镜像来查看信息状态
#创建systemctl mkdir /opt/systemctl cd /opt/systemctl vim Dockerfile..................................... FROM sshd:centos MAINTAINER this is systemctl image < zjh> 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 bashsystemctl status sshd

创建工作目录,根据上面的sshd:centos继续堆
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

完成后出来在/opt/systemctl目录中构建镜像
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

启动镜像,查看ps -a 是否正常运行
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

进入到这个镜像当中,使用systemcrl命令来查看sshd的状态,或者使用systemctl命令来操作sshd的状态
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

实验 构建docker容器-LNMP(实战项目)环境:(docker ip)
192.168.100.1 nginxdoc01
192.168.100.2 mysqldoc02
192.168.100.3 phpdoc03
默认所有机器关闭防火墙、配好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/nginx
cd /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:7
MAINTAINER this is nginx image < zjh>
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
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 /
roothtml;
indexindex.html index.php;

...
location ~ .php$
roothtml
(这行修改!)fastcgi_pass172.18.0.30:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;
includefastcgi_params;

...
首先是创建自定义网络,然后创建nginx的工作目录,然后切换进去吧nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz、nginx.conf配置文件传进去(nginx.conf自己先配好) ![1.png](https://s2.51cto.com/images/20220309/1646833437184496.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 随后我们创建相应文件给nginx解压,解压文件即可 ![2.png](https://s2.51cto.com/images/20220309/1646833506975415.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 下面是在 /opt/nginx/里创建dockerfile文件![3.1.png](https://s2.51cto.com/images/20220309/1646833556144569.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 成功创建镜像文件 ![3.png](https://s2.51cto.com/images/20220309/1646833587310035.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![3.2.png](https://s2.51cto.com/images/20220309/1646833591834327.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 运行镜像,然后就把他运行起来就行 ![4.png](https://s2.51cto.com/images/20220309/1646833608871090.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,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 < zjh 2022-03-10> 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 = 1sql_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

首先是创建目录,然后传文件进去
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

vim my.cnf 进去如图所示配置
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

然后是vim Dockerfile,如图所示填写dockerfile文件(上面有文字版)
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

docker build -t mysql:lnmp . (开始创建镜像文件,然后成功)
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

创建自定义网络192.168.100.2
docker镜像创建 2 ——实战(SSHDLNMP)

文章图片

【docker镜像创建 2 ——实战(SSHDLNMP)】```html/xml
#部署php(容器IP 为 192.168.100.3)
mkdir /opt/php
cd /opt/php
上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
vim Dockerfile
。。。。。。。。。。。。。。
FROM centos:7
MAINTAINER this is php image < zjh>
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 & & 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
CMD /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 www.conf
修改:
user= nginx
group = 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
mysql
create 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.51cto.com/images/20220310/1646892499795665.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)制作dockerfile文件 ![2.png](https://s2.51cto.com/images/20220310/1646892504767799.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 运行镜像 ![2.1.png](https://s2.51cto.com/images/20220310/1646892519512285.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![3.png](https://s2.51cto.com/images/20220310/1646892529832450.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)运行容器 ![4.png](https://s2.51cto.com/images/20220310/1646892535949912.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) 制作mysql数据库 ![5.png](https://s2.51cto.com/images/20220310/1646892546480783.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)刷新数据库 ![5.1.png](https://s2.51cto.com/images/20220310/1646892557661782.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=) ![5.2.png](https://s2.51cto.com/images/20220310/1646892565953237.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)#浏览器访问 http://192.168.206.3/wordpress/index.php 即可


    推荐阅读