本次实战是对着网易云课堂的免费课程操作记录。有兴趣的童鞋可以直接去看视频docker实战之路
简单demo
- 基础镜像
- 中间件镜像php
- 中间件镜像mysql
- 数据挂载
- 应用镜像
基础镜像
- 从码云克隆文件下来
git clone https://gitee.com/byleila/docker-training.git
使用现有的dockerfile创建一个镜像
语法:docker build -t registry_url/namespace/镜像名称:镜像版本 /dockerfile path
docker build -t csphere/centos:7.1 .
- 查看生成的docker镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
csphere/centos 7.1 a21878377f20 21 seconds ago 612.9 MB
docker.io/centos centos7.1.1503 fbe8925ecf55 12 weeks ago 212.1 MB
- 使用镜像生成容器
# docker help run
# docker run -d -p 2222:22 --name base csphere/centos:7.1
3a1459de687c89067a4b638ae7007538dfbc5f3590c04c7424c6ac4b4c1ded54
参数说明:
-d : 后台运行容器,返回容器id
-p : 设定指定端口映射
--name : 容器名称
最后指定使用的镜像REPOSITORY:TAG
- 查看启动的容器
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a1459de687c csphere/centos:7.1 "/usr/bin/supervisord" 12 minutes ago Up 12 minutes 0.0.0.0:2222->22/tcp base
docker ps -a 查看所有容器
中间件镜像phpfrom
# docker build -t csphere/php-fpm:5.4 .
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
csphere/centos 7.1 a21878377f20 3 hours ago 612.9 MB
csphere/php-fpm 5.4 a21878377f20 3 hours ago 612.9 MB
# docker run -d -p 8080:80 --name website csphere/php-fpm:5.4
f1a34ae0d90da8513c81b24298e8c22b60d557f11c2b2f56a0500f042a0aa01f
[root@localhost centos7]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1a34ae0d90d csphere/php-fpm:5.4 "/usr/bin/supervisord" 8 seconds ago Up 6 seconds 22/tcp, 0.0.0.0:8080->80/tcp website
3a1459de687c csphere/centos:7.1 "/usr/bin/supervisord" 50 minutes ago Up 50 minutes 0.0.0.0:2222->22/tcp base
在浏览器中访问:http://ip:8080/info.php,可以查看到“
PHP Logo
PHP Version 5.4.16”的说明,说明php启动成功。
查看docker中的应用是否启动:
#进入容器
[root@localhost php-fpm]# docker exec -it website /bin/bash
[root@7c7382fed9a1 /]# supervisorctl
nginx RUNNING pid 7, uptime 0:45:57
php-fpm RUNNING pid 8, uptime 0:45:57
supervisor> exit
#退出容器
[root@7c7382fed9a1 /]# exit
exit
中间件镜像mysql
//使用dockerfile创建镜像
[root@localhost mysql]# docker build -t csphere/mysql:5.5 .
//查找本地镜像
[root@localhost mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
csphere/mysql 5.5 47aeeae960f9 43 seconds ago 752.7 MB
csphere/php-fpm 5.4 91e5881763e3 About an hour ago 709.9 MB
csphere/centos 7.1 36354d5271a6 2 hours ago 612.9 MB
//指定容器端口映射与名称,启动mysql容器
[root@localhost mysql]# docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
d875f72a69d8470812fe870df83fcf1c413c5daaa4d1677c095f2fc979202371
//查看容器状态
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d875f72a69d8 csphere/mysql:5.5 "/scripts/start" 26 seconds ago Up 21 seconds 22/tcp, 0.0.0.0:3306->3306/tcp dbserver
7c7382fed9a1 csphere/php-fpm:5.4 "/usr/bin/supervisord" About an hour ago Up About an hour 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
8dab7c1789a6 csphere/centos:7.1 "/usr/bin/supervisord" 2 hours ago Up 2 hours 0.0.0.0:2222->22/tcp base
//进入容器
[root@localhost mysql]# docker exec -it dbserver /bin/bash
//使用mysql
[root@d875f72a69d8 /]# 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.08 sec)
MariaDB [(none)]> exit
Bye
[root@d875f72a69d8 /]# exit
exit
mysql数据挂载本地盘
- 删除原来创建的非挂载容器
//运行状态的docker容器需要-f强制删除
[root@localhost mysql]# docker rm dbserver
Error response from daemon: You cannot remove a running container d875f72a69d8470812fe870df83fcf1c413c5daaa4d1677c095f2fc979202371. Stop the container before attempting removal or use -f
[root@localhost mysql]# docker rm -f dbserver
dbserver
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c7382fed9a1 csphere/php-fpm:5.4 "/usr/bin/supervisord" About an hour ago Up About an hour 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
8dab7c1789a6 csphere/centos:7.1 "/usr/bin/supervisord" 2 hours ago Up 2 hours 0.0.0.0:2222->22/tcp base
- 关联mysql文件到本地
由于我的系统是centos7,执行挂载-v参数后创建容器失败,解决方法在此:mysql挂载
[root@localhost mysql]# docker run -d -p 3306:3306 -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql --privileged=true --name dbserver csphere/mysql:5.5
1f336bb3bcae2613b56a24d8bbcd881db1f445aa7b60ab3a2973e6385d9ac155
[root@localhost mysql]#
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f336bb3bcae csphere/mysql:5.5 "/scripts/start" 9 seconds ago Up 6 seconds 22/tcp, 0.0.0.0:3306->3306/tcp dbserver
7c7382fed9a1 csphere/php-fpm:5.4 "/usr/bin/supervisord" 2 hours ago Up 2 hours 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
8dab7c1789a6 csphere/centos:7.1 "/usr/bin/supervisord" 3 hours ago Up 3 hours 0.0.0.0:2222->22/tcp base
- 进入容器,新建数据库。查看挂载状态
[root@localhost mysql]# docker exec -it dbserver /bin/bash
[root@1f336bb3bcae /]# 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)]> create database mydb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@1f336bb3bcae /]# exit
exit
[root@localhost mysql]# ls /var/lib/docker/vfs/dir/mydata/
aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 mydb mysql mysql.sock performance_schema test
- 删除容器,查看本地文件依然存在
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f336bb3bcae csphere/mysql:5.5 "/scripts/start" 25 minutes ago Up 25 minutes 22/tcp, 0.0.0.0:3306->3306/tcp dbserver
7c7382fed9a1 csphere/php-fpm:5.4 "/usr/bin/supervisord" 3 hours ago Up 3 hours 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
8dab7c1789a6 csphere/centos:7.1 "/usr/bin/supervisord" 3 hours ago Up 3 hours 0.0.0.0:2222->22/tcp base
[root@localhost mysql]# docker rm -f dbserver
dbserver
[root@localhost mysql]# ls /var/lib/docker/vfs/dir/mydata/
aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 mydb mysql mysql.sock performance_schema test
- 新增容器,使用共享数据,发现之前的mydb依然存在
[root@localhost mysql]# docker run -d -p 3306:3306 --privileged=true --name newdb -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5
60572f948087f347ffeb9b8974dc90480f3f565755449766907515add23fde38
[root@localhost mysql]#
[root@localhost mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60572f948087 csphere/mysql:5.5 "/scripts/start" 8 seconds ago Up 5 seconds 22/tcp, 0.0.0.0:3306->3306/tcp newdb
7c7382fed9a1 csphere/php-fpm:5.4 "/usr/bin/supervisord" 3 hours ago Up 3 hours 22/tcp, 443/tcp, 0.0.0.0:8080->80/tcp website
8dab7c1789a6 csphere/centos:7.1 "/usr/bin/supervisord" 4 hours ago Up 4 hours 0.0.0.0:2222->22/tcp base
[root@localhost mysql]# docker exec -it newdb /bin/bash
[root@60572f948087 /]# ls /var/lib/mysql/
aria_log.00000001 aria_log_control ib_logfile0 ib_logfile1 ibdata1 mydb mysql mysql.sock performance_schema test
应用镜像
#生成镜像
[root@localhost wordpress]# docker build -t csphere/wordpress:4.2 .
[root@localhost wordpress]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
csphere/wordpress 4.2 8d6e719b8a8f 2 minutes ago 747.5 MB
#生成容器
[root@localhost wordpress]# docker run -it -p 80:80 --name wordpress -e WORDPRESS_DB_HOST=192.168.20.106 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2
查看容器启动状态成功后,访问ip即可,应该出现wordpress应用安装界面
实际上最后一步生成容器失败,连接mysql失败,暂时未找到原因。错误信息如下:
[root@localhost docker-training]# docker run -it -p 80:80 --name wordpress -e WORDPRESS_DB_HOST=192.168.20.106 -e WORDPRESS_DB_USER=admin -e WORDPRESS_DB_PASSWORD=csphere2015 csphere/wordpress:4.2
PHP Notice: Undefined offset: 1 in - on line 4
PHP Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on '192.168.20.106' (113) in - on line 7
MySQL Connection Error: (2003) Can't connect to MySQL server on '192.168.20.106' (113)
PHP Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on '192.168.20.106' (113) in - on line 7
MySQL Connection Error: (2003) Can't connect to MySQL server on '192.168.20.106' (113)
使用本地工具访问mysql连接成功,检查帐号密码OK……我的虚拟机是centos7,如果有人碰到同样的问题,欢迎讨论!