ITPub博客

首页 > 云计算 > 虚拟化 > 小丸子学Docker系列之——实战Dockerfile

小丸子学Docker系列之——实战Dockerfile

原创 虚拟化 作者:wxjzqym 时间:2016-04-01 17:18:39 0 删除 编辑
 这次研究下Docker的Dockerfile,通过Dockerfile来定制化和自动build自己的image,本次要实现的需求是定制一个包含Mysql,Mongodb,Redis三种数据库服务的镜像,base image采用centos,虽然Docker的设计原则是每个容器打包一个进程及其依赖,不过这次主要是为了研究Dockerfile的用法,所以不必纠结。

操作步骤如下:
1.编写Dockerfile
[root@hadoop1 software]# more Dockerfile 
FROM centos 
MAINTAINER Lei Tan

ENV MYSQL_HOME=/opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64
ENV REDIS_HOME=/opt/freeware/redis-3.0.1
ENV MONGODB_HOME=/opt/freeware/mongodb-linux-x86_64-rhel62-3.0.3
ENV PATH=$MYSQL_HOME/bin:$REDIS_HOME/bin:$MONGODB_HOME/bin:$PATH

RUN mkdir -p /opt/freeware \
        && mkdir -p /opt/mongodb/{dbdata,logs} \
        && yum install perl -y \
        && yum install net-tools -y \
        && yum install perl-Module-Install.noarch -y \
        && yum install libaio -y \
        && groupadd mysql \
        && useradd -r -g mysql -s /bin/false mysql

COPY software.tgz /opt/freeware/
COPY multi_process.sh /opt/freeware/

RUN  cd /opt/freeware && tar xzvf software.tgz >/dev/null \
        && ln -s Percona-Server-5.6.24-rel72.2-Linux.x86_64 mysql \
        && cd mysql \
        && chown -R mysql:mysql . \
        && scripts/mysql_install_db --user=mysql --port=3306 \
        && chown -R root . \
        && chown -R mysql data 

WORKDIR /opt/freeware

EXPOSE 3307 16379 28017

ENTRYPOINT   ./multi_process.sh 

2.编译自己的镜像(包含Mysql,Mongodb,Redis三种数据库服务)
[root@hadoop1 software]# docker build -f Dockerfile -t tlapp:V1.0 .
Sending build context to Docker daemon 7.792 MB
Sending build context to Docker daemon 15.04 MB
Sending build context to Docker daemon 22.84 MB
Sending build context to Docker daemon 30.64 MB
Sending build context to Docker daemon 38.44 MB
Sending build context to Docker daemon 46.24 MB
Sending build context to Docker daemon 54.03 MB
Sending build context to Docker daemon 61.83 MB
。。。
Sending build context to Docker daemon 3.054 GB
Sending build context to Docker daemon 3.062 GB
Sending build context to Docker daemon  3.07 GB
Sending build context to Docker daemon 3.077 GB
Sending build context to Docker daemon  3.08 GB
Step 1 : FROM centos
 ---> d0e7f81ca65c
Step 2 : MAINTAINER Lei Tan
 ---> Running in 06bf9958b043
 ---> f9d99354b32b
Removing intermediate container 06bf9958b043
Step 3 : ENV MYSQL_HOME /opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64
 ---> Running in 2faa96bf3e09
 ---> bf4ecd06e5ab
Removing intermediate container 2faa96bf3e09
Step 4 : ENV REDIS_HOME /opt/freeware/redis-3.0.1
 ---> Running in e9796e48d8e6
 ---> 1507372b5b12
Removing intermediate container e9796e48d8e6
Step 5 : ENV MONGODB_HOME /opt/freeware/mongodb-linux-x86_64-rhel62-3.0.3
 ---> Running in 0f1f0a342c46
 ---> f31ba336ff30
Removing intermediate container 0f1f0a342c46
Step 6 : ENV PATH $MYSQL_HOME/bin:$REDIS_HOME/bin:$MONGODB_HOME/bin:$PATH
 ---> Running in 0abedfe95731
 ---> d9ad08c824d0
Removing intermediate container 0abedfe95731
Step 7 : RUN mkdir -p /opt/freeware         && mkdir -p /opt/mongodb/{dbdata,logs}         && yum install perl -y         && yum install net-tools -y         && yum in
stall perl-Module-Install.noarch -y         && yum install libaio -y         && groupadd mysql         && useradd -r -g mysql -s /bin/false mysql
 ---> Running in 979a2d98505d
