利用 Docker 搭建 基于 Ubuntu18.4 的 GoCV 环境

使用说明

# 在桌面创建 gocv_docker 目录  
cd ~/Desktop && mkdir gocv_docker && cd ~/Desktop/gocv_docker

# 创建 Dockerfile 并将下文 Dockerfile 内容copy
touch Dockerfile

# 下载go1.17版本
wget --no-check-certificate "https://go.dev/dl/go1.17.10.linux-amd64.tar.gz"

# 构建 image
docker build -t gocv4.5_ubuntu18.4_go1.17_gocv0.30.0 . 

# 运行container 并映射文件,端口
# 容器开机自动重启 refer: https://www.jianshu.com/p/36f63f57b05d
docker run -d -p 9000-9010:9000-9010 \
   -v /Users/flybird1971/docker/gocv:/home/gocv/gocv_work \
   --name gocv4.5_ubuntu18.4_go1.17_gocv0.30.0 \
   --restart=always \
   --privileged=true gocv4.5_ubuntu18.4_go1.17_gocv0.30.0  /usr/sbin/init

# 进入container 
docker exec -it gocv4.5_ubuntu18.4_go1.17_gocv0.30.0  /bin/bash

Dockerfile

默认创建 gocv 账户运行

# base image
# FROM ubuntu:18.04

# mac os m1芯片
# 如果非 m1芯片,可以将 --platform=linux/amd64 移除
# refer: https://surfthedream.com.au/running-matchstick-for-testing-the-graph-on-mac-m1/
FROM --platform=linux/amd64 ubuntu:18.04

# description
LABEL maintainer="flybird1971@gmail.com"
LABEL version="1.0"
LABEL description="base ubuntu18.04、golang1.17.10、gocv0.30.0、opencv4.5.5"

# 设置国内源(阿里)
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo " " >> /etc/apt/sources.list && \
    echo "# 阿里源 " >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list

# 更新源
RUN apt-get update -y && apt-get -y upgrade

# 安装系统常用工具
RUN apt-get install -y openssl && \
    apt-get install -y libssl-dev && \
    apt-get -y install make && \
    apt-get install -y sudo  

RUN apt-get install -y wget && \
    apt install -y vim && \
    apt-get install -y curl && \    
    apt-get install -y tree && \
    apt-get install -y git

# 设置 root 密码
RUN echo "root:123456" | chpasswd
# 新增 gocv 用户
RUN useradd --create-home --no-log-init --shell /bin/bash gocv
RUN echo 'gocv:123456' | chpasswd

# 创建 gocv 工作目录
RUN mkdir /home/gocv/go && mkdir /home/gocv/gocv_work 

# 安装 golang
# wget --no-check-certificate "https://go.dev/dl/go1.18.2.linux-amd64.tar.gz"
# wget --no-check-certificate "https://go.dev/dl/go1.17.10.linux-amd64.tar.gz"
COPY go1.17.10.linux-amd64.tar.gz /tmp/
RUN cd /tmp/ && tar -xzf go1.17.10.linux-amd64.tar.gz && \
    mv go /usr/local && \
    echo "" >> /etc/profile && \
    echo "# golang" >> /etc/profile && \
    echo "GOROOT=/usr/local/go" >> /etc/profile && \
    echo "GOPATH=/home/gocv/go" >> /etc/profile && \
    echo "PATH=\$GOPATH/bin:\$GOROOT/bin:\$PATH" >> /etc/profile

# 当前导入环境变量
SHELL ["/bin/bash", "-c"]
RUN source /etc/profile
ENV GOROOT=/usr/local/go
ENV GOPATH=/home/gocv/go
ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH

# golang 设置国内代理
RUN go version && go env -w GOPROXY=https://goproxy.cn,direct

# 下载 gocv 库
RUN go get -u -d gocv.io/x/gocv@v0.30.0

# 安装 OpenCV 4.5.5
WORKDIR $GOPATH/pkg/mod/gocv.io/x/gocv@v0.30.0
RUN make -j4 deps

# 配置命令行代理
# 如果下个命令 make -j4 download 无法成功下载,则需要配置代理
# ENV https_proxy=http://192.168.0.105:7890
# ENV http_proxy=http://192.168.0.105:7890
# ENV all_proxy=http://192.168.0.105:7890

# 安装 OpenCV 4.5.5
RUN make -j4 download
RUN make -j4 build
RUN make -j4 sudo_install

# 测试gocv
RUN go run ./cmd/version/main.go

# gopath 权限切换为 gocv 用户
RUN chown -R gocv:root /home/gocv/go

# 切换用户
USER gocv
WORKDIR /home/gocv/gocv_work

# 创建 自动启动脚本
RUN echo '#!/bin/sh' >> /home/gocv/ini.sh
RUN echo "source /etc/profile && go version" >> /home/gocv/ini.sh
RUN echo "while true; do echo hello world; sleep 1; done" >> /home/gocv/ini.sh
RUN chmod +x /home/gocv/ini.sh

ENTRYPOINT [ "/home/gocv/ini.sh" ]

你可能感兴趣的