34323 发表于 2016-8-5 08:47:53

用Docker构建LNMP环境(上)

利用docker搭建lnmp环境(上)
小知识:

1
2
3
docker镜像运行之后变成容器(docker run)
Registry 是Docker镜像的中央存储仓库(pull/push)
https://git.oschina.net/                  #从这个地方拉取git的仓库








1.源码克隆到自己的linux服务器上

1
2
3
4
5
6
7
8
# 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.
#




2.创建centos7镜像其余的软件全部基于centos7

1
2
3
4
5
6
7
8
9
# pwd
/root/docker-training/centos7
# docker build -t csphere/centos:7.1 ./
# dockerimages
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
#




3.解释说明Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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使用一个镜像运行一个容器

1
2
3
4
dockerrun-d   -p2222:22   不管此个容器是否停止 下次系统还是用2222来映射咱们dockefile中的22端口
docker run   -d   -P2222:22容器重启以后 系统会随机的分配一个 没有使用的端口
--name容器的名字
-d后台




5.创建centos7.1容器

1
2
3
# docker run -d -p 2222:22 --name base csphere/centos:7.1   #通过csphere/centos:7.1创建一个名字为base放在后台运行的docker容器
55e9793e58c0f70ec2d358d2eeb11e1a1afebe7987d64339da0a30da995ef340#容器的长id号返回来
#




6.查看创建的容器

1
2
3
# dockerps -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的容器)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# docker build -t csphere/php-fpm:5.4.
最后的内容
Removing intermediate container a3b57f2eb456
Successfully built 38d1572aef94

# 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

# docker run -d -p 8080:80 --name website csphere/php-fpm:5.4
# 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内部查看

1
2
3
4
5
6
7
8
9
# dockerexec -it website /bin/bash
# supervisor
supervisorctlsupervisord   
# supervisor
supervisorctlsupervisord   
# supervisorctl
nginx                            RUNNING   pid 10, uptime 0:07:30
php-fpm                        RUNNING   pid 11, uptime 0:07:30
supervisor> exit




9.创建mysql的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# dockerbuild -t csphere/mysql:5.5   ./
# 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
# docker run -d -p 3305:3306 --name dbservercsphere/mysql:5.5
36fb44b10702ad1eb38f646e381354ef8b2d832d27b54f8eb3ce5424b6c41e7e
# dockerps -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容器内部
# dockerexec -it dbserver   /bin/bash#进入容器的命令




10-v参数的使用可以挂载一个目录到本地,docker投容器mysql这个容器如果被删除了那么还可以恢复数据数据文件还在的
此处的思想就是:先用-v参数启动一个数据库的docker容器,然后删除容器,再次新建一个数据库的docker容器,,挂载的目录还是原来的目录可以看出数据依然还在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
docker run -d -p 3306:3306-v /var/lib/docker/vfs/dir/mydata:/var/lib/mysqlcsphere/mysql:5.5
docker exec -it e5387295506f/bin/bash
mysql
show databases;
createdatabasemydb ;
# cd   /var/lib/docker/vfs/dir/mydata/
# ls
aria_log.00000001ibdata1      ib_logfile1mysql       performance_schema
aria_log_control   ib_logfile0mydb         mysql.socktest
#
准备删除docker容器
# 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   
# docker rm -f e5387295506f#强制删除一个容器   也可以dockerstop e5387295506f &&docker rm e5387295506f
e5387295506f
# cd   /var/lib/docker/vfs/dir/mydata/      #可以看出容器被删除了但是数据还在
# ls
aria_log.00000001ibdata1      ib_logfile1mysql       performance_schema
aria_log_control   ib_logfile0mydb         mysql.socktest
#
# dockerrun -d -p 3306:3306 --name newdb-v   /var/lib/docker/vfs/dir/mydata:/var/lib/mysqlcsphere/mysql:5.5#生成新的数据进行再次挂载
d9dddf959cdfdf7506812b8da258e8ac74e3af5cdf3223bbc26b24fcb2289fee
#
# docker exec -it newdb/bin/bash#再次进入
# mysql
show databases;#可以看出以前创建的数据库还在





页: [1]
查看完整版本: 用Docker构建LNMP环境(上)