docker 实战:利用 docker 搭建服务
用 docker
安装需要用到的服务实在是太方便了,我们下面介绍一些好的轮子来用。
- web-vscode: 搭建一个web版的vscode,不过实测下来不是特别好用。很多
vscode
上的插件用不了 - redis web: web端的web管理工具,不过配置起来稍微有点麻烦。
- lamp: php基础开发环境
# web-vscode 安装
#创建
docker rm -f vscode
docker run \
# 后台运行
-d \
# 总是运行
--restart=always \
# Alias
--name web_vscode \
# hostname
-h vscode \
# 一定要以root账号运行,不然会报 Permission denied
-u root \
# 所有端口映射,而不要是127.0.0.1
-p 8086:8080 \
-v "${HOME}/.local/share/code-server:/home/coder/.local/share/code-server" \
# 密码是通过环境变量加入的
-e PASSWORD=mycode \
# $PWD 是当前希望项目放置的位置
-v "$PWD:/home/coder/project" \
# 镜像名,本地不存在的话,会从仓库拉取
codercom/code-server:v2
#查看
docker ps -l
docker logs web_vscode
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
运行成功后
info Server listening on http://0.0.0.0:8080
info - Password is 558ba38067432e3beddf1228
info - To use your own password, set the PASSWORD environment variable
info - To disable use `--auth none`
info - Not serving HTTPS
2
3
4
5
由于我们的 web-vscode 是运行在容器内的,所以很多环境我们需要自己配置,参考上述操作。
# redis web 管理工具
mac 上 redis 管理工具破解版不好找,但是在 github 上找到了一个开源的工具,非常方便。拿来用一下
docker pull erikdubbelboer/phpredisadmin
docker run --rm -itd -e REDIS_1_HOST=redis -e REDIS_1_NAME=myredis -p 8015:80 --link redis-test:redis --name redisadmin erikdubbelboer/phpredisadmin
# 需要注意的是 REDIS_1_HOST 填写的是自己 redis 服务器的地址
# 这里我采用的是容器链接的方式
2
3
4
# redis & redis可视化工具
docker pull redis
docker run -p 127.0.0.1:6379:6379 -d --name redis redis --requirepass "mypassword"
# 查看redis的ip地址 172.17.0.2
docker inspect redis | grep -ai 'ipa'
docker run --rm -it -e REDIS_1_HOST=172.17.0.2 -e REDIS_1_NAME=redis -p 80:80 erikdubbelboer/phpredisadmin
2
3
4
5
6
7
# php LAMP 环境搭建
利用 docker
搭建 LAMP 环境本身应该是比较省力气的方式,但是网上 CSDN
之类的教程很多都是复制黏贴,很多实现不了,优质教程很少。目前发现了国外的一个 linode
的 docke r镜像,有非常详细的教程,很好用。
linode 详细配置,请看
- https://hub.docker.com/r/linode/lamp
- https://www.linode.com/docs/applications/containers/how-to-install-docker-and-deploy-a-lamp-stack/
# docker 安装
docker 环境搭建请看我的 Centos 环境配置(持续总结) (opens new window) 这一篇教程
# 拉取docker镜像
# docker search -s 10 lamp
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mattrayner/lamp A simple LAMP docker image running the prere… 211 [OK]
linode/lamp LAMP on Ubuntu 14.04.1 LTS Container 178
... linode 的镜像 star数还是比较高的
# sudo docker pull linode/lamp
2
3
4
5
6
7
# 生成配置文件目录
这一步很多教程里面都缺失的一部分,lamp 的很多文件需要映射到宿主机,方法后面的配置。
# 启动进入容器
$ docker run -itd --name lamp linode/lamp:latest /bin/bash
# 将需要映射的文件拷贝出来,主要就是 apache2, mysql的配置文件 和 www文件夹
.
├── apache2.conf
├── my.cnf
└── www
├── example.com
│ ├── backups
│ ├── log
│ └── public_html
└── html
└── index.html
$ docker cp lamp:/var/www /Users/xxx/Documents/Dockerfile/lamp/www
$ docker cp lamp:/etc/apache2/apache2.conf /Users/xxx/Documents/Dockerfile/lamp/apache2.conf
$ docker cp lamp:/etc/mysql/my.cnf /Users/xxx/Documents/Dockerfile/lamp/my.cnf
# 删除旧的容器
$ docker container rm -f lamp
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 启动容器
sudo docker run -p 8010:80 -p 3309:3306 \
-v /Users/ppsteven/Documents/Dockerfile/lamp/www:/var/www \
-v /Users/ppsteven/Documents/Dockerfile/lamp/apache2.conf:/etc/apache2/apache2.conf \
-v /Users/ppsteven/Documents/Dockerfile/lamp/my.cnf:/etc/mysql/my.cnf \
--name lamp \
-itd linode/lamp:latest /bin/bash
2
3
4
5
6
# 启动 Apache 和 MySQL 服务
容器内部的 Apache 和 MySQL 服务是默认不自动启动的(当 LAMP 重启的时候需要注意查看这两个服务是否也启动了)
service apache2 start
service mysql start
2
# MySQL 初始化配置
这一步是很多地方没有提的,linode 的 MySQL 是有默认密码,Admin2015
。我们需要初始化对于 MySQL 数据库的配置。
参考:https://zhuanlan.zhihu.com/p/111605563
# 如下命令即可
mysql_secure_installation
按下回城键你会看见结尾如下的对话。
Enter current password for root (enter for none): #
Set root password? # 是否设置root用户密码 y
New password: # 新密码
Re-enter new password:
Remove anonymous users? [Y/n] # 是否删除匿名用户,回车 y
Disallow root login remotely? [Y/n] # 是否禁止root远程登录
Remove test database and access to it? [Y/n] # 是否删除test数据库
Reload privilege tables now? [Y/n] # 是否重新加载权限表 y
All done! If you’ve completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
# 测试登录
mysql -uroot -p"自己的数据库密码"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 测试是否成功
游览器输入 http://localhost:8010/ ,看到如下信息,表示成功
The Docker LAMP stack is working.
The configuration information can be found here or here
This index.html file is located in the "/var/www/example.com/public_html" directory.
2
3
# MySQL 8.0 安装
docker pull mysql:8.0 # 拉取镜像
docker run -p 3307:3306 --name mysql8.0 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0 # 启动容器
docker exec -it mysql8.0 bash # 进入容器
mysql -uroot -p # 登录mysql
alter user 'root'@'%' identified with mysql_native_password by '我的密码'; # 修改三点: 1.运行外部访问 2.MySQL加密方式 3.密码
2
3
4
5
# 排查问题方法
# docker 退出
# 千万不要用 exit,容器会跟着退出的
CTRL + P + Q
2
# docker ip查看
进入容器查看,这种方法可能会失败,因为可能容器的系统中没有对应的命令工具
docker exec -ti <NAME OR ID> /bin/bash # ifconfig/ip addr
1
2最正统的写法(可以写在 shell 脚本里)
# docker inspect --format '{{ .NetworkSettings.IPAddress }}' 2e23d01384ac 172.17.0.2
1
2最简单查询的写法
$ docker inspect lamp | grep -ai 'ipa' "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAMConfig": null, "IPAddress": "172.17.0.2",
1
2
3
4
5
# docker 日志排查(tail -f /dev/null)
利用 tail -f /dev/null
长命令阻塞,不让容器立马结束,这种用法非常好用
docker exec -ti <NAME OR ID> tail -f /dev/null
# 前端环境打包
使用Docker进行前端环境打包,解决了不同系统环境下的不一致问题。由于使用Docker镜像编译静态文件,所以时间可能比较耗时。
FROM artifactory.momenta.works/docker/node:lts-alpine AS builder
WORKDIR /
COPY package.json /
# yarn 安装使用的版本依赖
COPY yarn.lock /
RUN yarn install
COPY . /
RUN yarn build
FROM nginx:alpine AS app
# nginx 配置
COPY nginx /etc/nginx/
COPY build/ /usr/share/nginx/html
EXPOSE 80
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
为了方便起见,也可以在本地编译后,打包至容器中。
FROM nginx:alpine AS app
# nginx 配置
COPY nginx /etc/nginx/
COPY build/ /usr/share/nginx/html
EXPOSE 80
2
3
4
5
6
7
8