Docs: add content to 1205: "building container images"

Signed-off-by: Solomon Hykes <solomon@dagger.io>
This commit is contained in:
Solomon Hykes 2022-04-05 01:04:40 +00:00
parent f3ac279f73
commit 26cbc7dc68
3 changed files with 47 additions and 20 deletions

View File

@ -3,20 +3,30 @@ slug: /1205/container-images
displayed_sidebar: europa
---
# Building container images
# Building docker container images
You can use Dagger to build container images. Here's a simple example of a [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) build:
You can use Dagger to build container images, either by executing a Dockerfile, or specifying the build steps natively in CUE. Which method to choose depends on the requirements of your project. You can mix and match builds from both methods in the same plan.
## Executing a Dockerfile
Dagger can natively load and execute Dockerfiles. This is recommended in cases where compatibility with existing Dockerfiles is more important than fully leveraging the power of CUE.
Here's a simple example of a [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) build:
```cue file=../tests/core-concepts/container-images/simple/with-dockerfile.cue
```
## Building with CUE
## Specifying a build in CUE
`Dockerfile` files are easy to start, but you can also build images entirely in CUE. The following example produces the same image as above:
You can specify your container build natively in CUE, using the official Docker package: `universe.dagger.io/docker`. This is recommended when you don't need to worry about Dockerfile compatibility, and want to take advantage of the full power of CUE and the Dagger APIs.
Native CUE builds have the same backend as Dockerfile builds, so all the same features are available. Since CUE is a more powerful language than the Dockerfile syntax, every Dockerfile can be ported to an equivalent CUE configuration, but the opposite is not true. The following example produces the same image as above.
```cue file=../tests/core-concepts/container-images/simple/build.cue
```
Because this build configuration is pure CUE, it can leverage the full power of Dagger's composition model.
## Automation
Building images in CUE gives you greater flexibility. For example, you can automate building multiple versions of an image, and deploy, all in Dagger:

View File

@ -5,16 +5,23 @@ import (
"universe.dagger.io/docker"
)
dagger.#Plan & {
client: filesystem: "./src": read: contents: dagger.#FS
// This action builds a docker image from a python app.
// Build steps are defined in native CUE.
#PythonBuild: {
// Source code of the Python application
app: dagger.#FS
actions: build: docker.#Build & {
// Container image
image: _build.output
// Build steps
_build: docker.#Build & {
steps: [
docker.#Pull & {
source: "python:3.9"
},
docker.#Copy & {
contents: client.filesystem."./src".read.contents
contents: app
dest: "/app"
},
docker.#Run & {
@ -29,3 +36,12 @@ dagger.#Plan & {
]
}
}
// Example usage in a plan
dagger.#Plan & {
client: filesystem: "./src": read: contents: dagger.#FS
actions: build: #PythonBuild & {
app: client.filesystem."./src".read.contents
}
}

View File

@ -5,21 +5,22 @@ import (
"universe.dagger.io/docker"
)
// This action builds a docker image from a python app.
// Build steps are defined in an inline Dockerfile.
#PythonBuild: docker.#Dockerfile & {
dockerfile: contents: """
FROM python:3.9
COPY . /app
RUN pip install -r /app/requirements.txt
CMD python /app/app.py
"""
}
// Example usage in a plan
dagger.#Plan & {
client: filesystem: "./src": read: contents: dagger.#FS
actions: build: docker.#Dockerfile & {
// This is the context.
actions: build: #PythonBuild & {
source: client.filesystem."./src".read.contents
// Default is to look for a Dockerfile in the context,
// but let's declare it here.
dockerfile: contents: #"""
FROM python:3.9
COPY . /app
RUN pip install -r /app/requirements.txt
CMD python /app/app.py
"""#
}
}