使用docker compose安装包

使用docker compose安装包,docker,go,docker-compose,Docker,Go,Docker Compose,在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

在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"]
如果我在没有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
工作得很好。

基本上,您的问题是引用了一个模块路径(
v2
给出了它),截至本文撰写之时

但是,通过一些调整和更正,您的项目将运行:

main.go
现在只要打电话

go mod init stackoverflow.com/user3939059/crawler
(当然,您应该调整域名和用户的名称)

Dockerfile
虽然您可以在基于“golang”图像的Docker图像中执行
go run main.go
,但这样做效率很低。最好静态编译应用程序,甚至可以使用空的scratch映像作为运行应用程序的基础:

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构建-t user3939059/crawler:latest.之后,您可以运行映像:

$docker运行--rm user3939059/爬虫程序
你好,加油!
参观http://go-colly.org/
参观http://go-colly.org/docs/
参观http://go-colly.org/articles/
参观http://go-colly.org/services/
参观http://go-colly.org/datasets/
参观http://go-colly.org/data/factbase_transcripts.tar.bz2
参观http://go-colly.org/docs/examples/factbase/
参观http://go-colly.org/docs/introduction/install/
参观http://go-colly.org/docs/introduction/start/
参观http://go-colly.org/docs/introduction/configuration/
参观http://go-colly.org/docs/best_practices/debugging/
参观http://go-colly.org/docs/best_practices/distributed/
参观http://go-colly.org/docs/best_practices/storage/
参观http://go-colly.org/docs/best_practices/multi_collector/
参观http://go-colly.org/docs/best_practices/crawling/
参观http://go-colly.org/docs/best_practices/extensions/
参观http://go-colly.org/docs/examples/basic/
参观http://go-colly.org/docs/examples/error_handling/
参观http://go-colly.org/docs/examples/login/
参观http://go-colly.org/docs/examples/max_depth/
参观http://go-colly.org/docs/examples/multipart/
参观http://go-colly.org/docs/examples/parallel/
参观http://go-colly.org/docs/examples/proxy_switcher/
参观http://go-colly.org/docs/examples/queue/
参观http://go-colly.org/docs/examples/random_delay/
参观http://go-colly.org/docs/examples/rate_limit/
参观http://go-colly.org/docs/examples/redis_backend/
参观http://go-colly.org/docs/examples/request_context/
参观http://go-colly.org/docs/examples/scraper_server/
参观http://go-colly.org/docs/examples/url_filter/
参观http://go-colly.org/docs/examples/cryptocoinmarketcap/
参观http://go-colly.org/docs/examples/coursera_courses/
参观http://go-colly.org/docs/examples/google_groups/
参观http://go-colly.org/docs/examples/hackernews_comments/
参观http://go-colly.org/docs/examples/instagram/
参观http://go-colly.org/docs/examples/openedx_courses/
参观http://go-colly.org/docs/examples/reddit/
参观http://go-colly.org/docs/examples/shopify_sitemap/
参观http://go-colly.org/docs/examples/xkcd_store/
参观http://go-colly.org/contact/
参观http://go-colly.org/sitemap.xml
参观http://go-colly.org/docs/examples/coursera_courses
参观http://go-colly.org/docs/examples/redis_backend
参观http://go-colly.org/docs/best_practices/storage
参观http://go-colly.org/docs/examples/scraper_server
参观http://go-colly.org/data/cryptocoin_market_cap.csv
参观http://go-colly.org/data/coursera_courses.json
参观http://go-colly.org/data/xkcd_store_items.csv
参观http://go-colly.org/contact
参观http://go-colly.org/articles/first_release/
参观http://go-colly.org/articles/scraping_related_http_headers/
参观http://go-colly.org/articles/nonprofit/
参观http://go-colly.org/articles/how_to_scrape_instagram/
参观http://go-colly.org/articles/scraping_tips/

我认为您需要签出特定的标签
v2.0.0
包github.com/gocolly/colly@[v2.0.0]:无效的github.com/import路径“github.com/gocolly/colly@[v2.0.0]”
请问您使用Go 1.7而不使用Go模块是否有具体原因?谢谢,我是否需要先在主机上运行
go-get-github.com/gocolly/colly
?我不太清楚包裹是从哪里被拉进集装箱的。(我正在与docker compose一起运行此功能)
go mod init stackoverflow.com/user3939059/crawler
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"]