no-image

【Docker實戰之入門】Dockerfile詳細分析:構建docker映象(3)DB映象

                                    

構建DB Docker映象

mysql的程式碼檔案

[root@Optimus /]# cd docker-training/
[root@Optimus docker-training]# ls
centos7  mysql  php-fpm  README.md  wordpress
[root@Optimus docker-training]# cd mysql/
[root@Optimus mysql]# ls
Dockerfile  mysqld_charset.cnf  scripts
[root@Optimus mysql]# 

dockerfile用於構建DB docker映象

[root@Optimus mysql]# vim Dockerfile 

#
# MAINTAINER        Carson,C.J.Zeong <zcy@nicescale.com>
# DOCKER-VERSION    1.6.2
#
# Dockerizing Mariadb: Dockerfile for building Mariadb images
#
FROM csphere/centos:7.1
MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com>

ENV DATA_DIR /var/lib/mysql

# Install Mariadb
RUN yum install -y mariadb mariadb-server && \
                                        yum clean all

ADD mysqld_charset.cnf /etc/my.cnf.d/

COPY scripts /scripts
RUN chmod  x /scripts/start

EXPOSE 3306

VOLUME ["/var/lib/mysql"]

ENTRYPOINT ["/scripts/start"]

[FROM csphere/centos:7.1

啟動container時主要執行start指令碼,start是一個/bin/bash指令碼

#!/bin/bash

set -e

#
# When Startup Container script
#

if [[ -e /scripts/firstrun ]]; then
    # config mariadb
    /scripts/firstrun_maria
    rm /scripts/firstrun
else
    # Cleanup previous mariadb sockets
    if [[ -e ${DATA_DIR}/mysql.sock ]]; then
        rm -f ${DATA_DIR}/mysql.sock
    fi
fi

exec /usr/bin/mysqld_safe

[#!/bin/bash

檢視當前本地docker映象

docker images
 
[root@Optimus mysql]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
csphere/mysql       5.5                 e147ac2a588e        15 minutes ago      752.7 MB
csphere/php-fpm     5.4                 1b08c94ce801        2 days ago          709.7 MB
csphere/centos      7.1                 fd1f7619e63e        3 days ago          613 MB
centos              centos7.1.1503      879c6d07c60e        6 weeks ago         212.1 MB

構建好docker映象,下面啟動mariadb的docker容器

docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5

檢視本地docker容器

[root@Optimus mysql]# docker ps -a

出錯:顯示3306埠被佔用

[root@Optimus mysql]# docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
e63911262c9490d65be1bc9c1c575f2eebaacc0c3afa3874e0f4cd687b5fc717
Error response from daemon: Cannot start container e63911262c9490d65be1bc9c1c575f2eebaacc0c3afa3874e0f4cd687b5fc717: Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use
[root@Optimus mysql]# fuser -v -n tcp 3306
                                                                                                                                                                                                                  USER        PID ACCESS COMMAND
3306/tcp:            mysql      2928 F.... mysqld
[root@Optimus mysql]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
e63911262c94        csphere/mysql:5.5     "/scripts/start"       32 minutes ago                                                                        dbserver           
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   2 days ago          Up 2 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   3 days ago          Up 3 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            4 days ago          Exited (127) 4 days ago                                           naughty_engelbart   
[root@Optimus mysql]# 

更改為3307埠號,重新啟動container,顯示已經存在dbserver這個container了

[root@Optimus mysql]# docker run -d -p 3307:3306 --name debserver csphere/mysql:5.5
Error response from daemon: Conflict. The name "debserver" is already in use by container e63911262c94. You have to delete (or rename) that container to be able to reuse that name.

刪除dbserver容器

[root@Optimus mysql]# docker rm debserver
debserver

重新run container,並且埠對映為3307

[root@Optimus mysql]# docker run -d -p 3307:3306 --name dbserver csphere/mysql:5.5
076cb9396ca0aaa9ce0418dc8c5950aa227b0f1e1def92021fcaab39502c255e

檢視容器

[root@Optimus mysql]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
076cb9396ca0        csphere/mysql:5.5     "/scripts/start"       13 minutes ago      Up 13 minutes             22/tcp, 0.0.0.0:3307->3306/tcp          dbserver            
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   2 days ago          Up 2 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   3 days ago          Up 3 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            4 days ago          Exited (127) 4 days ago       

進入互動模式,登入到容器中/bin/bash目錄下檢視

[root@Optimus mysql]# docker exec -it dbserver /bin/bash
[root@076cb9396ca0 /]# 

登入mysql,進入Mariadb中,可以進行任何資料庫的操作

 [root@076cb9396ca0 /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

檢視資料庫

MariaDB [(none)]> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| test               |
 -------------------- 
4 rows in set (0.17 sec)

MariaDB [(none)]> 

以上,一個具有Mariadb功能的docker容器建立成果。

退出mysql ,退出容器

MariaDB [(none)]> exit
Bye 
[root@076cb9396ca0 /]# exit
exit 

上面建立db容器的時候,沒有加-v引數。下面,使用docker run -v命令重新構建db容器。

docker run -v 掛載VOLUME引數

刪除容器

docker rm dbserver
不加任何引數時,不能刪除正在執行的container
docker rm -f dbserver
強制刪除

[root@Optimus mysql]# docker rm -f dbserver
dbserver

掛載-v和不掛載-v引數的區別
執行一個掛載-v引數的container
-v可以使用在資料庫、Nginx以及需要儲存檔案至宿主機下的服務上。

docker run -d -p 3307:3006 -v host_dir:container_dir
docker run -d -p 3307:3006 -v 宿主機的目錄/var/lib/docker/vfs/dir/mydata:容器的目錄/var/lib/mysql

mydata目錄和mysql的資料是一模一樣的,並且是同步的
這次不使用–name
我使用了3307做埠對映,因為本地3306埠被佔用。如果沒有被佔用,可以用3306:3306做埠對映
指定映象 csphere/mysql:5.5

[root@Optimus /]# docker run -d -p 3307:3306 -v /var/lib/docker/vfs/dir/mysqta:/var/lib/mysql csphere/mysql:5.5
9ad32fb968fe93daa349091b2725d9dc0b4ce414a9a13f4cf7751f0b15857d8f

檢視容器

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
9ad32fb968fe        csphere/mysql:5.5     "/scripts/start"       29 minutes ago      Up 29 minutes             22/tcp, 0.0.0.0:3307->3306/tcp          romantic_brattain   
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            7 days ago          Exited (127) 7 days ago                                           naughty_engelbart   
[root@Optimus /]# 

沒有使用--name,自動匹配字串 romantic_brattain 作為容器名稱

測試:掛載了-v以後,建立一個資料庫,然後刪除容器,資料是否還存在?

進入容器

docker exec -it containerID/containerName /bin/bash
進入容器中,容器必須是UP狀態,否則進不去。

[root@Optimus /]# docker exec -it 9ad /bin/bash
[root@9ad32fb968fe /]# 

登入mysql

[root@9ad32fb968fe /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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.10 sec)

建立資料庫

MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.12 sec)

檢視資料庫

MariaDB [(none)]> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
 -------------------- 
5 rows in set (0.00 sec)

退出

MariaDB [(none)]> exit
Bye
e[root@9ad32fb968fe /]# exit
exit
[root@Optimus /]# 

檢視一下剛才對映的目錄下有沒有mydb資料夾

[root@Optimus /]# ls /var/lib/docker/vfs/dir/mysqta/
aria_log.00000001  aria_log_control  ibdata1  ib_logfile0  ib_logfile1  mydb  mysql  mysql.sock  performance_schema  test

存在mydb目錄,說明宿主機已經和mysql的var目錄下的資料同步

檢視mysql映象

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
9ad32fb968fe        csphere/mysql:5.5     "/scripts/start"       5 hours ago         Up 5 hours                22/tcp, 0.0.0.0:3307->3306/tcp          romantic_brattain   
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            7 days ago          Exited (127) 7 days ago                                           naughty_engelbart   
[root@Optimus /]# 

停止容器

[root@Optimus /]# docker stop 9ad
9ad

再次檢視容器狀態為停止狀態

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                        PORTS                                   NAMES
9ad32fb968fe        csphere/mysql:5.5     "/scripts/start"       5 hours ago         Exited (137) 37 seconds ago                                           romantic_brattain   
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days                     22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days                     0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            7 days ago          Exited (127) 7 days ago                                               naughty_engelbart   
[root@Optimus /]# 

檢視UP狀態下的container

[root@Optimus /]# docker ps
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS              PORTS                                   NAMES
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days           22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days           0.0.0.0:2222->22/tcp                    base                
[root@Optimus /]# 

不用加引數,直接刪除容器

[root@Optimus /]# docker  rm 9ad
9ad

檢視mysqta目錄下的檔案依然還存在

[root@Optimus /]# ls /var/lib/docker/vfs/dir/mysqta/
aria_log.00000001  aria_log_control  ibdata1  ib_logfile0  ib_logfile1  mydb  mysql  mysql.sock  performance_schema  test
[root@Optimus /]# 

建立新的container,依然還可以繼續使用這個資料庫。

[root@Optimus /]# docker run -d -p 3307:3306 --name newdb -v /var/lib/docker/vfs/dir/mysqta:/var/lib/mysql csphere/mysql:5.5
8e266cd27a63495291c8b48f06204426c6d42b7c37ef8b86a6a6c124a5f5bafa
[root@Optimus /]# 

檢視容器狀態

[root@Optimus /]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS                    PORTS                                   NAMES
8e266cd27a63        csphere/mysql:5.5     "/scripts/start"       31 seconds ago      Up 29 seconds             22/tcp, 0.0.0.0:3307->3306/tcp          newdb               
e274049db704        csphere/php-fpm:5.4   "/usr/bin/supervisor   5 days ago          Up 5 days                 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp   website             
2d36dbfc72a0        csphere/centos:7.1    "/usr/bin/supervisor   6 days ago          Up 6 days                 0.0.0.0:2222->22/tcp                    base                
0d1f6225836b        centos                "/bin/bash"            7 days ago          Exited (127) 7 days ago                                           naughty_engelbart   
[root@Optimus /]# 

進入到容器中檢視資料庫

[root@Optimus /]# docker exec -it newdb /bin/bash
[root@8e266cd27a63 /]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, 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 |
| mydb               |
| mysql              |
| performance_schema |
| test               |
 -------------------- 
5 rows in set (0.08 sec)

MariaDB [(none)]> 

只要資料庫檔案目錄存在,container可以隨意刪除重建,恢復很快。

構建DB容器完成。

關聯文章