Skip to content

Commit c4979a4

Browse files
authored
Merge pull request #21550 from docker/published-update
publish updates from main
2 parents fa493ed + 27094d5 commit c4979a4

File tree

13 files changed

+403
-122
lines changed

13 files changed

+403
-122
lines changed

_vale/config/vocabularies/Docker/accept.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Zsh
113113
[Nn]amespace
114114
[Oo]nboarding
115115
[Pp]aravirtualization
116+
[Pp]repend
116117
[Pp]rocfs
117118
[Pp]roxied
118119
[Pp]roxying

content/guides/gha.md

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
---
2+
title: Introduction to GitHub Actions with Docker
3+
linkTitle: GitHub Actions and Docker
4+
summary: |
5+
Learn how to automate image build and push with GitHub Actions.
6+
params:
7+
tags: [devops]
8+
time: 10 minutes
9+
---
10+
11+
This guide provides an introduction to building CI pipelines using Docker and
12+
GitHub Actions. You will learn how to use Docker's official GitHub Actions to
13+
build your application as a Docker image and push it to Docker Hub. By the end
14+
of the guide, you'll have a simple, functional GitHub Actions configuration for
15+
Docker builds. Use it as-is, or extend it further to fit your needs.
16+
17+
## Prerequisites
18+
19+
If you want to follow along with the guide, ensure you have the following:
20+
21+
- A Docker account.
22+
- Familiarity with Dockerfiles.
23+
24+
This guide assumes basic knowledge of Docker concepts but provides explanations
25+
for using Docker in GitHub Actions workflows.
26+
27+
## Get the sample app
28+
29+
This guide is project-agnostic and assumes you have an application with a
30+
Dockerfile.
31+
32+
If you need a sample project to follow along, you can use [this sample
33+
application](https://github.com/dvdksn/rpg-name-generator.git), which includes
34+
a Dockerfile for building a containerized version of the app. Alternatively,
35+
use your own GitHub project or create a new repository from the template.
36+
37+
{{% dockerfile.inline %}}
38+
39+
{{ $data := resources.GetRemote "https://raw.githubusercontent.com/dvdksn/rpg-name-generator/refs/heads/main/Dockerfile" }}
40+
41+
```dockerfile {collapse=true}
42+
{{ $data.Content }}
43+
```
44+
45+
{{% /dockerfile.inline %}}
46+
47+
## Configure your GitHub repository
48+
49+
The workflow in this guide pushes the image you build to Docker Hub. To do
50+
that, you must authenticate with your Docker credentials (username and access
51+
token) as part of the GitHub Actions workflow.
52+
53+
For instructions on how to create a Docker access token, see
54+
[Create and manage access tokens](/manuals/security/for-developers/access-tokens.md).
55+
56+
Once you have your Docker credentials ready, add the credentials to your GitHub
57+
repository so you can use them in GitHub Actions:
58+
59+
1. Open your repository's **Settings**.
60+
2. Under **Security**, go to **Secrets and variables > Actions**.
61+
3. Under **Secrets**, create a new repository secret named `DOCKER_PASSWORD`,
62+
containing your Docker access token.
63+
4. Next, under **Variables**, create a `DOCKER_USERNAME` repository variable
64+
containing your Docker Hub username.
65+
66+
## Set up your GitHub Actions workflow
67+
68+
GitHub Actions workflows define a series of steps to automate tasks, such as
69+
building and pushing Docker images, in response to triggers like commits or
70+
pull requests. In this guide, the workflow focuses on automating Docker builds
71+
and testing, ensuring your containerized application works correctly before
72+
publishing it.
73+
74+
Create a file named `docker-ci.yml` in the `.github/workflows/` directory of
75+
your repository. Start with the basic workflow configuration:
76+
77+
```yaml
78+
name: Build and Push Docker Image
79+
80+
on:
81+
push:
82+
branches:
83+
- main
84+
pull_request:
85+
```
86+
87+
This configuration runs the workflow on pushes to the main branch and on pull
88+
requests. By including both triggers, you can ensure that the image builds
89+
correctly for a pull request before it's merged.
90+
91+
## Extract metadata for tags and annotations
92+
93+
For the first step in your workflow, use the `docker/metadata-action` to
94+
generate metadata for your image. This action extracts information about your
95+
Git repository, such as branch names and commit SHAs, and generates image
96+
metadata such as tags and annotations.
97+
98+
Add the following YAML to your workflow file:
99+
100+
```yaml
101+
jobs:
102+
build:
103+
runs-on: ubuntu-latest
104+
steps:
105+
- name: Checkout
106+
uses: actions/checkout@v4
107+
- name: Extract Docker image metadata
108+
id: meta
109+
uses: docker/metadata-action@v5
110+
with:
111+
images: ${{ vars.DOCKER_USERNAME }}/my-image
112+
```
113+
114+
These steps prepare metadata to tag and annotate your images during the build
115+
and push process.
116+
117+
- The **Checkout** step clones the Git repository.
118+
- The **Extract Docker image metadata** step extracts Git metadata and
119+
generates image tags and annotations for the Docker build.
120+
121+
## Authenticate to your registry
122+
123+
Before you build the image, authenticate to your registry to ensure that you
124+
can push your built image to the registry.
125+
126+
To authenticate with Docker Hub, add the following step to your workflow:
127+
128+
```yaml
129+
- name: Log in to Docker Hub
130+
uses: docker/login-action@v3
131+
with:
132+
username: ${{ vars.DOCKER_USERNAME }}
133+
password: ${{ secrets.DOCKER_PASSWORD }}
134+
```
135+
136+
This step uses the Docker credentials [configured in the repository settings](#configure-your-github-repository).
137+
138+
## Build and push the image
139+
140+
Finally, build the final production image and push it to your registry. The
141+
following configuration builds the image and pushes it directly to a registry.
142+
143+
```yaml
144+
- name: Build and push Docker image
145+
uses: docker/build-push-action@v6
146+
with:
147+
push: ${{ github.event_name != 'pull_request' }}
148+
tags: ${{ steps.meta.outputs.tags }}
149+
annotations: ${{ steps.meta.outputs.annotations }}
150+
```
151+
152+
In this configuration:
153+
154+
- `push: ${{ github.event_name != 'pull_request' }}` ensures that images are
155+
only pushed when the event is not a pull request. This way, the workflow
156+
builds and tests images for pull requests but only pushes images for commits
157+
to the main branch.
158+
- `tags` and `annotations` use the outputs from the metadata action to apply
159+
consistent tags and [annotations](/manuals/build/metadata/annotations.md) to
160+
the image automatically.
161+
162+
## Attestations
163+
164+
SBOM (Software Bill of Materials) and provenance attestations improve security
165+
and traceability, ensuring your images meet modern software supply chain
166+
requirements.
167+
168+
With a small amount of additional configuration, you can configure
169+
`docker/build-push-action` to generate Software Bill of Materials (SBOM) and
170+
provenance attestations for the image, at build-time.
171+
172+
To generate this additional metadata, you need to make two changes to your
173+
workflow:
174+
175+
- Before the build step, add a step that uses `docker/setup-buildx-action`.
176+
This action configures your Docker build client with additional capabilities
177+
that the default client doesn't support.
178+
- Then, update the **Build and push Docker image** step to also enable SBOM and
179+
provenance attestations.
180+
181+
Here's the updated snippet:
182+
183+
```yaml
184+
- name: Set up Docker Buildx
185+
uses: docker/setup-buildx-action@v3
186+
187+
- name: Build and push Docker image
188+
uses: docker/build-push-action@v6
189+
with:
190+
push: ${{ github.event_name != 'pull_request' }}
191+
tags: ${{ steps.meta.outputs.tags }}
192+
annotations: ${{ steps.meta.outputs.annotations }}
193+
provenance: true
194+
sbom: true
195+
```
196+
197+
For more details about attestations, refer to
198+
[the documentation](/manuals/build/metadata/attestations/_index.md).
199+
200+
## Conclusion
201+
202+
With all the steps outlined in the previous section, here's the full workflow
203+
configuration:
204+
205+
```yaml
206+
name: Build and Push Docker Image
207+
208+
on:
209+
push:
210+
branches:
211+
- main
212+
pull_request:
213+
214+
jobs:
215+
build:
216+
runs-on: ubuntu-latest
217+
steps:
218+
- name: Checkout
219+
uses: actions/checkout@v4
220+
221+
- name: Extract Docker image metadata
222+
id: meta
223+
uses: docker/metadata-action@v5
224+
with:
225+
images: ${{ vars.DOCKER_USERNAME }}/my-image
226+
227+
- name: Log in to Docker Hub
228+
uses: docker/login-action@v3
229+
with:
230+
username: ${{ vars.DOCKER_USERNAME }}
231+
password: ${{ secrets.DOCKER_PASSWORD }}
232+
233+
- name: Set up Docker Buildx
234+
uses: docker/setup-buildx-action@v3
235+
236+
- name: Build and push Docker image
237+
uses: docker/build-push-action@v6
238+
with:
239+
push: ${{ github.event_name != 'pull_request' }}
240+
tags: ${{ steps.meta.outputs.tags }}
241+
annotations: ${{ steps.meta.outputs.annotations }}
242+
provenance: true
243+
sbom: true
244+
```
245+
246+
This workflow implements best practices for building and pushing Docker images
247+
using GitHub Actions. This configuration can be used as-is or extended with
248+
additional features based on your project's needs, such as
249+
[multi-platform](/manuals/build/building/multi-platform.md).
250+
251+
### Further reading
252+
253+
- Learn more about advanced configurations and examples in the [Docker Build GitHub Actions](/manuals/build/ci/github-actions/_index.md) section.
254+
- For more complex build setups, you may want to consider [Bake](/manuals/build/bake/_index.md). (See also the [Mastering Buildx Bake guide](/guides/bake/index.md).)
255+
- Learn about Docker's managed build service, designed for faster, multi-platform builds, see [Docker Build Cloud](/guides/docker-build-cloud/_index.md).

content/manuals/build/building/best-practices.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ download of base images and dependencies.
9898
```dockerfile
9999
# syntax=docker/dockerfile:1
100100
FROM ubuntu:24.04
101-
RUN apt-get -y update && apt-get install -y python
101+
RUN apt-get -y update && apt-get install -y --no-install-recommends python3
102102
```
103103

104104
Also consider [pinning base image versions](#pin-base-image-versions).
@@ -165,7 +165,7 @@ review. Adding a space before a backslash (`\`) helps as well.
165165
Here’s an example from the [buildpack-deps image](https://github.com/docker-library/buildpack-deps):
166166

167167
```dockerfile
168-
RUN apt-get update && apt-get install -y \
168+
RUN apt-get update && apt-get install -y --no-install-recommends \
169169
bzr \
170170
cvs \
171171
git \
@@ -322,7 +322,7 @@ For example, you can chain commands with the `&&` operator, and use
322322
escape characters to break long commands into multiple lines.
323323

324324
```dockerfile
325-
RUN apt-get update && apt-get install -y \
325+
RUN apt-get update && apt-get install -y --no-install-recommends \
326326
package-bar \
327327
package-baz \
328328
package-foo
@@ -337,7 +337,7 @@ with a pipeline operator:
337337
```dockerfile
338338
RUN <<EOF
339339
apt-get update
340-
apt-get install -y \
340+
apt-get install -y --no-install-recommends \
341341
package-bar \
342342
package-baz \
343343
package-foo
@@ -356,7 +356,7 @@ Always combine `RUN apt-get update` with `apt-get install` in the same `RUN`
356356
statement. For example:
357357

358358
```dockerfile
359-
RUN apt-get update && apt-get install -y \
359+
RUN apt-get update && apt-get install -y --no-install-recommends \
360360
package-bar \
361361
package-baz \
362362
package-foo
@@ -370,7 +370,7 @@ subsequent `apt-get install` instructions to fail. For example, this issue will
370370

371371
FROM ubuntu:22.04
372372
RUN apt-get update
373-
RUN apt-get install -y curl
373+
RUN apt-get install -y --no-install-recommends curl
374374
```
375375

376376
After building the image, all layers are in the Docker cache. Suppose you later
@@ -381,7 +381,7 @@ modify `apt-get install` by adding an extra package as shown in the following Do
381381

382382
FROM ubuntu:22.04
383383
RUN apt-get update
384-
RUN apt-get install -y curl nginx
384+
RUN apt-get install -y --no-install-recommends curl nginx
385385
```
386386

387387
Docker sees the initial and modified instructions as identical and reuses the
@@ -390,14 +390,14 @@ because the build uses the cached version. Because the `apt-get update` isn't
390390
run, your build can potentially get an outdated version of the `curl` and
391391
`nginx` packages.
392392

393-
Using `RUN apt-get update && apt-get install -y` ensures your Dockerfile
393+
Using `RUN apt-get update && apt-get install -y --no-install-recommends` ensures your Dockerfile
394394
installs the latest package versions with no further coding or manual
395395
intervention. This technique is known as cache busting. You can also achieve
396396
cache busting by specifying a package version. This is known as version pinning.
397397
For example:
398398

399399
```dockerfile
400-
RUN apt-get update && apt-get install -y \
400+
RUN apt-get update && apt-get install -y --no-install-recommends \
401401
package-bar \
402402
package-baz \
403403
package-foo=1.3.*
@@ -411,7 +411,7 @@ Below is a well-formed `RUN` instruction that demonstrates all the `apt-get`
411411
recommendations.
412412

413413
```dockerfile
414-
RUN apt-get update && apt-get install -y \
414+
RUN apt-get update && apt-get install -y --no-install-recommends \
415415
aufs-tools \
416416
automake \
417417
build-essential \

content/manuals/build/ci/github-actions/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Introduction to GitHub Actions
2+
title: Docker Build GitHub Actions
33
linkTitle: GitHub Actions
44
description: Docker maintains a set of official GitHub Actions for building Docker images.
55
keywords: ci, github actions, gha, build, introduction, tutorial

0 commit comments

Comments
 (0)