使用docker镜像搭建svn+Apache环境
2019獨角獸企業重金招聘Python工程師標準>>>
環境準備
虛擬機裝好之后,按照官網步驟檢查虛擬機內核版本,必須在3.10以上版本,故此處安裝redhat_7.2
#?uname?-r 3.10.0-327.el7.x86_64安裝docker:
yum?install?docker-io有依賴是直接安裝具體的依賴軟件,解決依賴
docker安裝成功,啟動docker服務
service?docker?start并設置為開機自啟動
chkconfig?docker?on現在我們需要制作docker鏡像,可以通過Dockerfile或者是在現有的鏡像上修改之后commit。
此處選擇在現有的鏡像基礎上修改后commit并push到私有鏡像庫,以方便后期使用。
以下以Apache+svn(搭建svn環境)為例說明docker 鏡像的制作build、提交commit、上傳push過程?
一、獲取docker基礎鏡像
從docker公有庫down一個適合自己系統的鏡像(我此處down的是centos)
docker?pull?centos運行down下來的docker容器:
docker?run?-it?centos?/bin/bash [root@84292236ae90?/]#進入docker容器,進行svn環境搭建,此處的?84292236ae90 即為你對于centos鏡像修改的ID,提交時只需要提交該ID的內容即可。
二、在鏡像中安裝部署svn+Apache
此處使用yum安裝
yum -y install subversion?mod_dav_svn httpd
安裝成功后,配置初始化svn、用戶,權限(此處對于svn環境的搭建不做詳細的說明)
配置完成使用exit 退出docker容器
三、重啟Apache
為了使得docker容器可以使用宿主機的端口,此處映射宿主機的端口到docker容器
docker -p 參數把虛擬機的80端口映射到容器的80端口;虛擬機的80端口在 Vagrantfile 中被綁定到主機的8080端口,也就是:主機8080->虛擬機80->容器80
sudo docker run -t -i -p 80:80 -v /vagrant/htdocs:/var/www/html custom/httpd /bin/bash
# 啟動 Apache
apachectl -k start?
[root@bogon?svn_apache]#?sudo?docker?run?-t?-i?-p?80:80?-v?/vagrant/htdocs:/var/www/html?test?/bin/bash [root@84292236ae90?/]#?apachectl?-k?start Passing?arguments?to?httpd?using?apachectl?is?no?longer?supported. You?can?only?start/stop/restart?httpd?using?this?script. If?you?want?to?pass?extra?arguments?to?httpd,?edit?the /etc/sysconfig/httpd?config?file. [root@84292236ae90?/]#?ps?-ef|grep?httpd root?????????18??????1??7?18:44??????????00:00:00?/usr/sbin/httpd?-k?start svn??????????19?????18??0?18:44??????????00:00:00?/usr/sbin/httpd?-k?start svn??????????20?????18??0?18:44??????????00:00:00?/usr/sbin/httpd?-k?start svn??????????21?????18??0?18:44??????????00:00:00?/usr/sbin/httpd?-k?start svn??????????22?????18??0?18:44??????????00:00:00?/usr/sbin/httpd?-k?start svn??????????23?????18??0?18:44??????????00:00:00?/usr/sbin/httpd?-k?start root?????????25??????1??0?18:44??????????00:00:00?grep?--color=auto?httpd使用url訪問:
四、提交對鏡像所做的修改
docker?commit?-m?"Added?svn+apache"?-a?"yayad"?84292236ae90?centos-svn提交至本地的centos-svn鏡像,目前只存在于本機器,為了便于其他機器使用,需要提交至公有庫/私有個人庫,根據個人選擇。
此處我提交至個人私有庫:
1.找到本地鏡像的ID:docker images
[root@bogon?opt]#?docker?images REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????VIRTUAL?SIZE firstdocker?????????latest??????????????a3062f931635????????2?hours?ago?????????342.6?MB centos-svn??????????latest??????????????4dea4adb699d????????2?hours?ago?????????396.4?MB yayad/centos_svn????latest??????????????4dea4adb699d????????2?hours?ago?????????396.4?MB centos??????????????latest??????????????bb3d629a7cbc????????10?days?ago?????????196.6?MB2.docker tag <imageID> <namespace>/<image name>:<version tag eg latest>
docker?tag?4dea4adb699d?yayad/centos_svn3.push docker鏡像到官方的個人私有庫
docker?push?yayad/centos_svnpush時會提示輸入庫的賬號、密碼和郵箱,此處需要提前注冊docker.hub
push 成功后在個人私有庫即可看到push的鏡像
此時就可以在其他已經安裝docker環境的機器上執行docker pull centos-svn down該鏡像并直接使用svn環境,無需再安裝配置。
五、讓Apache服務在后臺自動running
但如何在啟動容器的同時自動啟動Apache服務,不用再需要手動啟動,那么我就只需要在宿主機上監控容器的狀態是否running,以下方式即可實現。
1.通過dockerfile 來build
編輯dockerfile
[root@bogon?svn_apache]#?cat?Dockerfile FROM?yayad/centos_svn ENTRYPOINT?apachectl?-k?start?&&?/bin/bashbuild 新image,設置tag為df
[root@bogon?svn_apache]#?docker?build?-t?yayad/centos_svn:df?. Sending?build?context?to?Docker?daemon?2.048?kB Sending?build?context?to?Docker?daemon Step?0?:?FROM?yayad/centos_svn--->?52561e4f9e39 Step?1?:?ENTRYPOINT?apachectl?-k?start?&&?/bin/bash--->?Running?in?30cab1c3a861--->?de5ad506e7dc Removing?intermediate?container?30cab1c3a861 Successfully?built?de5ad506e7dc查看build的image
[root@bogon?svn_apache]#?docker?images REPOSITORY??????????TAG?????????????????IMAGE?ID????????????CREATED?????????????VIRTUAL?SIZE yayad/centos_svn????df??????????????????de5ad506e7dc????????11?seconds?ago??????396.5?MB啟動容器查看配置結果
[root@bogon?svn_apache]#?docker?run?-it?yayad/centos_svn:df?/bin/bash Passing?arguments?to?httpd?using?apachectl?is?no?longer?supported. You?can?only?start/stop/restart?httpd?using?this?script. If?you?want?to?pass?extra?arguments?to?httpd,?edit?the /etc/sysconfig/httpd?config?file. Passing?arguments?to?httpd?using?apachectl?is?no?longer?supported. You?can?only?start/stop/restart?httpd?using?this?script. If?you?want?to?pass?extra?arguments?to?httpd,?edit?the /etc/sysconfig/httpd?config?file. httpd?(pid?9)?already?running [root@1dce83066942?/]#Apache服務已經啟動起來了
2.修改容器的bashrc
這是投機取巧但不失為最簡單的一種辦法,見Run a service automatically in a docker container。
以bash啟動容器:
#docker?run?-it?-p?80:80?-v?/vagrant/htdocs:/var/www/html?yayad/centos_svn?/bin/bash [root@87da9f94dc08?/]#?vim?/etc/bashrc #add?by?dy????添加到最后 apachectl?-k?start若需要可以把修改后的image commit之后使用。
想提及一下的問題:刪除本地一些多余的名稱為NONE的images,報錯,刪除失敗,使用如下的方式解決了,但具體內在聯系還不太清楚
docker?ps?-a?|?grep?"Exited"?|?awk?'{print?$1?}'|xargs?docker?stop docker?ps?-a?|?grep?"Exited"?|?awk?'{print?$1?}'|xargs?docker?rm docker?images|grep?none|awk?'{print?$3?}'|xargs?docker?rmi轉載于:https://my.oschina.net/u/2006667/blog/637882
總結
以上是生活随笔為你收集整理的使用docker镜像搭建svn+Apache环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Activiti Modeler发布以及
- 下一篇: 输入输出流(I/O)