这篇文章上次修改于 902 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

1. Install Docker Engine

安装

wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm
yum install docker-ce-selinux-17.03.3.ce-1.el7.noarch.rpm

wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install docker-ce-19.03.9-3.el7.x86_64.rpm -y

wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm
yum install docker-ce-19.03.9-3.el7.x86_64.rpm

systemctl start docker

验证

docker run hello-world
docker run -i -t ubuntu /bin/bash

允许其他非特权用户也使用 docker 命令

groupadd docker
usermod -aG docker $USER

开机自启动

systemctl enable docker

简单示例

yum install git -y

git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app

docker build --tag bulletinboard:1.0 .

docker run --publish 80:8080 --detach --name bb bulletinboard:1.0

# 删除
docker rm --force bb

2. 安装 docker cli

wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-19.03.9-3.el7.x86_64.rpm
yum install docker-ce-cli-19.03.9-3.el7.x86_64.rpm -y

3. Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

验证

Install Docker Compose

Install command completion

 sudo curl -L https://raw.githubusercontent.com/docker/compose/1.26.2/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

简单示例

https://docs.docker.com/compose/gettingstarted/

4. 最佳实践

DockerFile 中,将多个 RUN 命令合并为一行,可避免 docker 镜像过大。

5. 示例

Dockerfile 编写:

FROM andals/python:multi

LABEL name="Lightpower Image"
LABEL vendor="Lighthouse"

COPY pkg/ /build/pkg/
COPY script/ /build/script/

RUN /build/script/build_image.sh

CMD /build/script/init.sh

其中:

  • RUN 执行命令
  • CMD 执行容器启动命令

运行:

docker run -d --name=lightpower \ 
--volumes-from=data-home -v $HOME/data/lightpower:/data/lightpower \
-p 8001:8001 -e "REGION_CONF_SUFFIX=mac" lighthouse/lightpower:${lightpowerVer}

其中,

  • --volumes-from 从另一个容器当中挂载容器中已经创建好的数据卷。data-home 便是预先创建好的 centos 容器。
  • -v 将主机内目录映射到容器内目录

6 相关命令

以 xremote 身份进入 docker:

docker exec -it -u xremote "$XREMOTE_CONTAINER_NAME" bash

不进入 docker 时,以 xremote 身份在 docker 内部执行命令:

docker exec -i -u xremote "$XREMOTE_CONTAINER_NAME" \
        bash -c "source /home/xremote/.cargo/env && cd client && bash release_build.sh build_deb"

参考

  1. https://docs.docker.com/engine/install/centos/#install-from-a-package
  2. https://docs.docker.com/compose/install/
  3. https://docs.docker.com/develop/dev-best-practices/