Loaded plugins: fastestmirror, ovl
http://centos.01link.hk/7.2.1511/os/x86_64/repodata/436345f4b666f0a461d479ccfabc2c22823d4f2173c2653e5250fea62f0afe98-c7-x86_64-comps.xml.gz: [Errno 14] HTTP Error 
302 - Found
Trying other mirror.
Determining fastest mirrors
 * base: mirrors.sina.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.neusoft.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package perl.x86_64 4:5.16.3-286.el7 will be installed
--> Processing Dependency: perl-libs = 4:5.16.3-286.el7 for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl-libs for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-286.el7.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 4:perl-5.16.3-286.el7.x86_64
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-2.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-2.el7.noarch
================================================================================
Installing:
 perl                        x86_64      4:5.16.3-286.el7       base      8.0 M
Installing for dependencies:
 groff-base                  x86_64      1.22.2-8.el7           base      942 k
 perl-Carp                   noarch      1.26-244.el7           base       19 k
 perl-Encode                 x86_64      2.51-7.el7             base      1.5 M
 perl-Exporter               noarch      5.68-3.el7             base       28 k
 perl-File-Path              noarch      2.09-2.el7             base       26 k
 perl-File-Temp              noarch      0.23.01-3.el7          base       56 k
 perl-Filter                 x86_64      1.49-3.el7             base       76 k
 perl-Getopt-Long            noarch      2.40-2.el7             base       56 k
 perl-HTTP-Tiny              noarch      0.033-3.el7            base       38 k
 perl-PathTools              x86_64      3.40-5.el7             base       82 k
 perl-Pod-Escapes            noarch      1:1.04-286.el7         base       50 k
 perl-Pod-Perldoc            noarch      3.20-4.el7             base       87 k
 perl-Pod-Simple             noarch      1:3.28-4.el7           base      216 k
 perl-Pod-Usage              noarch      1.63-3.el7             base       27 k
 perl-Scalar-List-Utils      x86_64      1.27-248.el7           base       36 k
 perl-Socket                 x86_64      2.010-3.el7            base       49 k
 perl-Storable               x86_64      2.45-3.el7             base       77 k
 perl-Text-ParseWords        noarch      3.29-4.el7             base       14 k
 perl-Time-HiRes             x86_64      4:1.9725-3.el7         base       45 k
 perl-Time-Local             noarch      1.2300-2.el7           base       24 k
 perl-constant               noarch      1.27-2.el7             base       19 k
 perl-libs                   x86_64      4:5.16.3-286.el7       base      687 k
 perl-macros                 x86_64      4:5.16.3-286.el7       base       43 k
 perl-parent                 noarch      1:0.225-244.el7        base       12 k
 perl-podlators              noarch      2.5.1-3.el7            base      112 k
 perl-threads                x86_64      1.87-4.el7             base       49 k
 perl-threads-shared         x86_64      1.43-6.el7             base       39 k


Transaction Summary
================================================================================
Install  1 Package (+27 Dependent packages)


Total download size: 12 M
Installed size: 40 M
Downloading packages:
。。。
Running transaction
  Installing : libaio-0.3.109-13.el7.x86_64                                 1/1 
  Verifying  : libaio-0.3.109-13.el7.x86_64                                 1/1 


Installed:
  libaio.x86_64 0:0.3.109-13.el7                                                


Complete!
 ---> 8f72a183d4b4
Removing intermediate container 979a2d98505d
Step 8 : COPY software.tgz /opt/freeware/
 ---> 76928394eef8
Removing intermediate container 247ac466af9e
Step 9 : COPY multi_process.sh /opt/freeware/
 ---> f92a1b2026a4
Removing intermediate container d3a5de43097a
Step 10 : RUN cd /opt/freeware && tar xzvf software.tgz >/dev/null         && ln -s Percona-Server-5.6.24-rel72.2-Linux.x86_64 mysql         && cd mysql         && cho
wn -R mysql:mysql .         && scripts/mysql_install_db --user=mysql --port=3306         && chown -R root .         && chown -R mysql data
 ---> Running in 557b5ffe6598
