小丸子學Docker系列之——實戰Dockerfile

wxjzqym發表於2016-04-01
 這次研究下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
[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 () 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 () 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
 


The latest information about Percona Server is available on the web at
  /software/percona-server


Support Percona by buying support at
 /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] **          
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章