利用docker搭建lnmp环境(上)
小知识:
docker镜像运行之后变成容器(docker run) Registry 是Docker镜像的中央存储仓库(pull/push) https://git.oschina.net/ #从这个地方拉取git的仓库
1.源码克隆到自己的linux服务器上
[root@oldboy ~]# git clone https://git.oschina.net/xxsl/docker-training.git Initialized empty Git repository in /root/docker-training/.git/ remote: Counting objects: 2045, done. remote: Compressing objects: 100% (1326/1326), done. remote: Total 2045 (delta 681), reused 2045 (delta 681) Receiving objects: 100% (2045/2045), 7.22 MiB | 386 KiB/s, done. Resolving deltas: 100% (681/681), done. [root@oldboy ~]#
2.创建centos7镜像其余的软件全部基于centos7
[root@oldboy centos7]# pwd /root/docker-training/centos7 [root@oldboy centos7]# docker build -t csphere/centos:7.1 ./ [root@oldboy centos7]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE csphere/centos 7.1 080063d1c72d 6 seconds ago 591.4 MB jb/jobs03 latest dd40474b2a4c 17 hours ago 1.093 MB jobs03 latest dd40474b2a4c 17 hours ago 1.093 MB [root@oldboy centos7]#
3.解释说明Dockerfile
[root@oldboy centos7]# cat Dockerfile # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION 1.6.2 # # Dockerizing CentOS7: Dockerfile for building CentOS images # FROM centos:centos7.1.1503 MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> ENV TZ "Asia/Shanghai" #ENV环境变量 以后在docker容器run指令使用,并在容器运行保持 ENV TERM xterm ADD aliyun-mirror.repo /etc/yum.repos.d/CentOS-Base.repo ADD aliyun-epel.repo /etc/yum.repos.d/epel.repo RUN yum install -y curl wget tar bzip2 unzip vim-enhanced passwd sudo yum-utils hostname net-tools rsync man && yum install -y gcc gcc-c++ git make automake cmake patch logrotate python-devel libpng-devel libjpeg-devel && yum install -y --enablerepo=epel pwgen python-pip && yum clean all RUN pip install supervisor #RUN执行的指令 ADD supervisord.conf /etc/supervisord.conf #将本地的文件上传至/etc/下命名为supervisord.conf RUN mkdir -p /etc/supervisor.conf.d && mkdir -p /var/log/supervisor EXPOSE 22 #告诉docker服务端容器暴露的端口 ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf"]
4.docker使用一个镜像运行一个容器
docker run -d -p 2222:22 不管此个容器是否停止 下次系统还是用2222 来映射咱们dockefile中的22端口 docker run -d -P 2222:22 容器重启以后 系统会随机的分配一个 没有使用的端口 --name 容器的名字 -d 后台
5.创建centos7.1容器
[root@oldboy ~]# docker run -d -p 2222:22 --name base csphere/centos:7.1 #通过csphere/centos:7.1 创建一个名字为base放在后台运行的docker容器 55e9793e58c0f70ec2d358d2eeb11e1a1afebe7987d64339da0a30da995ef340 #容器的长id号返回来 [root@oldboy ~]#
6.查看创建的容器
[root@oldboy ~]# docker ps -a #查看容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 55e9793e58c0 csphere/centos:7.1 "/usr/bin/supervisor 2 minutes ago Up 2 minutes 0.0.0.0:2222->22/tcp base
7.创建php的容器(以及运行php的容器)
[root@oldboy php-fpm]# docker build -t csphere/php-fpm:5.4 . 最后的内容 Removing intermediate container a3b57f2eb456 Successfully built 38d1572aef94 [root@oldboy php-fpm]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE csphere/php-fpm 5.4 38d1572aef94 36 seconds ago 685 MB csphere/centos 7.1 080063d1c72d 49 minutes ago 591.4 MB [root@oldboy php-fpm]# docker run -d -p 8080:80 --name website csphere/php-fpm:5.4 [root@oldboy php-fpm]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf1f2bd73808 csphere/php-fpm:5.4 "/usr/bin/supervisor 8 seconds ago Up 8 seconds 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website 55e9793e58c0 csphere/centos:7.1 "/usr/bin/supervisor 33 minutes ago Up 33 minutes 0.0.0.0:2222->22/tcp base 测试容器OK
8进入一个docker内部查看
[root@oldboy php-fpm]# docker exec -it website /bin/bash [root@cf1f2bd73808 /]# supervisor supervisorctl supervisord [root@cf1f2bd73808 /]# supervisor supervisorctl supervisord [root@cf1f2bd73808 /]# supervisorctl nginx RUNNING pid 10, uptime 0:07:30 php-fpm RUNNING pid 11, uptime 0:07:30 supervisor> exit
9.创建mysql的容器
[root@oldboy mysql]# docker build -t csphere/mysql:5.5 ./ [root@oldboy mysql]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE csphere/mysql 5.5 63b2bd2cab97 35 seconds ago 725.1 MB csphere/php-fpm 5.4 38d1572aef94 33 minutes ago 685 MB csphere/centos 7.1 080063d1c72d About an hour ago 591.4 MB [root@oldboy mysql]# docker run -d -p 3305:3306 --name dbserver csphere/mysql:5.5 36fb44b10702ad1eb38f646e381354ef8b2d832d27b54f8eb3ce5424b6c41e7e [root@oldboy mysql]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36fb44b10702 csphere/mysql:5.5 "/scripts/start" 9 seconds ago Up 8 seconds 22/tcp, 0.0.0.0:3305->3306/tcp dbserver cf1f2bd73808 csphere/php-fpm:5.4 "/usr/bin/supervisor 27 minutes ago Up 27 minutes 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website 55e9793e58c0 csphere/centos:7.1 "/usr/bin/supervisor About an hour ago Up About an hour 0.0.0.0:2222->22/tcp base 进入docker容器内部 [root@oldboy ~]# docker exec -it dbserver /bin/bash #进入容器的命令
10-v参数的使用 可以挂载一个目录到本地,docker投容器mysql这个容器如果被删除了那么还可以恢复数据数据文件还在的
此处的思想就是:先用-v 参数启动一个数据库的docker容器,然后删除容器,再次新建一个数据库的docker容器,,挂载的目录还是原来的目录可以看出数据依然还在
docker run -d -p 3306:3306 -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5 docker exec -it e5387295506f /bin/bash mysql show databases; create database mydb ; [root@oldboy centos7]# cd /var/lib/docker/vfs/dir/mydata/ [root@oldboy mydata]# ls aria_log.00000001 ibdata1 ib_logfile1 mysql performance_schema aria_log_control ib_logfile0 mydb mysql.sock test [root@oldboy mydata]# 准备删除docker容器 [root@oldboy mydata]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5387295506f csphere/mysql:5.5 "/scripts/start" 36 minutes ago Up 36 minutes 22/tcp, 0.0.0.0:3306->3306/tcp suspicious_morse [root@oldboy mydata]# docker rm -f e5387295506f #强制删除一个容器 也可以docker stop e5387295506f &&docker rm e5387295506f e5387295506f [root@oldboy mydata]# cd /var/lib/docker/vfs/dir/mydata/ #可以看出容器被删除了但是数据还在 [root@oldboy mydata]# ls aria_log.00000001 ibdata1 ib_logfile1 mysql performance_schema aria_log_control ib_logfile0 mydb mysql.sock test [root@oldboy mydata]# [root@oldboy mydata]# docker run -d -p 3306:3306 --name newdb -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5 #生成新的数据进行再次挂载 d9dddf959cdfdf7506812b8da258e8ac74e3af5cdf3223bbc26b24fcb2289fee [root@oldboy mydata]# [root@oldboy mydata]# docker exec -it newdb /bin/bash #再次进入 [root@d9dddf959cdf /]# mysql show databases; #可以看出以前创建的数据库还在
本文出自 “砖家博客” 博客,请务必保留此出处http://wsxxsl.blog.51cto.com/9085838/1834558
原文地址:http://wsxxsl.blog.51cto.com/9085838/1834558