Installing MySQL system tables...2016-04-01 08:01:28 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp 
server option (see documentation for more details).
2016-04-01 08:01:28 0 [Note] ./bin/mysqld (mysqld 5.6.24-72.2) starting as process 14 ...
2016-04-01 08:01:28 14 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-04-01 08:01:28 14 [Note] InnoDB: The InnoDB memory heap is disabled
2016-04-01 08:01:28 14 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-04-01 08:01:28 14 [Note] InnoDB: Memory barrier is not used
2016-04-01 08:01:28 14 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-04-01 08:01:28 14 [Note] InnoDB: Using Linux native AIO
2016-04-01 08:01:28 14 [Note] InnoDB: Using CPU crc32 instructions
2016-04-01 08:01:28 14 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-04-01 08:01:28 14 [Note] InnoDB: Completed initialization of buffer pool
2016-04-01 08:01:28 14 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-04-01 08:01:28 14 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-04-01 08:01:28 14 [Note] InnoDB: Database physically writes the file full: wait...
2016-04-01 08:01:33 14 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-04-01 08:01:33 14 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-04-01 08:01:36 14 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-04-01 08:01:36 14 [Warning] InnoDB: New log files created, LSN=45781
2016-04-01 08:01:36 14 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-04-01 08:01:36 14 [Note] InnoDB: Doublewrite buffer created
2016-04-01 08:01:36 14 [Note] InnoDB: 128 rollback segment(s) are active.
2016-04-01 08:01:36 14 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-04-01 08:01:36 14 [Note] InnoDB: Foreign key constraint system tables created
2016-04-01 08:01:36 14 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-04-01 08:01:36 14 [Note] InnoDB: Tablespace and datafile system tables created.
2016-04-01 08:01:36 14 [Note] InnoDB: Waiting for purge to start
2016-04-01 08:01:36 14 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.24-rel72.2 started; log sequence number 0
2016-04-01 08:01:36 14 [Note] RSA private key file not found: /opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64/data//private_key.pem. Some authenticatio
n plugins will not work.
2016-04-01 08:01:36 14 [Note] RSA public key file not found: /opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64/data//public_key.pem. Some authentication plugins
 will not work.
2016-04-01 08:01:40 14 [Note] Binlog end
2016-04-01 08:01:40 14 [Note] InnoDB: FTS optimize thread exiting.
2016-04-01 08:01:40 14 [Note] InnoDB: Starting shutdown...
2016-04-01 08:01:42 14 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK


Filling help tables...2016-04-01 08:01:42 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server opti
on (see documentation for more details).
2016-04-01 08:01:42 0 [Note] ./bin/mysqld (mysqld 5.6.24-72.2) starting as process 38 ...
2016-04-01 08:01:42 38 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-04-01 08:01:42 38 [Note] InnoDB: The InnoDB memory heap is disabled
2016-04-01 08:01:42 38 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-04-01 08:01:42 38 [Note] InnoDB: Memory barrier is not used
2016-04-01 08:01:42 38 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-04-01 08:01:42 38 [Note] InnoDB: Using Linux native AIO
2016-04-01 08:01:42 38 [Note] InnoDB: Using CPU crc32 instructions
2016-04-01 08:01:42 38 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-04-01 08:01:42 38 [Note] InnoDB: Completed initialization of buffer pool
2016-04-01 08:01:42 38 [Note] InnoDB: Highest supported file format is Barracuda.
2016-04-01 08:01:42 38 [Note] InnoDB: 128 rollback segment(s) are active.
2016-04-01 08:01:42 38 [Note] InnoDB: Waiting for purge to start
2016-04-01 08:01:42 38 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.24-rel72.2 started; log sequence number 1625977
2016-04-01 08:01:42 38 [Note] RSA private key file not found: /opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64/data//private_key.pem. Some authenticatio
n plugins will not work.
2016-04-01 08:01:42 38 [Note] RSA public key file not found: /opt/freeware/Percona-Server-5.6.24-rel72.2-Linux.x86_64/data//public_key.pem. Some authentication plugins
 will not work.
2016-04-01 08:01:42 38 [Note] Binlog end
2016-04-01 08:01:42 38 [Note] InnoDB: FTS optimize thread exiting.
2016-04-01 08:01:42 38 [Note] InnoDB: Starting shutdown...
2016-04-01 08:01:44 38 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK


