1、编写简单的mysql的Dockerfile文件
[root@docker ~]# cd /opt/docker-file/mysql/ [root@docker mysql]# ls Dockerfile [root@docker mysql]# cat Dockerfile FROM blalor/centos MAINTAINER molewan "molewan@163.com" RUN yum -y install mysql-server mysql RUN service mysqld start RUN chkconfig mysqld on EXPOSE 3306 CMD ["/usr/bin/mysqld_safe"] [root@docker mysql]# docker build -t wan/mysql .
2、查看生成的数据库镜像
[root@docker mysql]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE wanlong/mysql latest eb33223bfe29 About a minute ago 498.1 MB centos123 latest 2a4997703ec3 5 days ago 213.9 MB nginx-file v1 eabf358df7ea 6 days ago 512.2 MB wordpress latest 62a9acf5fc2a 2 weeks ago 517.3 MB nginx latest bbb75b846e7b 2 weeks ago 134.5 MB dockerui/dockerui latest 95c8b9dc91e0 4 weeks ago 6.13 MB redis latest 34ca6ac180ad 4 weeks ago 151.2 MB mongo latest ad74160b3443 4 weeks ago 317.4 MB registry latest 07d93e41c370 4 weeks ago 422.8 MB daocloud.io/library/python latest a2db1214d015 4 weeks ago 689.1 MB debian latest 5eb1402f0414 5 weeks ago 125.1 MB daocloud.io/library/ubuntu latest 8693db7e8a00 5 weeks ago 187.9 MB centos latest 60e65a8e4030 9 weeks ago 196.6 MB joedval/stress latest 7eb627cb08a2 12 weeks ago 214.9 MB doumadou/centos6.5_x86_64-base latest 5bcd27f3a345 7 months ago 429.8 MB lemonbar/centos6-ssh latest b78c71c001db 19 months ago 296.9 MB blalor/centos latest f01c1b138488 20 months ago 322.4 MB
3、使用镜像构建容器,并进行查看
[root@docker mysql]# docker run --name mysql123 -d -p 3306 wanlong/mysql 0eb98e3c3e60ce9082a92a2adb5c02a426e90dc1cb4cc6ddccbac40c549f5a44 [root@docker mysql]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0eb98e3c3e60 wanlong/mysql "/usr/bin/mysqld_saf 5 seconds ago Up 4 seconds 0.0.0.0:32768->3306/tcp mysql123
4、进入容器,并进行测试(端口号以及数据库)
[root@docker ~]# ./in.sh 0eb98e3c3e60 -bash: BASH_FUNC_module(): line 0: syntax error near unexpected token `)‘-bash: BASH_FUNC_module(): line 0: `BASH_FUNC_module() () { eval `/usr/bin/modulecmd bash $*`‘-bash: error importing function definition for `BASH_FUNC_module‘ -bash-4.1# ls bin boot dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var- bash-4.1# netstat -tunlp Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 91/mysqld -bash-4.1# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.00 sec)
5、in.sh
[root@docker ~]# cat in.sh #!/bin/bash CNAME=$1 CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME) nsenter --target "$CPID" --mount --uts --ipc --net --pid
本文出自 “冰冻vs西瓜” 博客,请务必保留此出处http://molewan.blog.51cto.com/287340/1746463
原文:http://molewan.blog.51cto.com/287340/1746463