(打了無數(shù)遍的docker 也算學(xué)習(xí)了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出現(xiàn)這個問題
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已經(jīng)搭建好谢澈,先把容器單獨提交成鏡像以防后邊太亂
docker commit 容器名 鏡像名
按住ctrl+p然后按住ctrl+q是退出容器 但是容器仍然運行
將鏡像打包成文件
docker save -o 要保存的文件名 要保存的鏡像
用新的鏡像開一個容器配置一下端口映射
-p參數(shù) 映射的端口:docker的端口
docker run -it -p 5555:80 鏡像id
進入容器
docker exec -it 容器id /bin/bash
注意這里新開的容器apache服務(wù)是沒有開啟的,所以需要開啟一下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
然后會發(fā)現(xiàn)
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
已經(jīng)成功解析
安裝mysql(安裝mysql時輸入密碼的時候直接回車,不設(shè)置密碼因為后邊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
關(guān)閉
service apparmor teardown
然后給想要寫入文件的文件夾加mysql權(quán)限
chown -R mysql:mysql /var/www/html
如果mysql重啟的時候關(guān)閉失敗 安裝的時候顯示
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:
參考網(wǎng)址
https://askubuntu.com/questions/789686/dpkg-error-processing-package-mysql-server-configure
(真的是百度一小時谷歌十分鐘,百度很多都是同一篇律秃。。治唤。。)
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安裝過程中地區(qū)的設(shè)置出錯
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
#保險起見給文件加權(quán)限
chown mysql /var/run/mysqld/
chown -R mysql:mysql /var/lib/mysql
#修改密碼(前邊安裝的時候默認是空)
mysqladmin -u root password "password"
#創(chuàng)建ctf數(shù)據(jù)庫
mysql -uroot -ppassword -e "CREATE DATABASE IF NOT EXISTS ctf"
#導(dǎo)入sql文件
mysql -uroot -ppassword ctf < /root/ctf.sql
#啟動apache
service apache2 restart
#永不退出的進程
tail -f /var/log/1.txt
有的sh腳本剛開始build的時候會出現(xiàn)找不到sh腳本柜裸,是因為編輯sh文件是在win下的缕陕,linux中的換行和win的換行不同,可以在linux下編寫sh腳本疙挺,或者在編輯器中調(diào)整輸入格式
還有遇到錯誤是 怎么都訪問不了index.php頁面
后來陰差陽錯的失誤忘了把源碼放進更改后的跟目錄發(fā)現(xiàn)訪問網(wǎng)站是個文件列表
然后查出來是因為之前在刪除源碼里邊的空格的時候不小心把php代碼刪了
可以先把index.php改個名字然后從文件目錄去訪問它 看能訪問不能 如果不能那很可能就是php代碼的問題