Part 6: Using Docker In Earthly
To copy the files for this example ( Part 6 ) run
earthly --artifact github.com/earthly/earthly/examples/tutorial/go:main+part6/part6 ./part6
Examples in Python, Javascript and Java are at the bottom of this page.

The WITH DOCKER Command

You may find that you need to run Docker commands inside of a target. For those cases Earthly offers WITH DOCKER. WITH DOCKER will initialize a Docker daemon that can be used in the context of a RUN command.
Whenever you need to use WITH DOCKER we recommend (though it is not required) that you use Earthly's own Docker in Docker (dind) image: earthly/dind:alpine.
Notice WITH DOCKER creates a block of code that has an END keyword. Everything that happens within this block is going to take place within our earthly/dind:alpine container.

Pulling an Image

hello:
FROM earthly/dind:alpine
WITH DOCKER --pull hello-world
RUN docker run hello-world
END
You can see in the command above that we can pass a flag to WITH DOCKER telling it to pull an image from Docker Hub. We can pass other flags to load in artifacts built by other targets --load or even images defined by docker-compose --compose. These images will be available within the context of WITH DOCKER's docker daemon.

Loading an Image

We can load in an image created by another target with the --load flag.
my-hello-world:
FROM ubuntu
CMD echo "hello world"
SAVE IMAGE my-hello:latest
​
hello:
FROM earthly/dind:alpine
WITH DOCKER --load hello:latest=+my-hello-world
RUN docker run hello:latest
END

A Real World Example

One common use case for WITH DOCKER is running integration tests that require other services. In this case we need to set up a redis service for our tests. For this we can user a docker-compose.yml.
docker-compose.yml
version: "3"
​
services:
redis:
container_name: local-redis
image: redis:6.0-alpine
ports:
- 127.0.0.1:6379:6379
hostname: redis
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:6379"]
interval: 1s
timeout: 10s
retries: 5
networks:
- go/part6_default
​
networks:
go/part6_default:
main.go
package main
​
import (
"github.com/sirupsen/logrus"
)
​
var howCoolIsEarthly = "IceCool"
​
func main() {
logrus.Info("hello world")
}
main_integration_test.go
package main
​
import (
"context"
"testing"
​
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/require"
)
​
func TestIntegration(t *testing.T) {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "", // no password set
DB: 0, // use default DB
})
​
err := rdb.Set(ctx, "howCoolIsEarthly", howCoolIsEarthly, 0).Err()
if err != nil {
panic(err)
}
​
resultFromDB, err := rdb.Get(ctx, "howCoolIsEarthly").Result()
if err != nil {
panic(err)
}
require.Equal(t, howCoolIsEarthly, resultFromDB)
}
VERSION 0.6
FROM golang:1.15-alpine3.13
WORKDIR /go-example
​
deps:
COPY go.mod go.sum ./
RUN go mod download
SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT go.sum AS LOCAL go.sum
​
test-setup:
FROM +deps
COPY main.go .
COPY main_integration_test.go .
ENV CGO_ENABLED=0
ENTRYPOINT [ "go", "test", "github.com/earthly/earthly/examples/go"]
SAVE IMAGE test:latest
​
integration-tests:
FROM earthly/dind:alpine
COPY docker-compose.yml ./
WITH DOCKER --compose docker-compose.yml --load tests:latest=+test-setup
RUN docker run --network=default_go/part6_default tests:latest
END
When we use the --compose flag, Earthly will start up the services defined in the docker-compose file for us. In this case, we built a separate image that copies in our test files and uses the command to run the tests as its ENTRYPOINT. We can then load this image into our WITH DOCKER command. Note that loading an image will not run it by default, we need to explicitly run the image after we load it.

More Examples

Javascript
Java
Python
Copy link
On this page
The WITH DOCKER Command
A Real World Example
More Examples