Gentoo镜像

DockerHub Gentoo 提供了 Gentoo Linux 官方镜像:

Gentoo使用 OpenRC 提供了轻量级的 OpenRC初始化系统openrc-init 可以在Docker容器中作为 Docker init进程管理器

基础运行 gentoo-base

Fedora镜像(采用tini替代systemd) 类似,我们需要一个轻量级的的 Docker init进程管理器 。显然采用 Systemd进程管理器 对于容器过于沉重,而且容器通常是直接运行程序,无需独立的 Docker init进程管理器 。只不过,为了适应我们的开发环境,我们构建一个通过 init 来运行多个程序的富容器。

Gentoo 默认的 OpenRC 是一个轻量级的解决方案,不仅被Gentoo作为默认进程管理器,也是轻量级发行版 Alpine Linux 默认进程管理器。这个 sys-apps/openr 已经默认在官方镜像中提供,只不过一般不会启用(因为容器都是轻量级的单个应用运行),常规的容器最后执行命令就是直接应用程序,例如 /bin/bash 或者 /usr/sbin/nginx

非常幸运的是 OpenRC 和Docker兼容性极佳,可以轻而易举在Docker中采用:

基础Gentoo镜像Dockerfile
# name the portage image
FROM gentoo/portage:latest as portage

# based on stage3 image
FROM gentoo/stage3:latest

# copy the entire portage volume in
COPY --from=portage /var/db/repos/gentoo /var/db/repos/gentoo

# continue with image build ...
RUN emerge -qv sys-apps/openrc

CMD ["/sbin/init"]
  • 构建 gentoo-base 镜像:

构建基础Gentoo镜像Dockerfile
docker build -t gentoo-base .
  • 运行 gentoo-base 镜像:

运行gentoo-base容器
docker run -dt --name gentoo-base --hostname gentoo-base gentoo-base
  • 连接到 gentoo-base 容器内:

通过docker exec运行容器内部bash
docker exec -it gentoo-base /bin/bash

基础运行 gentoo-base-plus

在上述最为简单的 Gentoo 基础上,按照 在Gentoo上运行Gentoo(容器) 的实践定制一个包含工具、本地化等配置的镜像,并启用 ssh 服务:

在基础Gentoo镜像上增加工具、更新以及部署ssh的Dockerfile
# name the portage image
FROM gentoo/portage:latest as portage

# based on stage3 image
FROM gentoo/stage3:latest

# copy the entire portage volume in
COPY --from=portage /var/db/repos/gentoo /var/db/repos/gentoo

# config make.conf:  use chinese mirror
RUN echo 'GENTOO_MIRRORS="http://mirrors.aliyun.com/gentoo http://distfiles.gentoo.org http://www.ibiblio.org/pub/Linux/distributions/gentoo"' >> /etc/portage/make.conf
RUN sed -i 's/\-O2 \-pipe/\-march=native \-O2 \-pipe/g' /etc/portage/make.conf

# config gentoo.conf: use chinese repos
RUN mkdir /etc/portage/repos.conf
RUN cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
RUN sed -i 's/rsync.gentoo.org/rsync.cn.gentoo.org/g' /etc/portage/repos.conf/gentoo.conf

# timezone
RUN echo "Asia/Shanghai" > /etc/timezone
RUN emerge --config sys-libs/timezone-data

# sync
RUN emaint -a sync

# USE for cpu
RUN emerge -qv app-portage/cpuid2cpuflags
RUN echo "*/* $(cpuid2cpuflags)" > /etc/portage/package.use/00cpu-flags

# upgrade: emerge quiet (-q)
RUN emerge -qvuDN @world

# continue with image build ...
RUN emerge -qv sys-apps/openrc
RUN emerge -qv sys-apps/mlocate
RUN emerge -qv net-dns/bind-tools
RUN emerge -qv net-analyzer/netcat
RUN emerge -qv app-editors/neovim
RUN emerge -qv app-admin/sudo
RUN emerge -qv app-misc/tmux

# sshd
RUN rc-update add sshd default

# add account "admin" and give sudo privilege
RUN groupadd -g 1001 admin
RUN useradd -g 1001 -u 1001 -d /home/admin -m admin
RUN usermod -aG wheel admin
RUN echo "%wheel        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

# Add ssh public key for login
RUN mkdir -p /home/admin/.ssh
COPY authorized_keys /home/admin/.ssh/authorized_keys
RUN chown -R admin:admin /home/admin/.ssh
RUN chmod 600 /home/admin/.ssh/authorized_keys
RUN chmod 700 /home/admin/.ssh

# run service when container started - sshd
EXPOSE 22:1122

