Skip to content
thawk edited this page Mar 15, 2020 · 12 revisions

Docker

1. Docker使用

1.1. 清理空间

docker system prune

1.2. 运行X11程序

IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
xhost + $IP
docker run -d --name firefox -e DISPLAY=$IP:0 images

2. 容器开发

2.1. lint

2.2. 自动为image打label

可以通过 https://microbadger.com 展示层数、大小等信息。

  • 在Dockerfile中加入:

    ARG BUILD_DATE
    ARG VCS_REF
    
    LABEL org.label-schema.build-date=$BUILD_DATE \
          org.label-schema.vcs-url="https://github.com/rossf7/label-schema-automated-build.git" \
          org.label-schema.vcs-ref=$VCS_REF \
          org.label-schema.schema-version="1.0.0-rc1"
  • hooks/build

    #!/bin/bash
    
    # $IMAGE_NAME var is injected into the build so the tag is correct.
    
    echo "Build hook running"
    docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \
                 --build-arg VCS_REF=`git rev-parse --short HEAD` \
                 -t $IMAGE_NAME .

3. 一些可用的工具容器

3.1. 命令行镜像

git
alias git="docker run -ti --rm -v \$(pwd):/git bwits/docker-git-alpine"
asciidoctor
docker run -it -v <your directory>:/documents/ asciidoctor/docker-asciidoctor
  • 基本用法

    • asciidoctor sample.adoc

    • asciidoctor-pdf sample.adoc

    • asciidoctor-epub3 sample.adoc

  • 支持diagrams

    • asciidoctor -r asciidoctor-diagram sample-with-diagram.adoc

    • asciidoctor-pdf -r asciidoctor-diagram sample-with-diagram.adoc

    • asciidoctor-epub3 -r asciidoctor-diagram sample-with-diagram.adoc

  • 使用backends。backends

    -T /asciidoctor-backends/<引擎>-T $BACKENDS/<引擎>

    asciidoctor -T /asciidoctor-backends/slim/dzslides myFile.adoc
    #or
    asciidoctor -T $BACKENDS/slim/dzslides myFile.adoc
  • fopub

    先生成docbook,再调用fopub

    asciidoctor -b docbook sample.adoc
    fopub sample.xml
  • asciidoctor-confluence

    asciidoctor-confluence --host HOSTNAME --spaceKey SPACEKEY --title TITLE --username USER --password PASSWORD sample.adoc
  • 批处理

    docker run --rm -v $(pwd):/documents/ asciidoctor/docker-asciidoctor asciidoctor-pdf index.adoc

3.2. GUI镜像

需要先

  • Install the latest XQuartz X11 server and run it

  • Activate the option ‘Allow connections from network clients’ in XQuartz settings

  • Quit & restart XQuartz (to activate the setting)

  • xhost + 127.0.0.1

vscode
docker run -e DISPLAY=host.docker.internal:0 -v $HOME:/home/user --name vscode jessfraz/vscode
gimp
docker run -e DISPLAY=host.docker.internal:0 -v $HOME/Pictures:/root/Picture --name gimp jessfraz/gimp

4. Rancher

4.1. 在FreeNAS上,通过iohyve安装RancherOS

由于FreeNAS 11的GUI创建的虚拟机只支持UEFI启动,而RancherOS的ISO不支持UEFI启动,因此只能在命令行下通过iohyve安装。

4.1.1. 创建虚拟机,启动到RancherOS的光盘

# rancheros的下载地址: https://releases.rancher.com/os/latest/rancheros.iso

# 在指定pool下创建iohyve pool并建立相应的mount
sudo iohyve setup pool=main kmod=1 net=bge0

# 把iso拷贝到iohyve内部存储上
sudo iohyve cpiso /mnt/main/media/softwares/linux_iso/rancheros.iso
# 或者直接下载
#sudo iohyve fetch https://releases.rancher.com/os/latest/rancheros.iso

# 创建虚拟机,2G硬盘
sudo iohyve create RancherOS 2G
sudo iohyve set RancherOS loader=grub-bhyve ram=2G cpu=1 os=debian boot=1
# 安装操作系统
sudo iohyve install RancherOS rancheros.iso

在另一个窗口运行命令,打开虚拟机的控制台,配置GRUB

sudo iohyve console RancherOS

在控制台上按回车,在GRUB提示符下输入:

grub> set root=(cd0,msdos1)
grub> linux /boot/vm<TAB> ro rancher.password=rancher
grub> initrd /boot/initrd<TAB>
grub> boot

目前版本用的是:

  • /boot/vmlinuz-4.9.80-rancher

  • initrd /boot/initrd-v1.3.0

4.1.2. 安装RancherOS到虚拟机中

vi cloud-config.yml
# cloud-config
ssh_authorized_keys:
  - ssh-rsa <your pubkey here>
sudo ros install -c cloud-config.yml -d /dev/sda

重启会失败,这是正常的。

4.1.3. 设置RancherOS的启动方式

# 确认虚拟机已经停止
sudo iohyve list

设置启动方式:

sudo iohyve set RancherOS os=custom
sudo iohyve set RancherOS boot=1

删除 /mnt/iohyve/RancherOS/device.map 中,除了 hd0 以外的行。

创建 grub.cfg

set root=(hd0,1)
linux /boot/vmlinuz-4.9.80-rancher printk.devkmsg=on rancher.state.dev=LABEL=RANCHER_STATE rancher.state.wait console=tty0
initrd /boot/initrd-v1.3.0
boot

此时,虚拟机应该可以成功启动。

4.1.4. 加上一个专用的卷

  1. 创建一个zvol:

    main/vdisks/docker : 20G

.

sudo iohyve set RancherOS pcidev:1=ahci-hd,/dev/zvol/main/vdisks/docker

4.2. 配置Rancher

  • 设置虚拟机的地址

    sudo ros config set rancher.network.interfaces.eth0.address 172.31.0.70/24
    sudo ros config set rancher.network.interfaces.eth0.gateway 172.31.0.253
    sudo ros config set rancher.network.interfaces.eth0.mtu 1500
    sudo ros config set rancher.network.interfaces.eth0.dhcp false
    #  重启生效
    sudo reboot
  • 在虚拟机中安装rancher服务

    sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher
  • 配置Rancher

    • 打开管理页面,地址为上面查到的虚拟机地址,端口为8080

    • 增加要管理的主机

      点击Add a host,填写与上面相同的IP和端口

4.3. 升级RancherOS

sudo ros os upgrade

4.4. 允许远程通过TCP连接docker服务

sudo ros config set rancher.docker.extra_args "['-H','tcp://0.0.0.0:2375']"

5. podman

不需要daemon,代替docker运行容器的工具,是libpod的一部分。

8. 使用RedHat的容器

$ docker login registry.redhat.io
Username: ${REGISTRY-SERVICE-ACCOUNT-USERNAME}
Password: ${REGISTRY-SERVICE-ACCOUNT-PASSWORD}
Login Succeeded!

$ docker pull registry.redhat.io/rhel7
Clone this wiki locally