-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi-stage builds to the C++ guide #21562
Open
aerabi
wants to merge
2
commits into
docker:main
Choose a base branch
from
aerabi:aerabi/add-multistage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
title: Create a multi-stage build for your C++ application | ||
linkTitle: Containerize your app | ||
weight: 60 | ||
keywords: C++, containerize, multi-stage | ||
description: Learn how to create a multi-stage build for a C++ application. | ||
aliases: | ||
- /language/cpp/multistage/ | ||
- /guides/language/cpp/multistage/ | ||
--- | ||
|
||
## Prerequisites | ||
|
||
- You have a [Git client](https://git-scm.com/downloads). The examples in this section use a command-line based Git client, but you can use any client. | ||
|
||
## Overview | ||
|
||
This section walks you through creating a multi-stage Docker build for a C++ application. | ||
A multi-stage build is a Docker feature that allows you to use different base images for different stages of the build process, | ||
so you can optimize the size of your final image and separate build dependencies from runtime dependencies. | ||
|
||
## Get the sample application | ||
|
||
We're using the same sample repository that you used in the previous sections of this guide. If you haven't already cloned the repository, clone it now: | ||
|
||
```bash | ||
$ git clone https://github.com/dockersamples/c-plus-plus-docker.git | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This repo should be synced with the upstream repo created by Pradumna. |
||
``` | ||
|
||
The example for this section is under the `hello` directory in the repository. Get inside it and take a look at the files: | ||
|
||
```bash | ||
$ cd c-plus-plus-docker/hello | ||
$ ls | ||
``` | ||
|
||
You should see the following files: | ||
|
||
```text | ||
Dockerfile hello.cpp | ||
``` | ||
|
||
## Check the Dockerfile | ||
|
||
Open the `Dockerfile` in an IDE or text editor. The `Dockerfile` contains the instructions for building the Docker image. | ||
|
||
```Dockerfile | ||
# Stage 1: Build stage | ||
FROM ubuntu:latest AS build | ||
|
||
# Install build-essential for compiling C++ code | ||
RUN apt-get update && apt-get install -y build-essential | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Copy the source code into the container | ||
COPY hello.cpp . | ||
|
||
# Compile the C++ code statically to ensure it doesn't depend on runtime libraries | ||
RUN g++ -o hello hello.cpp -static | ||
|
||
# Stage 2: Runtime stage | ||
FROM scratch | ||
|
||
# Copy the static binary from the build stage | ||
COPY --from=build /app/hello /hello | ||
|
||
# Command to run the binary | ||
CMD ["/hello"] | ||
``` | ||
|
||
The `Dockerfile` has two stages: | ||
|
||
1. **Build stage**: This stage uses the `ubuntu:latest` image to compile the C++ code and create a static binary. | ||
2. **Runtime stage**: This stage uses the `scratch` image, which is an empty image, to copy the static binary from the build stage and run it. | ||
|
||
## Build the Docker image | ||
|
||
To build the Docker image, run the following command in the `hello` directory: | ||
|
||
```bash | ||
$ docker build -t hello . | ||
``` | ||
|
||
The `-t` flag tags the image with the name `hello`. | ||
|
||
## Run the Docker container | ||
|
||
To run the Docker container, use the following command: | ||
|
||
```bash | ||
$ docker run hello | ||
``` | ||
|
||
You should see the output `Hello, World!` in the terminal. | ||
|
||
## Summary | ||
|
||
In this section, you learned how to create a multi-stage build for a C++ application. Multi-stage builds help you optimize the size of your final image and separate build dependencies from runtime dependencies. | ||
In this example, the final image only contains the static binary and doesn't include any build dependencies. | ||
|
||
As the image has an empty base, the usual OS tools are also absent. So, for example, you can't run a simple `ls` command in the container: | ||
|
||
```bash | ||
$ docker run hello ls | ||
``` | ||
|
||
This makes the image very lightweight and secure. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs to be a completely separate page. I think it could be a section on the first page: https://docs.docker.com/guides/cpp/containerize/#run-the-application
The sequence could be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that Pradumna's example is a bit complicated to make multi-stage. If I want to do it on the same page, I have to use my own example for the whole thing. Would that be OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aerabi hi, yea that's fine - multi-stage is such an important strategy, especially for compiled programs, that I think it makes sense to present that as the main workflow.