CMD ["/sbin/init"]
  • 构建 gentoo-base-plus 镜像:

构建 gentoo-base-plus 镜像Dockerfile
docker build -t gentoo-base-plus .
  • 运行 gentoo-base-plus 镜像:

运行 gentoo-base-plus 容器
docker run -dt -p 1122:22 \
    --name gentoo-base-plus --hostname gentoo-base-plus gentoo-base-plus
  • 连接到 gentoo-base-plus 容器内:

通过docker exec运行 gentoo-base-plus 容器内部bash
docker exec -it gentoo-base-plus /bin/bash

开发环境 gentoo-dev

备注

开发环境的构建 Dockerfile 将随着我的开发学习以及工作不断调整和完善

  • gentoo-base-plus 增加开发工具安装的 Dockerfile (逐步完善):

gentoo-base-plus 基础上增加开发工具安装
# name the portage image
FROM gentoo/portage:latest as portage

# based on stage3 image
FROM gentoo/stage3:latest

# copy the entire portage volume in
COPY --from=portage /var/db/repos/gentoo /var/db/repos/gentoo

# config make.conf:  use chinese mirror
RUN echo 'GENTOO_MIRRORS="http://mirrors.aliyun.com/gentoo http://distfiles.gentoo.org http://www.ibiblio.org/pub/Linux/distributions/gentoo"' >> /etc/portage/make.conf
RUN sed -i 's/\-O2 \-pipe/\-march=native \-O2 \-pipe/g' /etc/portage/make.conf

# config gentoo.conf: use chinese repos
RUN mkdir /etc/portage/repos.conf
RUN cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf
RUN sed -i 's/rsync.gentoo.org/rsync.cn.gentoo.org/g' /etc/portage/repos.conf/gentoo.conf

# timezone
RUN echo "Asia/Shanghai" > /etc/timezone
RUN emerge --config sys-libs/timezone-data

# sync
RUN emaint -a sync

# USE for cpu
RUN emerge -qv app-portage/cpuid2cpuflags
RUN echo "*/* $(cpuid2cpuflags)" > /etc/portage/package.use/00cpu-flags

# upgrade: emerge quiet (-q)
RUN emerge -qvuDN @world

# continue with image build ...
RUN emerge -qv sys-apps/openrc
RUN emerge -qv sys-apps/mlocate
RUN emerge -qv net-dns/bind-tools
RUN emerge -qv net-analyzer/netcat
RUN emerge -qv app-editors/neovim
RUN emerge -qv app-admin/sudo
RUN emerge -qv app-misc/tmux

# sshd
RUN rc-update add sshd default

# add account "admin" and give sudo privilege
RUN groupadd -g 1001 admin
RUN useradd -g 1001 -u 1001 -d /home/admin -m admin
RUN usermod -aG wheel admin
RUN echo "%wheel        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers

# Add ssh public key for login
RUN mkdir -p /home/admin/.ssh
COPY authorized_keys /home/admin/.ssh/authorized_keys
RUN chown -R admin:admin /home/admin/.ssh
RUN chmod 600 /home/admin/.ssh/authorized_keys
RUN chmod 700 /home/admin/.ssh

# 墙内RVM安装需要梯子,在Dockerfile中注入代理配置
#ENV HTTP_PROXY "http://192.168.6.200:3128"
#ENV HTTPS_PROXY "http://192.168.6.200:3128"
#ENV NO_PROXY "*.baidu.com,.taobao.com"

# Ruby Rails (master)
RUN gpg2 --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
RUN curl -sSL https://get.rvm.io | bash -s master --rails

# expose ssh/http/https AND some dev ports
EXPOSE 22
EXPOSE 80
EXPOSE 443
#EXPOSE 3000
#EXPOSE 8000

CMD ["/sbin/init"]

备注

墙内使用 RVM 需要梯子,所以结合 配置Docker使用代理Squid父级socks代理 实现翻墙。上面的 Dockerfile 配置中通过添加环境变量使得容器镜像构建时可以使用代理服务器。

如果没有GFW干扰,可以去除代理配置;请按照实际情况调整配置内容

  • 构建 gentoo-dev 镜像:

构建 gentoo-dev Dockerfile镜像
docker build -t gentoo-dev .
  • 运行 gentoo-dev 镜像:

运行 gentoo-dev 容器
docker run -dt -p 1122:22 -p 1180:80 -p 11443:443 \
    --name gentoo-dev --hostname gentoo-dev gentoo-dev
  • 连接到 gentoo-dev 容器内:

通过docker exec运行 gentoo-dev 容器内部bash
docker exec -it gentoo-dev /bin/bash

参考