在docker-compose的构建过程中寻找一些安装Go软件包的帮助。特别是去科利。
设置:
docker-compose.yml
services:
crawler:
container_name: crawler
build: ./crawler/
working_dir: /go/src/crawler
volumes:
- ./data:/go/src/crawler/data
- ./crawler:/go/src/crawler
Dockerfile
FROM golang:1.7-alpine
ADD . /go/src/crawler
WORKDIR /go/src/crawler
RUN \
apk add --no-cache bash git openssh && \
go get github.com/golang/example/stringutil/... && \
go get github.com/gocolly/colly/... && \
go install
CMD ["go","run","collect.go"]
package main
import (
"fmt"
"github.com/golang/example/stringutil"
"github.com/gocolly/colly/v2"
)
func main() {
fmt.Println(stringutil.Reverse("!selpmaxe oG ,olleH"))
c := colly.NewCollector()
// Find and visit all links
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.Visit("http://go-colly.org/")
}
如果我在没有Colly的情况下运行项目,即只是"github.com/golang/example/stringutil"
,然后该项目将安装软件包并正确运行,但是通过Colly,我得到了:
OK: 34 MiB in 25 packages
package github.com/gocolly/colly
imports github.com/gocolly/colly/v2/debug: cannot find package "github.com/gocolly/colly/v2/debug" in any of:
/usr/local/go/src/github.com/gocolly/colly/v2/debug (from $GOROOT)
/go/src/github.com/gocolly/colly/v2/debug (from $GOPATH)
package github.com/gocolly/colly
imports github.com/gocolly/colly/v2/storage: cannot find package "github.com/gocolly/colly/v2/storage" in any of:
/usr/local/go/src/github.com/gocolly/colly/v2/storage (from $GOROOT)
/go/src/github.com/gocolly/colly/v2/storage (from $GOPATH)
package github.com/gocolly/colly/extensions
imports github.com/gocolly/colly/v2: cannot find package "github.com/gocolly/colly/v2" in any of:
/usr/local/go/src/github.com/gocolly/colly/v2 (from $GOROOT)
/go/src/github.com/gocolly/colly/v2 (from $GOPATH)
ERROR: Service 'crawler' failed to build: The command '/bin/sh -c apk add --no-cache bash git openssh && go get github.com/golang/example/stringutil/... && go get github.com/gocolly/colly/... && go install' returned a non-zero code: 1
我最初的想法是,挂载- ./crawler:/go/src/crawler
会覆盖软件包,但随后我对github.com/golang/example/stringutil
为何工作正常感到困惑。
答案 0 :(得分:0)
基本上,您的问题是您正在引用模块路径(v2
给出了该模块路径),在撰写本文时,该路径为seems to be broken。
但是,通过一些调整和更正,您的项目将运行:
main.go
package main
import (
"fmt"
// Note the version suffix "/v2" is gone
// This installs the v1.2.0 version, but apparently, this works, too
"github.com/gocolly/colly"
"github.com/golang/example/stringutil"
)
func main() {
fmt.Println(stringutil.Reverse("!selpmaxe oG ,olleH"))
// Limit the domains for the sake of the example
c := colly.NewCollector(colly.AllowedDomains("go-colly.org"))
// Find and visit all links
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.Visit("http://go-colly.org/")
}
现在只需拨打电话
go mod init stackoverflow.com/user3939059/crawler
(当然,您应该调整域和用户的名称)
Dockerfile
尽管您可以在基于“ golang”映像的Docker映像中执行go run main.go
,但这简直效率低下。最好静态编译应用程序,甚至可以使用空的暂存映像作为运行应用程序的基础:
FROM golang:alpine as builder
ADD . /go/src/crawler
WORKDIR /go/src/crawler
RUN \
apk add --no-cache tzdata && \
adduser -D -g '' appuser
# Statically compile our application
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -a -installsuffix cgo -o /crawler
FROM scratch
# Import the files required by the standard library from builder.
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Make sure the user can be identified by the docker daemon
COPY --from=builder /etc/passwd /etc/passwd
# Copy our static executable
COPY --from=builder /crawler /bin/crawler
# Use an unprivileged user.
USER appuser
# Run the binary.
CMD ["/bin/crawler"]
进行上述更改后,在docker build -t user3939059/crawler:latest .
之后,您可以运行图像:
$ docker run --rm user3939059/crawler
Hello, Go examples!
Visiting http://go-colly.org/
Visiting http://go-colly.org/docs/
Visiting http://go-colly.org/articles/
Visiting http://go-colly.org/services/
Visiting http://go-colly.org/datasets/
Visiting http://go-colly.org/data/factbase_transcripts.tar.bz2
Visiting http://go-colly.org/docs/examples/factbase/
Visiting http://go-colly.org/docs/introduction/install/
Visiting http://go-colly.org/docs/introduction/start/
Visiting http://go-colly.org/docs/introduction/configuration/
Visiting http://go-colly.org/docs/best_practices/debugging/
Visiting http://go-colly.org/docs/best_practices/distributed/
Visiting http://go-colly.org/docs/best_practices/storage/
Visiting http://go-colly.org/docs/best_practices/multi_collector/
Visiting http://go-colly.org/docs/best_practices/crawling/
Visiting http://go-colly.org/docs/best_practices/extensions/
Visiting http://go-colly.org/docs/examples/basic/
Visiting http://go-colly.org/docs/examples/error_handling/
Visiting http://go-colly.org/docs/examples/login/
Visiting http://go-colly.org/docs/examples/max_depth/
Visiting http://go-colly.org/docs/examples/multipart/
Visiting http://go-colly.org/docs/examples/parallel/
Visiting http://go-colly.org/docs/examples/proxy_switcher/
Visiting http://go-colly.org/docs/examples/queue/
Visiting http://go-colly.org/docs/examples/random_delay/
Visiting http://go-colly.org/docs/examples/rate_limit/
Visiting http://go-colly.org/docs/examples/redis_backend/
Visiting http://go-colly.org/docs/examples/request_context/
Visiting http://go-colly.org/docs/examples/scraper_server/
Visiting http://go-colly.org/docs/examples/url_filter/
Visiting http://go-colly.org/docs/examples/cryptocoinmarketcap/
Visiting http://go-colly.org/docs/examples/coursera_courses/
Visiting http://go-colly.org/docs/examples/google_groups/
Visiting http://go-colly.org/docs/examples/hackernews_comments/
Visiting http://go-colly.org/docs/examples/instagram/
Visiting http://go-colly.org/docs/examples/openedx_courses/
Visiting http://go-colly.org/docs/examples/reddit/
Visiting http://go-colly.org/docs/examples/shopify_sitemap/
Visiting http://go-colly.org/docs/examples/xkcd_store/
Visiting http://go-colly.org/contact/
Visiting http://go-colly.org/sitemap.xml
Visiting http://go-colly.org/docs/examples/coursera_courses
Visiting http://go-colly.org/docs/examples/redis_backend
Visiting http://go-colly.org/docs/best_practices/storage
Visiting http://go-colly.org/docs/examples/scraper_server
Visiting http://go-colly.org/data/cryptocoin_market_cap.csv
Visiting http://go-colly.org/data/coursera_courses.json
Visiting http://go-colly.org/data/xkcd_store_items.csv
Visiting http://go-colly.org/contact
Visiting http://go-colly.org/articles/first_release/
Visiting http://go-colly.org/articles/scraping_related_http_headers/
Visiting http://go-colly.org/articles/nonprofit/
Visiting http://go-colly.org/articles/how_to_scrape_instagram/
Visiting http://go-colly.org/articles/scraping_tips/