To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:


  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h 0bc4c5093a7b password 'new-password'


Alternatively you can run:


  ./bin/mysql_secure_installation


which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:


  cd . ; ./bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl


  cd mysql-test ; perl mysql-test-run.pl


Please report any problems at
 https://bugs.launchpad.net/percona-server/+filebug


The latest information about Percona Server is available on the web at
  http://www.percona.com/software/percona-server


Support Percona by buying support at
 http://www.percona.com/products/mysql-support


 ---> bbb9e8ff3444
Removing intermediate container 557b5ffe6598
Step 11 : WORKDIR /opt/freeware
 ---> Running in 1650ea7e1d40
 ---> 256bc3b041ab
Removing intermediate container 1650ea7e1d40
Step 12 : EXPOSE 3307 16379 28017
 ---> Running in e4713a6cf1d1
 ---> 695fa7ec0b0f
Removing intermediate container e4713a6cf1d1
Step 13 : ENTRYPOINT ./multi_process.sh && touch t2.txt && echo sucessful >t.txt
 ---> Running in 47b06877f798
 ---> 05abe010e1dd
Removing intermediate container 47b06877f798
Successfully built 05abe010e1dd
注:至此编译成功。

3.用自编译的image运行一个container
[root@hadoop1 software]# docker run --name=tl_mms -p 6666:16379 -p 8888:28017 -d tlapp:V1.0
2c470fc1c3e12a71640972c1e7c7277151db867059e2b33d994e94307e06b90f
注:后台运行一个container会返回一个containter id。

4.验证容器内的MMS服务(Mysql&Mmongodb&Redis)
4.1 验证Mysql服务
[root@hadoop1 software]# docker exec -it tl_mms mysql -uroot -p -e "show variables like 'port'" 
Enter password: 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3307  |
+---------------+-------+

4.2 验证Mongodb服务
[root@hadoop1 software]# mongo hadoop1:8888
MongoDB shell version: 3.0.3
connecting to: hadoop1:8888/test
Server has startup warnings: 
2016-04-01T08:58:45.135+0000 I STORAGE  [initandlisten] 
2016-04-01T08:58:45.135+0000 I STORAGE  [initandlisten] ** WARNING: Readahead for /opt/mongodb/dbdata is set to 4096KB
2016-04-01T08:58:45.135+0000 I STORAGE  [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
2016-04-01T08:58:45.135+0000 I STORAGE  [initandlisten] **          http://dochub.mongodb.org/core/readahead
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] 
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] 
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] 
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-04-01T08:58:49.960+0000 I CONTROL  [initandlisten] 
> show dbs;
local  0.078GB

4.3 验证Redis服务
[root@hadoop1 software]# 
[root@hadoop1 software]# redis-cli -h hadoop1 -p 6666
hadoop1:6666> set person fok
OK
hadoop1:6666> get person
"fok"
注:从上面操作可以发现container内的Mysql,Mongodb,Redis服务可以被正常访问。

5.总结
对与Dockerfile编写过程中遇到的的一些问题以及理解记录在此:
1.COPY模块只能拷贝本地的文件到镜像中不能拷贝目录,如需拷贝目录的话许打包再拷贝
2.RUN模块中&不能与&&连用,否则会报语法错误;任何一个占用前台的命令都会导致编译镜像的过程hang住;如果将某一个命令发到后台执行编译可以通过,但是启动容器后改命令不会处于运行状态
3.EXPOSE模块的作用是暴露容器内的监听端口,但是这里即使不写也可以通过docker run的-p参数来暴露
4.ENTRYPOINT模块作为容器启动后默认加载的命令,但是无法通过&&来实现多命令加载的效果,这里我使用的方法是讲多个命令封装在一个shell脚本中,然后将此脚本作为ENTRYPOINT模块的参数

mutli_process.sh脚本内容如下:
#!/bin/bash
mongod --dbpath=/opt/mongodb/dbdata --logpath=/opt/mongodb/logs/mongod.log --port 28017 &
redis-server --port 16379 &
cd /opt/freeware/mysql
bin/mysqld_safe --user=mysql --port=3307

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20801486/viewspace-2073846/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-05-15

  • 博文量
    100
  • 访问量
    472464