dockerfile-CTF出题笔记(docker学习)

拉取镜像
docker pull ubuntu:16.04
开启一个容器并进入
docker run -it 【镜像id】
更新阿里源

 echo -e "deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse\n">> sources.list

安装vim
apt-get install vim

安装apache2
apt-get install apache2 -y

docker停止所有容器
docker stop $(docker ps -aq)

docekr删除所有容器
docker rm $(docker ps -aq)

启动apache出现这个问题

root@d7096ad8e0a7:/etc/apt# service apache2 restart

 * Restarting Apache httpd web server apache2 AH00558: apache2: Could not reliably determine the     
   server's fully qualified domain name, using 172.17.0.2. 
   Set the 'ServerName' directive globally to suppress this message

在/etc/apache2/apache2.conf中添加
ServerName localhost:80
用curl测试127.0.0.1 apache已经搭建好,先把容器单独提交成镜像以防后边太乱
docker commit 容器名 镜像名
按住ctrl+p然后按住ctrl+q是退出容器 但是容器仍然运行

将镜像打包成文件
docker save -o 要保存的文件名 要保存的镜像

用新的镜像开一个容器配置一下端口映射
-p参数 映射的端口:docker的端口
docker run -it -p 5555:80 镜像id

进入容器
docker exec -it 容器id /bin/bash
注意这里新开的容器apache服务是没有开启的,所以需要开启一下apache
service apache2 restart
然后从物理机访问linux的ip:映射的端口,看会不会出来apache的默认页面

这里想修改一下apache2的根目录
将/etc/apache2/sites-available下的default-ssl.conf中的DocumentRoot指定的根目录改为自己的根目录,因为要写dockerfile所以用sed命令来修改
sed -i 's#/var/www/html#/var/www/const#g' /etc/apache2/sites-available/default-ssl.conf

还有/etc/apache2/sites-enabled/000-default.conf中的DocumentRoot也要修改
sed -i 's#/var/www/html#/var/www/const#g' /etc/apache2/sites-enabled/000-default.conf

然后会发现
image.png
php不解析(…尴尬 是因为没有装php所以没有解析,,但是之前也遇到过php不解析加上这一行就可以了)
在/etc/apache2/apache2.conf中添加一行AddType application/x-httpd-php .php
echo "AddType application/x-httpd-php .php" >>/etc/apache2/apache2.conf

安装装php和php插件

apt-get install php -y
apt-get install libapache2-mod-php -y  --fix-missing
apt-get install php7.0-mysql

image.png
已经成功解析

安装mysql(安装mysql时输入密码的时候直接回车,不设置密码因为后边dockerfile更改密码的时候密码为空才能更改)

apt-get install mysql-server -y
apt-get install mysql-client -y

安装好mysql后想要更改一下mysql读写文件的路径

echo “secure_file_priv=’’” >> /etc/mysql/mysql.conf.d/mysqld.cnf

关闭
service apparmor teardown

然后给想要写入文件的文件夹加mysql权限
chown -R mysql:mysql /var/www/html

如果mysql重启的时候关闭失败 安装的时候显示
dpkg: error processing package mysql-server-5.7 (–configure): subprocess installed post-installation script returned error exit status 1 dpkg: dependency problems prevent configuration of mysql-server: mysql-server depends on mysql-server-5.7; however:

参考网址
https://askubuntu.com/questions/789686/dpkg-error-processing-package-mysql-server-configure
image.png
(真的是百度一小时谷歌十分钟,百度很多都是同一篇。。。。)

dockerfie

FROM ubuntu:16.04

MAINTAINER const

ENV REFRESHED_AT 2020-08-26

ENV LANG C.UTF-8

#更新源
RUN rm /etc/apt/sources.list 
COPY sources.list /etc/apt/sources.list


RUN apt-get update


#防止Apache安装过程中地区的设置出错
ENV DEBIAN_FRONTEND noninteractive


#安装apache2
RUN apt-get install apache2 -y

#安装php
RUN apt-get install php -y
RUN apt-get install libapache2-mod-php -y  --fix-missing
RUN apt-get install php7.0-mysql -y


#安装mysql
RUN apt-get install mysql-server -y
RUN apt-get install mysql-client -y

#配置apache 更改根目录
RUN mkdir /var/www/const
RUN sed -i 's#/var/www/html#/var/www/const#g' /etc/apache2/sites-available/default-ssl.conf
RUN sed -i 's#/var/www/html#/var/www/const#g' /etc/apache2/sites-enabled/000-default.conf
RUN echo "AddType application/x-httpd-php .php" >>/etc/apache2/apache2.conf
RUN echo "ServerName localhost:80" >>/etc/apache2/apache2.conf



#修改mysql文件读写
RUN echo "secure_file_priv=''" >> /etc/mysql/mysql.conf.d/mysqld.cnf
RUN service apparmor teardown
RUN chown -R mysql:mysql /var/www/const
RUN chmod 777 /var/www/const

#将题目源码放进去
COPY  /src/index.php /var/www/const/index.php
COPY /src/1.php /var/www/const/1.php
COPY /src/flag.txt /flag.txt

#将sql文件放进docker
COPY ctf.sql /root/ctf.sql
RUN chmod +x /root/ctf.sql

#因为docker是单个进程的,如果一个进程退出docker就退出,所以需要一个永远不退出的进程,将日志输入到1.txt
RUN touch /var/log/1.txt
RUN chmod +x /var/log/1.txt


#启动脚本
COPY start.sh /root/start.sh
RUN  chmod +x /root/start.sh
ENTRYPOINT cd /root; ./start.sh

#暴露端口
EXPOSE 80

docker-compose.yml

version: '2'
services:
 web:
  build: .
  ports: 
   - "5000:80"

start.sh

#!/bin/bash
#启动mysql
service mysql restart

#保险起见给文件加权限
chown mysql /var/run/mysqld/
chown -R mysql:mysql /var/lib/mysql

#修改密码(前边安装的时候默认是空)
mysqladmin -u root password "password"
#创建ctf数据库
mysql -uroot -ppassword -e "CREATE DATABASE IF NOT EXISTS ctf"
#导入sql文件
mysql -uroot -ppassword ctf < /root/ctf.sql
#启动apache
service apache2 restart
#永不退出的进程
tail -f /var/log/1.txt

有的sh脚本刚开始build的时候会出现找不到sh脚本,是因为编辑sh文件是在win下的,linux中的换行和win的换行不同,可以在linux下编写sh脚本,或者在编辑器中调整输入格式

还有遇到错误是 怎么都访问不了index.php页面
后来阴差阳错的失误忘了把源码放进更改后的跟目录发现访问网站是个文件列表
然后查出来是因为之前在删除源码里边的空格的时候不小心把php代码删了
可以先把index.php改个名字然后从文件目录去访问它 看能访问不能 如果不能那很可能就是php代码的问题

因为是配置好之后写的文章,好多错误不是很记了,如果有问题的话可以评论

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值