Skip to content

Commit

Permalink
fix/service image name (#212)
Browse files Browse the repository at this point in the history
* fix(service): remove image name suppress function
* feat: add docker image data-source
* docs(service): add example for iamge datasource usage
* fix: image repo digest with tag determination
* fix: always return a repoDigest
* fix(image): deprecation notice for latest attribute
* fix(ci): explicitly go get tfplugindocs
* fix(ci): remove gocenter.io proxy
  • Loading branch information
mavogel committed Jun 21, 2021
1 parent 7f4c14d commit 4d40b84
Show file tree
Hide file tree
Showing 40 changed files with 650 additions and 243 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/acc-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
- '.github/workflows/**'

env:
GOPROXY: https://proxy.golang.org,https://gocenter.io,direct
GOPROXY: https://proxy.golang.org,direct
DEBIAN_FRONTEND: noninteractive
DOCKER_CE_VERSION: "5:20.10.5~3-0~ubuntu-focal"
GO_VERSION: "1.16"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
- '.github/workflows/**'

env:
GOPROXY: https://proxy.golang.org,https://gocenter.io,direct
GOPROXY: https://proxy.golang.org,direct
GO_VERSION: "1.16"

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
- '.github/workflows/**'

env:
GOPROXY: https://proxy.golang.org,https://gocenter.io,direct
GOPROXY: https://proxy.golang.org,direct
GO_VERSION: "1.16"

jobs:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/website-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- docs/**

env:
GOPROXY: https://proxy.golang.org,https://gocenter.io,direct
GOPROXY: https://proxy.golang.org,direct
GO_VERSION: "1.16"

jobs:
Expand All @@ -24,6 +24,8 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Setup tools
run: make setup
- name: Explicitly get tfplugindocs
run: go get github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
- name: Generate the website
run: make website-generation
- name: Verify Changed files
Expand Down
5 changes: 2 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ build: fmtcheck
go install

setup:
go mod download
cd tools && GO111MODULE=on go install github.com/client9/misspell/cmd/misspell
cd tools && GO111MODULE=on go install github.com/katbyte/terrafmt
cd tools && GO111MODULE=on go install github.com/golangci/golangci-lint/cmd/golangci-lint
Expand Down Expand Up @@ -76,7 +75,7 @@ website-lint:
echo "Unexpected mispelling found in website files."; \
echo "To automatically fix the misspelling, run 'make website-lint-fix' and commit the changes."; \
exit 1)
@docker run -v $(PWD):/markdown 06kellyjac/markdownlint-cli docs/ || (echo; \
@docker run --rm -v $(PWD):/markdown 06kellyjac/markdownlint-cli docs/ || (echo; \
echo "Unexpected issues found in website Markdown files."; \
echo "To apply any automatic fixes, run 'make website-lint-fix' and commit the changes."; \
exit 1)
Expand All @@ -89,7 +88,7 @@ website-lint:
website-lint-fix:
@echo "==> Applying automatic website linter fixes..."
@misspell -w -source=text docs/
@docker run -v $(PWD):/markdown 06kellyjac/markdownlint-cli --fix docs/
@docker run --rm -v $(PWD):/markdown 06kellyjac/markdownlint-cli --fix docs/
@terrafmt fmt ./docs --pattern '*.md'

.PHONY: build test testacc vet fmt fmtcheck errcheck test-compile website-link-check website-lint website-lint-fix
Expand Down
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ terraform {
# since new versions are released frequently
docker = {
source = "kreuzwerker/docker"
version = "2.12.1"
version = "2.12.2"
}
}
}
Expand All @@ -42,9 +42,38 @@ resource "docker_image" "nginx" {
}
# Create a docker container resource
# -> same as 'docker run --name nginx -d nginx:latest'
# -> same as 'docker run --name nginx -p8080:80 -d nginx:latest'
resource "docker_container" "nginx" {
name = "nginx"
image = docker_image.nginx.latest
ports {
external = 8080
internal = 80
}
}
# Or create a service resource
# -> same as 'docker service create -d -p 8081:80 --name nginx-service --replicas 2 nginx:latest'
resource "docker_service" "nginx_service" {
name = "nginx-service"
task_spec {
container_spec {
image = docker_image.nginx.repo_digest
}
}
mode {
replicated {
replicas = 2
}
}
endpoint_spec {
ports {
published_port = 8081
target_port = 80
}
}
}
```
52 changes: 52 additions & 0 deletions docs/data-sources/image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "docker_image Data Source - terraform-provider-docker"
subcategory: ""
description: |-
docker_image provides details about a specific Docker Image which need to be presend on the Docker Host
---

# docker_image (Data Source)

`docker_image` provides details about a specific Docker Image which need to be presend on the Docker Host

## Example Usage

```terraform
# uses the 'latest' tag
data "docker_image" "latest" {
name = "nginx"
}
# uses a specific tag
data "docker_image" "specific" {
name = "nginx:1.17.6"
}
# use the image digest
data "docker_image" "digest" {
name = "nginx@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
}
# uses the tag and the image digest
data "docker_image" "tag_and_digest" {
name = "nginx:1.19.1@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) The name of the Docker image, including any tags or SHA256 repo digests.

### Optional

- **id** (String) The ID of this resource.

### Read-Only

- **repo_digest** (String) The image sha256 digest in the form of `repo[:tag]@sha256:<hash>`. It may be empty in the edge case where the local image was pulled from a repo, tagged locally, and then referred to in the data source by that local name/tag.


11 changes: 6 additions & 5 deletions docs/resources/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ resource "docker_image" "ubuntu" {

### Dynamic updates

To be able to update an update dynamically when the `sha256` sum changes,
To be able to update an image dynamically when the `sha256` sum changes,
you need to use it in combination with `docker_registry_image` as follows:

```terraform
Expand All @@ -43,8 +43,8 @@ resource "docker_image" "ubuntu" {

### Build

You can also use the resource to build and image.
In thid case the image "zoo" and "zoo:develop" are built.
You can also use the resource to build an image.
In this case the image "zoo" and "zoo:develop" are built.

```terraform
resource "docker_image" "zoo" {
Expand Down Expand Up @@ -80,8 +80,9 @@ resource "docker_image" "zoo" {

### Read-Only

- **latest** (String) The ID of the image.
- **latest** (String, Deprecated) The ID of the image in the form of `sha256:<hash>` image digest. Do not confuse it with the default `latest` tag.
- **output** (String, Deprecated)
- **repo_digest** (String) The image sha256 digest in the form of `repo[:tag]@sha256:<hash>`.

<a id="nestedblock--build"></a>
### Nested Schema for `build`
Expand All @@ -99,4 +100,4 @@ Optional:
- **no_cache** (Boolean) Do not use cache when building the image
- **remove** (Boolean) Remove intermediate containers after a successful build. Defaults to `true`.
- **tag** (List of String) Name and optionally a tag in the 'name:tag' format
- **target** (String) Set the target build stage to build
- **target** (String) Set the target build stage to build
29 changes: 28 additions & 1 deletion docs/resources/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ The following command is the equivalent:
docker service create -d -p 8080 --name foo-service repo.mycompany.com:8080/foo-service:v1
```

### Basic with Datasource

Alternatively, if the image is already present on the Docker Host and not managed
by `terraform`, you can also use the `docker_image` datasource:

```terraform
data "docker_image" "foo" {
name = "repo.mycompany.com:8080/foo-service:v1"
}
resource "docker_service" "foo" {
name = "foo-service"
task_spec {
container_spec {
image = data.docker_image.foo.repo_digest
}
}
endpoint_spec {
ports {
target_port = "8080"
}
}
}
```

### Advanced

The following configuration shows the full capabilities of a Docker Service,
Expand Down Expand Up @@ -307,7 +334,7 @@ Optional:

Required:

- **image** (String) The image name to use for the containers of the service
- **image** (String) The image name to use for the containers of the service, like `nginx:1.17.6`. Also use the data-source or resource of `docker_image` with the `repo_digest` or `docker_registry_image` with the `name` attribute for this, as shown in the examples.

Optional:

Expand Down
19 changes: 19 additions & 0 deletions examples/data-sources/docker_image/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# uses the 'latest' tag
data "docker_image" "latest" {
name = "nginx"
}

# uses a specific tag
data "docker_image" "specific" {
name = "nginx:1.17.6"
}

# use the image digest
data "docker_image" "digest" {
name = "nginx@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
}

# uses the tag and the image digest
data "docker_image" "tag_and_digest" {
name = "nginx:1.19.1@sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2"
}
19 changes: 19 additions & 0 deletions examples/resources/docker_service/resource-basic-image-ds.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
data "docker_image" "foo" {
name = "repo.mycompany.com:8080/foo-service:v1"
}

resource "docker_service" "foo" {
name = "foo-service"

task_spec {
container_spec {
image = data.docker_image.foo.repo_digest
}
}

endpoint_spec {
ports {
target_port = "8080"
}
}
}
3 changes: 2 additions & 1 deletion examples/resources/docker_service/resource-basic.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

resource "docker_service" "foo" {
name = "foo-service"

Expand All @@ -12,4 +13,4 @@ resource "docker_service" "foo" {
target_port = "8080"
}
}
}
}

0 comments on commit 4d40b84

Please sign in to comment.