1、背景

        本机是ubuntu18.04,有一个evo 的测评工程需要迁移到他机运行,但是为了避免他机环境配置不一致(主要是麻烦)、以及与他机应用程序环境配置冲突等原因,决定基于docker部署。

2、在本机、他机安装docker环境:

        具体可参考文章:https://www.jianshu.com/p/cef32b054968。 下面在复述下安装步骤:

        (1)查看ubuntu版本

root@ubuntu:~# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS" root@ubuntu:~# uname -a Linux ubuntu 4.15.0-29-generic #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

        (2)先卸载可能存在的旧版本           

sudo apt-get remove docker docker-engine docker-ce docker.io

        (3)更新apt包

                可以先更新apt源:        

备份

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak


清华源

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse


阿里源

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse


sudo apt-get update
    sudo apt-get upgrade

        (4) 切换到root权限

                        sudo -i

        (5)安装以下包以使apt可以通过HTTPS使用存储库(repository):

                    apt-get install apt-transport-https \ ca-certificates \ curl \ software-properties-common

        (6)添加Docker官方的GPG密钥

                curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

                安装成功会提示OK

        (7)安装稳定存储库                

                sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu                 \ $(lsb_release -cs) \stable"

        (8)再更新一下apt包索引

                apt-get update

        (9)安装完成 检验是否安装成功

                docker version:显示docker版本信息

                docker run hello-world:运行下hello-world检验下是否运行OK

        (10) Docker 常用命令          

                        启动docker
                                   sudo service docker start

                        停止docker
                                       sudo service docker stop

                        重启docker
                                        sudo service docker restart

                        列出Docker CLI命令
                                        docker
                                        docker container --help

                        显示Docker版本和信息
                                        docker --version
                                        docker version
                                        docker info

                        Execute Docker image
                                        docker run hello-world

                        列出镜像列表
                                        docker image ls

                        列出docker容器 (running, all, all in quiet mode)
                                        docker container ls
                                        docker container ls --all
                                        docker container ls -aq

3、本机下载ubuntu16.04 docker镜像

        从docker hub拉取的ubuntu18.04有问题(文件很小还没ubuntu16.04大,似乎不是原版。而且安装ok后,运行时提示python的莫名奇妙的错误),因此换为ubuntu16.04运行evo.

        docker pull ubuntu:16.04

4、在docker中配置evo所需要的环境

        基于ubuntu16.04镜像启动一个容器:docker run -it ubuntu:16.04 /bin/bash  (这样就启动一个容器啦,注意加载镜像不要使用镜像ID进行加载,要使用镜像名:版本号的方式,这样在后面容器保存后,他机加载新镜像时才会自动导入容器名字和版本号,要不然会是none.)

        安装python3

        更新pip源:

                       根目录创建.pip文件:mkdir ~/.pip

                        创建文件pip.conf:vim ~/.pip/pip.conf

                        点击“i”键,进入编辑模式,复制信息:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

5、把正在基于ubuntu16.04的实例容器保存为镜像

 


        下面是一个把正在运行的docker容器保存为新的镜像的命令:

sudo docker commit -m "evo_docker" -a "admin" 1683ab3e55f0 evo/ubuntu:v1
# 参数解释如下:
 -m: 提交的描述信息
 -a: 指定镜像作者
 1683ab3e55f0:容器 ID
evo/ubuntu:v1: 指定要创建的目标镜像名

 6、他机导入docker镜像:

        方式一:只是启动docker容器,并不进入docker容器

echo "Start to run docker container..."
    docker run --rm -t \
               --privileged \
               --name ${CONTAINER_NAME} \
               -e DOCKER_IMG=$IMG \
               -v $CODE_DIR:/workspace/ \
               -v /media:/media \
               -w /workspace \
               $DOCKER_IMAGE \
               /bin/bash -c "pwd && echo $EXEC_SHELL && $EXEC_SHELL"
    echo "finished to run docker container..."


### 注意:上述代码把/media下的挂载文件全部加载到docker容器中,这样便于和宿主机数据传递。可以挂载多个目录,如上所示,挂载了两个目录。

        方式二:启动并进入docker容器:

function start_docker() {
    filter_name="name=$CONTAINER_NAME"
    if [ -n "$(docker container ls -aq -f=${filter_name})" ]; then
        docker stop $CONTAINER_NAME >/dev/null 2>&1
        docker rm -f $CONTAINER_NAME >/dev/null 2>&1
    fi
    echo "Start to run docker container..."
    docker run -it \
               -d \
               --privileged \
               --name ${CONTAINER_NAME} \
               -e DOCKER_IMG=$IMG \
               -v $CODE_DIR:/workspace/ \
               -v /media:/media \
               -w /workspace \
               $DOCKER_IMAGE \
               /bin/bash #-c "pwd && echo $EXEC_SHELL && $EXEC_SHELL"
    echo "finished to run docker container..."
}