Docker 相关

mattrayner/lamp 配置和安装 redis 扩展 (参考 Docker 安装 的目录挂载)

新建 Dockerfile
1
2
3
4
5
FROM mattrayner/lamp:latest-1804
RUN add-apt-repository -y ppa:ondrej/php && \
apt-get update && \
apt-get -y upgrade && \
apt-get -y install php-redis
生成镜像
1
docker build -t sevming/lamp:1.1 .
宿主机新建 mysqlapp 文件夹
1
2
mkdir /d/docker/mysql
mkdir /d/docker/app
新建 docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: '3'
services:
lamp:
image: sevming/lamp:1.1
container_name: lamp
ports:
- 80:80
- 3306:3306
volumes:
- /data/mysql:/var/lib/mysql
- /data/app:/app
redis:
image: redis
container_name: redis
restart: always
ports:
- 6379:6379
volumes:
- /data/redis/data:/data
command:
redis-server --appendonly yes --requirepass 123456
启动
1
docker-compose up -d

Docker CentOS

新建 Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
生成镜像镜像
1
docker build --rm -t sevming/centos7:0.1 .
启用 systemd 容器
1
docker run -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro sevming/centos7:0.1
常见问题
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 问题:
Failed to mount tmpfs at /run: Operation not permitted
Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
[!!!!!!] Failed to mount API filesystems, freezing.
# 解决方法:
docker run -it -d --name=centos7 --privileged=true -p 80:80 -v /sys/fs/cgroup:/sys/fs/cgroup:ro sevming/centos7:0.1
docker exec -it centos7 /bin/bash
systemctl status

# 问题:
Error response from daemon: cgroups: cannot find cgroup mount destination: unknown.
# 解决方法:
# 1.进入虚拟机
docker-machine ssh
# 2.创建 systemd
sudo mkdir /sys/fs/cgroup/systemd
# 3.挂载一棵cgroup树,但不关联任何subsystem
sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd

# 若提示 mkdir: can't create directory '/sys/fs/cgroup/systemd': Read-only file system
# 进入不能创建目录的外层目录
cd /sys/fs
# 执行命令
mount -o remount,rw cgroup/

# 问题 (docker-compose 启动 mysql 错误):
[ERROR] Could not open file '/var/log/mysql/error.log' for error logging: Permission denied
# 解决方案一 (宿主机创建 mysql 用户和组):
# 添加 mysql 组
groupadd mysql
# 创建 mysql 用户并加入 mysql 组
useradd mysql -g mysql
# 禁止 mysql 用户登录
vim /etc/passwd
mysql:x:1005:1005::/home/mysql:/sbin/nologin
# 解决方案二 (配置 docker-compose.yml):
services:
mysql:
# 指定mysql容器启动后默认执行的命令
command:
- "--default-authentication-plugin=mysql_native_password"
# 启动容器后需要执行的命令
entrypoint: bash -c "chown -R mysql:mysql /var/log/mysql && exec /entrypoint.sh mysqld"

Docker Node + Git 运行并部署 Hexo 项目

Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM node:12

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster main non-free contrib' > /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb http://mirrors.aliyun.com/debian-security buster/updates main' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib' >> /etc/apt/sources.list \
&& echo 'deb-src http://mirrors.aliyun.com/debian-security buster/updates main' >> /etc/apt/sources.list

RUN apt update && apt install -y wget && apt install -y vim && apt install -y iputils-ping && apt install -y git
生成镜像
1
docker build -t sevming/node12:0.1 .
运行
1
2
3
4
# 运行容器
docker run -itd --name node -p 4000:4000 -v /Users/sev/project/docker:/home/node sevming/node12:0.1
# 访问容器
docker exec -it node /bin/bash
0%