docker-wordpress

在Docker中,通常遵循一個Docker只運行一個應用,這樣方便維護。php

首先須要將centos 鏡像pull到本地,並搭建本地yum倉庫

yum倉庫地址:http://192.168.2.11:8000  這裏使用阿里yum和163yum均可以。html

[root@node1 test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              904d6c400333        4 weeks ago 196.7 MB

編寫Dockerfile文件node

複製代碼
[root@node1 web-Dockerfile]# vim Dockerfile 

FROM centos
MAINTAINER hukey
RUN rm -rf /etc/yum.repos.d/* RUN echo -e "[yum]\nname = yum\ngpgcheck = 0\nbaseurl = http://192.168.2.11:8000" > /etc/yum.repos.d/yum.repo RUN yum install httpd php php-mysql php-mbstring -y && yum clean all EXPOSE 80 CMD ["/usr/sbin/httpd","-f","/etc/httpd/conf/httpd.conf","-DFOREGROUND"]
複製代碼

嘗試執行Dockerfile文件mysql

複製代碼
[root@node1 web-Dockerfile]# docker build -f Dockerfile -t web:centos7 .
...
...
Step 6 : EXPOSE 80 ---> Running in cd581ad95fb0 ---> 368d9fc0b1ce Removing intermediate container cd581ad95fb0 Step 7 : CMD /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf -DFOREGROUND ---> Running in 34e0a903d8b5 ---> 07d76f84111c Removing intermediate container 34e0a903d8b5 Successfully built 07d76f84111c
複製代碼

docker每執行完成一行RUN 語句就會生成一個鏡像文件,若是還有RUN指令執行,就會刪除原來的鏡像文件,並建立一個新的鏡像文件,所以在編寫Dockerfile時,要簡練。web

 

經過生成的鏡像文件運行一個容器sql

複製代碼
[root@node1 web-Dockerfile]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
web                 centos7             07d76f84111c        2 minutes ago 268 MB docker.io/centos latest 904d6c400333 4 weeks ago 196.7 MB [root@node1 web-Dockerfile]# docker run -d -p 80:80 -v /myweb/:/var/www/html/ --name web web:centos7   # -v 本機目錄/myweb 掛載到容器內的/var/www/html目錄
複製代碼

經過查看,咱們已經成功的建立了一個web容器。docker

[root@node1 web-Dockerfile]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
7d5c54ba0cf1        web:centos7         "/usr/sbin/httpd -f /" 11 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp web [root@node1 web-Dockerfile]# docker port web 80/tcp -> 0.0.0.0:80

測試訪問:
http://192.168.2.11數據庫

 

web 服務已經搭建完畢,接下來是MySQLvim

編寫Dockerfile文件,CMD運行的mysql.sh腳本,須要咱們手動編寫。這裏開通ssh服務,方便對於數據庫的管理工做centos

複製代碼
[root@node1 mysql-Dockerfile]# vim Dockerfile 

  1 FROM centos 2 MAINTAINER hukey 3 RUN rm -rf /etc/yum.repos.d/* && echo -e "[yum]\nname = yum\ngpgcheck = 0\nbaseurl = http://192.168.2.11:8000" > /etc/yum.repos.d/yum.repo 4 RUN yum install mariadb-server openssh-server -y && yum clean all 5 RUN mysql_install_db && chown -R mysql:mysql /var/lib/mysql/ 6 VOLUME /var/lib/mysql/ 7 ADD mysql.sh /mysql.sh 8 RUN chmod 755 /mysql.sh 9 EXPOSE 22 10 EXPOSE 3306 11 CMD ["/mysql.sh"]
複製代碼

mysql.sh 腳本內容

複製代碼
[root@node1 mysql-Dockerfile]# vim mysql.sh 

  1 #!/bin/bash 2 # Author:hukey 3 mysqld_safe & 4 sleep 5 5 mysqladmin -uroot password '123456' 6 mysql -uroot -p123456 -e "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';FLUSH PRIVILEGES;" 7 sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key 8 echo 123456 | passwd --stdin root 9 /usr/sbin/sshd -D
複製代碼

嘗試生成鏡像

[root@node1 mysql-Dockerfile]# docker build -f Dockerfile -t mariadb:centos7 .
[root@node1 mysql-Dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mariadb centos7 49b88fcfb709 29 seconds ago 367.8 MB

生成完成,嘗試啓動容器

[root@node1 mysql-Dockerfile]# docker run -d -p 20002:22 --name db mariadb:centos7 [root@node1 mysql-Dockerfile]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0988d8c8eb2 mariadb:centos7 "/mysql.sh" 7 seconds ago Up 6 seconds 3306/tcp, 0.0.0.0:20002->22/tcp db

經過ssh服務鏈接到容器

複製代碼
[root@node1 mysql-Dockerfile]# ssh 192.168.2.11 -p 20002 The authenticity of host '[192.168.2.11]:20002 ([192.168.2.11]:20002)' can't be established. RSA key fingerprint is 8c:c0:c0:a5:1e:0b:83:03:48:1e:51:77:25:37:20:bc. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.2.11]:20002' (RSA) to the list of known hosts. root@192.168.2.11's password: [root@c0988d8c8eb2 ~]# mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
複製代碼

如今web容器和mariadb容器已經搭建完畢,接下來就須要將它們關聯在起來

複製代碼
[root@node1 mysql-Dockerfile]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                             NAMES
c0988d8c8eb2        mariadb:centos7     "/mysql.sh" 2 minutes ago Up 2 minutes 3306/tcp, 0.0.0.0:20002->22/tcp db 7d5c54ba0cf1 web:centos7 "/usr/sbin/httpd -f /" 58 minutes ago Up 58 minutes 0.0.0.0:80->80/tcp web [root@node1 mysql-Dockerfile]# docker stop web web [root@node1 mysql-Dockerfile]# docker rm web web
複製代碼

測試是否可以訪問到mysql容器

複製代碼
[root@node1 mysql-Dockerfile]# docker run -it -p 80:80 --link=db --name web web:centos7 /bin/bash [root@f310c805bdfc /]# yum install mariadb -y [root@f310c805bdfc /]# mysql -uroot -p123456 -h db Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
複製代碼

鏈接mysql容器成功。

 

再次建立後臺運行的web容器

[root@node1 web-Dockerfile]# docker run -d -p 80:80 -v /myweb/:/var/www/html/ --link=db:todb --name=web web:centos7 [root@node1 web-Dockerfile]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a36ee3781da web:centos7 "/usr/sbin/httpd -f /" 8 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp web 50dba9d34f51 mariadb:centos7 "/mysql.sh" 9 minutes ago Up 9 minutes 3306/tcp, 0.0.0.0:20002->22/tcp db

安裝wordpress web程序

[root@node1 myweb]# cp -a /root/wordpress/* /myweb/

瀏覽器訪問

登陸數據庫容器,建立wordpress所需數據庫。

複製代碼
[root@node1 myweb]# ssh 192.168.2.11 -p 20002 root@192.168.2.11's password: [root@50dba9d34f51 ~]# mysql -uroot -p123456 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CREATE DATABASE wordpress; Query OK, 1 row affected (0.09 sec) MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'wordpress'@'%' IDENTIFIED BY 'wordpress'; Query OK, 0 rows affected (0.03 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> Bye
複製代碼

 

到此,基於Docker lamp平臺的wordpress搭建成功。

若是是在生產環境中使用docker,不太建議將數據庫程序存放與docker內執行。建議MySQL 實現負載或者冗餘。

分類: Docker
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息