Preface
We commonly use VMware or VirtualBox to install our Virtual Machines before.For the sake of creating a simple testing environment with less resources.I consider to implement a MySQL db server relies on docker which can help you saving your time when developing code or something else.
Introduce
What`s docker?There`re two concept you should first try to understand,image & container(like an iso file,put all kinds of different tools in it,then manage the by containner).You`re supposed to package your applications(or indispensable software in your dev environment) to a container.Whenever you need a second evironment to install same softwares or dependent packages,only one thing that you migrate the image to it is neccessary.It`s rather simple and efficient by using docker.
Procedure
Let`s install docker and then implement a MySQL server on it,here we go.
1 ###Create a docker host named "docker" by vagrant.### 2 $ vagrant up docker 3 Bringing machine `docker` up with `virtualbox` provider... 4 ==> docker: Importing base box `CentOS7`... 5 Progress: 30% 6 ==> docker: Matching MAC address for NAT networking... 7 ==> docker: Setting the name of the VM: Vagrant_docker_1529333111553_95525 8 ==> docker: Clearing any previously set forwarded ports... 9 ==> docker: Clearing any previously set network interfaces... 10 ==> docker: Preparing network interfaces based on configuration... 11 docker: Adapter 1: nat 12 docker: Adapter 2: hostonly 13 ==> docker: Forwarding ports... 14 docker: 22 (guest) => 2222 (host) (adapter 1) 15 ==> docker: Running `pre-boot` VM customizations... 16 ==> docker: Booting VM... 17 ==> docker: Waiting for machine to boot. This may take a few minutes... 18 docker: SSH address: 127.0.0.1:2222 19 docker: SSH username: vagrant 20 docker: SSH auth method: private key 21 docker: 22 docker: Vagrant insecure key detected. Vagrant will automatically replace 23 docker: this with a newly generated keypair for better security. 24 docker: 25 docker: Inserting generated public key within guest... 26 docker: Removing insecure key from the guest if it`s present... 27 docker: Key inserted! Disconnecting and reconnecting using new SSH key... 28 ==> docker: Machine booted and ready! 29 ==> docker: Checking for guest additions in VM... 30 docker: The guest additions on this VM do not match the installed version of 31 docker: VirtualBox! In most cases this is fine, but in rare cases it can 32 docker: prevent things such as shared folders from working properly. If you see 33 docker: shared folder errors, please make sure the guest additions within the 34 docker: virtual machine match the version of VirtualBox you have installed on 35 docker: your host and reload your VM. 36 docker: 37 docker: Guest Additions Version: 4.3.28 38 docker: VirtualBox Version: 5.1 39 ==> docker: Setting hostname... 40 ==> docker: Configuring and enabling network interfaces... 41 docker: SSH address: 127.0.0.1:2222 42 docker: SSH username: vagrant 43 docker: SSH auth method: private key 44 ==> docker: Mounting shared folders... 45 docker: /vagrant => E:/Vagrant 46 47 [root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 48 -- Omitted. 49 50 Installed: 51 yum-utils.noarch 0:1.1.31-45.el7 52 53 Dependency Installed: 54 libxml2-python.x86_64 0:2.9.1-6.el7_2.3 python-chardet.noarch 0:2.2.1-1.el7_1 python-kitchen.noarch 0:1.1.1-5.el7 55 56 Updated: 57 device-mapper-persistent-data.x86_64 0:0.7.3-3.el7 lvm2.x86_64 7:2.02.177-4.el7 58 59 Dependency Updated: 60 device-mapper.x86_64 7:1.02.146-4.el7 device-mapper-event.x86_64 7:1.02.146-4.el7 device-mapper-event-libs.x86_64 7:1.02.146-4.el7 61 device-mapper-libs.x86_64 7:1.02.146-4.el7 libxml2.x86_64 0:2.9.1-6.el7_2.3 lvm2-libs.x86_64 7:2.02.177-4.el7 62 python-urlgrabber.noarch 0:3.10-8.el7 rpm.x86_64 0:4.11.3-32.el7 rpm-build-libs.x86_64 0:4.11.3-32.el7 63 rpm-libs.x86_64 0:4.11.3-32.el7 rpm-python.x86_64 0:4.11.3-32.el7 yum.noarch 0:3.4.3-158.el7.centos 64 65 ###Add repo of docker of aliyun.### 66 [root@docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 67 Loaded plugins: fastestmirror 68 adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 69 grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo 70 repo saved to /etc/yum.repos.d/docker-ce.repo 71 [root@docker ~]# 72 73 ###Create metadata cache.### 74 [root@docker ~]# yum makecache fast 75 Loaded plugins: fastestmirror 76 base | 3.6 kB 00:00:00 77 docker-ce-stable | 2.9 kB 00:00:00 78 extras | 3.4 kB 00:00:00 79 updates | 3.4 kB 00:00:00 80 docker-ce-stable/x86_64/primary_db | 13 kB 00:00:00 81 Loading mirror speeds from cached hostfile 82 * base: mirrors.shu.edu.cn 83 * extras: ftp.sjtu.edu.cn 84 * updates: mirrors.shu.edu.cn 85 Metadata Cache Created 86 87 ###Install docker from repo.### 88 [root@docker ~]# yum -y install docker-ce 89 Loaded plugins: fastestmirror 90 Loading mirror speeds from cached hostfile 91 * base: mirrors.shu.edu.cn 92 * extras: ftp.sjtu.edu.cn 93 * updates: mirrors.shu.edu.cn 94 Resolving Dependencies 95 --> Running transaction check 96 ---> Package device-mapper-persistent-data.x86_64 0:0.4.1-2.el7 will be updated 97 ---> Package device-mapper-persistent-data.x86_64 0:0.7.3-3.el7 will be an update 98 ---> Package lvm2.x86_64 7:2.02.115-3.el7 will be updated 99 ---> Package lvm2.x86_64 7:2.02.177-4.el7 will be an update 100 --> Processing Dependency: lvm2-libs = 7:2.02.177-4.el7 for package: 7:lvm2-2.02.177-4.el7.x86_64 101 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_97)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 102 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_141)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 103 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_138)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 104 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_135)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 105 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_133)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 106 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_128)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 107 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_113)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 108 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_110)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 109 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_107)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 110 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_104)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 111 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_103)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 112 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_101)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 113 --> Processing Dependency: libdevmapper.so.1.02(DM_1_02_100)(64bit) for package: 7:lvm2-2.02.177-4.el7.x86_64 114 ---> Package yum-utils.noarch 0:1.1.31-45.el7 will be installed 115 116 -- Omitted. 117 118 Installed: 119 docker-ce.x86_64 0:18.03.1.ce-1.el7.centos -- The lastest version of docker CE is 18.03.1 120 121 Dependency Installed: 122 audit-libs-python.x86_64 0:2.8.1-3.el7 checkpolicy.x86_64 0:2.5-6.el7 container-selinux.noarch 2:2.55-1.el7 123 libcgroup.x86_64 0:0.41-15.el7 libseccomp.x86_64 0:2.3.1-3.el7 libsemanage-python.x86_64 0:2.5-11.el7 124 libtool-ltdl.x86_64 0:2.4.2-22.el7_3 lz4.x86_64 0:1.7.5-2.el7 pigz.x86_64 0:2.3.3-1.el7.centos 125 policycoreutils-python.x86_64 0:2.5-22.el7 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-2.el7 126 127 Updated: 128 centos-release.x86_64 0:7-5.1804.el7.centos.2 dracut.x86_64 0:033-535.el7 initscripts.x86_64 0:9.49.41-1.el7 129 130 Dependency Updated: 131 audit.x86_64 0:2.8.1-3.el7 audit-libs.x86_64 0:2.8.1-3.el7 dracut-config-rescue.x86_64 0:033-535.el7 132 dracut-network.x86_64 0:033-535.el7 glib2.x86_64 0:2.54.2-2.el7 kmod.x86_64 0:20-21.el7 133 libgudev1.x86_64 0:219-57.el7 libselinux.x86_64 0:2.5-12.el7 libselinux-devel.x86_64 0:2.5-12.el7 134 libselinux-python.x86_64 0:2.5-12.el7 libselinux-ruby.x86_64 0:2.5-12.el7 libselinux-utils.x86_64 0:2.5-12.el7 135 libsemanage.x86_64 0:2.5-11.el7 libsepol.x86_64 0:2.5-8.1.el7 libsepol-devel.x86_64 0:2.5-8.1.el7 136 policycoreutils.x86_64 0:2.5-22.el7 selinux-policy.noarch 0:3.13.1-192.el7_5.3 selinux-policy-targeted.noarch 0:3.13.1-192.el7_5.3 137 systemd.x86_64 0:219-57.el7 systemd-libs.x86_64 0:219-57.el7 systemd-sysv.x86_64 0:219-57.el7 138 139 Complete! 140 141 ###Check docker detail version.### 142 [root@docker ~]# docker version 143 Client: 144 Version: 18.03.1-ce 145 API version: 1.37 146 Go version: go1.9.5 147 Git commit: 9ee9f40 148 Built: Thu Apr 26 07:20:16 2018 149 OS/Arch: linux/amd64 150 Experimental: false 151 Orchestrator: swarm 152 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 153 [root@docker ~]# 154 155 ###Create a docker daemon config file.### 156 [root@docker docker]# pwd 157 /etc/docker 158 [root@docker docker]# vim daemon.json 159 [root@docker docker]# cat daemon.json 160 { 161 "registry-mirrors": [ "https://registry.docker-cn.com"] 162 } 163 164 ###Start docker service and list infomations.### 165 [root@docker docker]# systemctl start docker 166 [root@docker docker]# docker info 167 Containers: 0 168 Running: 0 169 Paused: 0 170 Stopped: 0 171 Images: 0 172 Server Version: 18.03.1-ce 173 Storage Driver: devicemapper 174 Pool Name: docker-253:0-16777839-pool 175 Pool Blocksize: 65.54kB 176 Base Device Size: 10.74GB 177 Backing Filesystem: xfs 178 Udev Sync Supported: true 179 Data file: /dev/loop0 180 Metadata file: /dev/loop1 181 Data loop file: /var/lib/docker/devicemapper/devicemapper/data 182 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata 183 Data Space Used: 11.8MB 184 Data Space Total: 107.4GB 185 Data Space Available: 7.6GB 186 Metadata Space Used: 581.6kB 187 Metadata Space Total: 2.147GB 188 Metadata Space Available: 2.147GB 189 Thin Pool Minimum Free Space: 10.74GB 190 Deferred Removal Enabled: true 191 Deferred Deletion Enabled: true 192 Deferred Deleted Device Count: 0 193 Library Version: 1.02.146-RHEL7 (2018-01-22) 194 Logging Driver: json-file 195 Cgroup Driver: cgroupfs 196 Plugins: 197 Volume: local 198 Network: bridge host macvlan null overlay 199 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog 200 Swarm: inactive 201 Runtimes: runc 202 Default Runtime: runc 203 Init Binary: docker-init 204 containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88 205 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 206 init version: 949e6fa 207 Security Options: 208 seccomp 209 Profile: default 210 Kernel Version: 3.10.0-229.el7.x86_64 211 Operating System: CentOS Linux 7 (Core) 212 OSType: linux 213 Architecture: x86_64 214 CPUs: 1 215 Total Memory: 993.6MiB 216 Name: docker 217 ID: 4GY3:Q2FL:P4DD:HOFN:WFJZ:RDAV:GMJA:RDFF:VTAS:VW6D:TSC6:LCSC 218 Docker Root Dir: /var/lib/docker 219 Debug Mode (client): false 220 Debug Mode (server): false 221 Registry: https://index.docker.io/v1/ 222 Labels: 223 Experimental: false 224 Insecure Registries: 225 127.0.0.0/8 226 Registry Mirrors: 227 https://registry.docker-cn.com/ 228 Live Restore Enabled: false 229 230 WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use. 231 Use `--storage-opt dm.thinpooldev` to specify a custom block storage device. 232 WARNING: bridge-nf-call-iptables is disabled 233 WARNING: bridge-nf-call-ip6tables is disabled 234 235 ###Load image of CentOS7.### 236 [root@docker docker]# docker image 237 238 Usage: docker image COMMAND 239 240 Manage images 241 242 Options: 243 244 245 Commands: 246 build Build an image from a Dockerfile 247 history Show the history of an image 248 import Import the contents from a tarball to create a filesystem image 249 inspect Display detailed information on one or more images 250 load Load an image from a tar archive or STDIN 251 ls List images 252 prune Remove unused images 253 pull Pull an image or a repository from a registry 254 push Push an image or a repository to a registry 255 rm Remove one or more images 256 save Save one or more images to a tar archive (streamed to STDOUT by default) 257 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 258 259 Run `docker image COMMAND --help` for more information on a command. 260 [root@docker docker]# docker images 261 REPOSITORY TAG IMAGE ID CREATED SIZE 262 [root@docker docker]# cd /vagrant 263 [root@docker vagrant]# ls -l 264 total 1887022 265 -rwxrwxrwx 1 vagrant vagrant 498180608 Mar 13 16:19 centos-7.0-x86_64.box 266 -rwxrwxrwx 1 vagrant vagrant 15080 Mar 21 18:42 epel-release-latest-7.noarch.rpm 267 drwxrwxrwx 1 vagrant vagrant 4096 Mar 18 16:42 masterha 268 -rwxrwxrwx 1 vagrant vagrant 87119 Nov 7 2014 mha4mysql-manager-0.56-0.el6.noarch.rpm 269 -rwxrwxrwx 1 vagrant vagrant 113914 Nov 7 2014 mha4mysql-manager-0.56.tar.gz 270 -rwxrwxrwx 1 vagrant vagrant 118521 Mar 11 18:36 mha4mysql-manager-0.57.tar.gz 271 -rwxrwxrwx 1 vagrant vagrant 36326 Nov 7 2014 mha4mysql-node-0.56-0.el6.noarch.rpm 272 -rwxrwxrwx 1 vagrant vagrant 50172 Nov 4 2014 mha4mysql-node-0.56.tar.gz 273 -rwxrwxrwx 1 vagrant vagrant 54484 Mar 11 18:36 mha4mysql-node-0.57.tar.gz 274 drwxrwxrwx 1 vagrant vagrant 4096 Jun 2 16:23 MyFlash-master 275 -rwxrwxrwx 1 vagrant vagrant 2339403 Jun 1 16:15 MyFlash-master.zip 276 drwxrwxrwx 1 vagrant vagrant 8192 May 3 17:22 MySQL 277 -rwxrwxrwx 1 vagrant vagrant 641798603 Mar 18 14:32 mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 278 drwxrwxrwx 1 vagrant vagrant 4096 Jun 8 15:57 mysqlbinlog_flashback-master 279 -rwxrwxrwx 1 vagrant vagrant 299829 Jun 8 15:41 mysqlbinlog_flashback-master.zip 280 -rwxrwxrwx 1 vagrant vagrant 4720640 Jun 8 16:57 pip-10.0.1.tar 281 -rwxrwxrwx 1 vagrant vagrant 74297 Jun 8 17:06 PyMySQL-0.8.1.tar.gz 282 -rwxrwxrwx 1 vagrant vagrant 59238400 Jun 8 16:17 Python-2.6.9.tar 283 drwxrwxrwx 1 vagrant vagrant 4096 Jun 8 17:18 setuptools-39.2.0 284 -rwxrwxrwx 1 vagrant vagrant 851112 Jun 8 17:17 setuptools-39.2.0.zip 285 -rwxrwxrwx 1 vagrant vagrant 4140 Jun 18 16:43 Vagrantfile 286 -rwxrwxrwx 1 vagrant vagrant 540672 Apr 19 14:21 zabbix-3.0.16.tar.gz 287 -rwxrwxrwx 1 vagrant vagrant 723758592 Jun 13 17:08 zst_centos7-201806.tar 288 [root@docker vagrant]# docker image load < zst_centos7-201806.tar 289 129b697f70e9: Loading layer [==================================================>] 205.1MB/205.1MB 290 e17a43dfd99e: Loading layer [==================================================>] 20.09MB/20.09MB 291 81230abc4426: Loading layer [==================================================>] 2.802MB/2.802MB 292 a70f1502766b: Loading layer [==================================================>] 3.584kB/3.584kB 293 4c4dfc844006: Loading layer [==================================================>] 6.656kB/6.656kB 294 49fde8c22696: Loading layer [==================================================>] 6.144kB/6.144kB 295 3fe67ade2cc6: Loading layer [==================================================>] 3.072kB/3.072kB 296 eeb315c508b1: Loading layer [==================================================>] 77.82kB/77.82kB 297 0439f8e04051: Loading layer [==================================================>] 8.704kB/8.704kB 298 ec476b61f06f: Loading layer [==================================================>] 13.82kB/13.82kB 299 9f752899cdce: Loading layer [==================================================>] 5.632kB/5.632kB 300 027f1ff8b124: Loading layer [==================================================>] 3.584kB/3.584kB 301 f9b50836c42f: Loading layer [==================================================>] 5.12kB/5.12kB 302 2f68f3a1869a: Loading layer [==================================================>] 89.6kB/89.6kB 303 61f05bc1691a: Loading layer [==================================================>] 495.2MB/495.2MB 304 24d441f4dab8: Loading layer [==================================================>] 191.5kB/191.5kB 305 Loaded image ID: sha256:7d0b68af5a06e04c2713dab89a293dc1b1dff5ccbaab8235e97cbc9cff7db2cb 306 [root@docker vagrant]# docker images 307 REPOSITORY TAG IMAGE ID CREATED SIZE 308 <none> <none> 7d0b68af5a06 5 days ago 694MB 309 310 ###Add a tag for repository.### 311 [root@docker vagrant]# docker tag 7d0b68af5a06 CentOS7 312 Error parsing reference: "CentOS7" is not a valid repository/tag: invalid reference format: repository name must be lowercase 313 [root@docker vagrant]# docker tag 7d0b68af5a06 centos7 - Change the repository name into lowercase. 314 [root@docker vagrant]# docker images 315 REPOSITORY TAG IMAGE ID CREATED SIZE 316 centos7 latest 7d0b68af5a06 6 days ago 694MB 317 [root@docker vagrant]# 318 319 ###Check whether there`re running containers.### 320 [root@docker vagrant]# docker ps -a 321 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 322 [root@docker vagrant]# 323 324 ###Start a container with dependency of directory themselves.### 325 [root@docker vagrant]# mkdir mysql_soft 326 [root@docker vagrant]# mkdir data1 327 [root@docker vagrant]# docker run -d -v /vagrant/mysql_soft:/opt/mysql -v /vagrant/data1:/data -v /etc/hosts:/etc/hosts -p2222:22 --cap-add=NET_ADMIN --name node1 -h docker centos7 328 9ff260da9643d7b8243668699760d7a64d3f797a1cd2a9bc2562f11e9790dcc7 329 [root@docker vagrant]# df -h 330 Filesystem Size Used Avail Use% Mounted on 331 /dev/mapper/centos-root 8.4G 2.1G 6.3G 26% / 332 devtmpfs 488M 0 488M 0% /dev 333 tmpfs 497M 0 497M 0% /dev/shm 334 tmpfs 497M 6.7M 491M 2% /run 335 tmpfs 497M 0 497M 0% /sys/fs/cgroup 336 /dev/sda1 497M 118M 379M 24% /boot 337 none 612G 243G 369G 40% /vagrant 338 tmpfs 100M 0 100M 0% /run/user/0 339 /dev/dm-3 10G 682M 9.4G 7% /var/lib/docker/devicemapper/mnt/fe5e6b5cf3f15ebcd2a1d0805919d1858b789b37a2f5d1c31af771072e812334 340 shm 64M 0 64M 0% /var/lib/docker/containers/9ff260da9643d7b8243668699760d7a64d3f797a1cd2a9bc2562f11e9790dcc7/mounts/shm 341 [root@docker vagrant]# docker ps -a 342 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 343 9ff260da9643 centos7 "/usr/bin/supervisor…" 14 seconds ago Up 13 seconds (healthy) 0.0.0.0:2222->22/tcp node1 344 345 ###Check the IP of docker "node1".### 346 [root@docker vagrant]# docker inspect node1 | grep IPAddress 347 "SecondaryIPAddresses": null, 348 "IPAddress": "172.17.0.2", 349 "IPAddress": "172.17.0.2", 350 351 ###Login docker "node1".### 352 [root@docker vagrant]# ssh root@172.17.0.2 353 The authenticity of host `172.17.0.2 (172.17.0.2)` can`t be established. 354 ECDSA key fingerprint is 1c:3a:48:00:26:63:1f:5b:6d:82:bf:0a:4d:df:03:cc. 355 Are you sure you want to continue connecting (yes/no)? yes 356 Warning: Permanently added `172.17.0.2` (ECDSA) to the list of known hosts. 357 root@172.17.0.2`s password: 358 Last login: Wed Jun 13 00:18:11 2018 from 172.17.0.1 359 [root@docker ~]# df -h 360 Filesystem Size Used Avail Use% Mounted on 361 /dev/mapper/docker-253:0-16777839-fe5e6b5cf3f15ebcd2a1d0805919d1858b789b37a2f5d1c31af771072e812334 10G 682M 9.4G 7% / 362 tmpfs 64M 0 64M 0% /dev 363 tmpfs 497M 0 497M 0% /sys/fs/cgroup 364 none 612G 243G 369G 40% /data 365 /dev/mapper/centos-root 8.4G 2.1G 6.3G 26% /etc/hosts 366 shm 64M 0 64M 0% /dev/shm 367 tmpfs 497M 0 497M 0% /proc/scsi 368 tmpfs 497M 0 497M 0% /sys/firmware
Summary
- The docker has been installed properly.Now all the newly added container can share the contents and diskspace in directory “/vagrant” on host “docker”.
- You don`t need to to copy MySQL db software to each of your node anymore now.
- As soon as you copy a file to “/vagrant”,it will appear in the container`s dierctory “/opt/mysql” and the free diskspace depends on your diskspace of physical machine.Does it convenient?I`m afraid it really does.