Skip to content

Commit 855bb90

Browse files
hubwritergithub-actions
and
github-actions
authored
Codespaces: Enhanced Codespaces base image policy - [GA] (#48652)
Co-authored-by: github-actions <github-actions@github.com>
1 parent 50e742a commit 855bb90

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed
Loading

content/codespaces/developing-in-a-codespace/rebuilding-the-container-in-a-codespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If you want to preserve files outside the `/workspaces` directory over a rebuild
4949

5050
```json
5151
{
52-
"image": "mcr.microsoft.com/vscode/devcontainers/base:alpine",
52+
"image": "mcr.microsoft.com/devcontainers/base:alpine",
5353
"postCreateCommand": ".devcontainer/postCreate.sh"
5454
}
5555
```

content/codespaces/managing-codespaces-for-your-organization/restricting-the-base-image-for-codespaces.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,32 @@ When you create a codespace, a development container is automatically created on
1919
You can choose which image you want to use for your codespaces by specifying it in the dev container configuration for a repository. You can do this, for example, by using the `image` property in the `devcontainer.json` file.
2020

2121
```json copy
22-
"image": "mcr.microsoft.com/vscode/devcontainers/javascript-node:18",
22+
"image": "mcr.microsoft.com/devcontainers/javascript-node:18",
2323
```
2424

25-
For more information, see the [dev containers specification](https://containers.dev/implementors/json_reference/) on the Development Containers website.
25+
For more information, see the [dev containers specification](https://containers.dev/implementors/json_reference/#image-specific) on the Development Containers website.
26+
27+
Alternatively, you can specify the base image in a Dockerfile. For more information, see "[AUTOTITLE](/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#dockerfile)."
2628

2729
If you don't specify an image in the dev container configuration for a repository, the default dev container image is used. The default image contains a number of runtime versions for popular languages and commonly used tools. For more information, see "[AUTOTITLE](/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#using-the-default-dev-container-configuration)."
2830

2931
As an organization owner, you can add a policy to restrict which dev container images can be used for codespaces created within your organization.
3032

31-
If the image specified in the dev container configuration does not match one of the allowed images, the following message is displayed when someone tries to create a codespace for the repository:
33+
If you attempt to create a codespace using a `devcontainer.json` that specifies an image that is not allowed by the policy, the following message is displayed in {% data variables.product.prodname_dotcom_the_website %}:
34+
35+
> The codespace cannot be created because the image specified in your dev container configuration is not allowed. You may need to select a different branch, modify your container configuration, or adjust your organization's policy setting.
36+
37+
Using the CLI, the error message is:
3238

33-
> Codespace could not be created: Base image 'DETAILS FROM DEV CONTAINER CONFIGURATION' is not allowed based on an organization policy set by your organization owner.
39+
> error creating codespace: HTTP 400: Base image 'IMAGE-REFERENCE' is not allowed based on an organization policy set by your organization administrator.
40+
41+
If the image for a codespace is specified in a Dockerfile, and does not match the policy, a codespace is created in recovery mode, using the default recovery container rather than the specified image. A message to this effect is included at the end of the creation log. For more information about the creation log, see "[AUTOTITLE](/codespaces/troubleshooting/github-codespaces-logs#creation-logs)."
3442

3543
{% note %}
3644

3745
**Notes**:
3846
- The dev container base image should not be confused with the host image. The host image is the image used to build the virtual machine on which the dev container runs. For more information, see "[AUTOTITLE](/codespaces/setting-your-user-preferences/choosing-the-stable-or-beta-host-image)."
39-
- The base image policy is only applied when a codespace is created. It is currently not applied when you rebuild a container. This will change in a future release. For more information, see "[AUTOTITLE](/codespaces/getting-started/understanding-the-codespace-lifecycle#rebuilding-a-codespace)."
47+
- The base image policy is applied when a codespace is created, and when you perform a full container rebuild. For more information, see "[AUTOTITLE](/codespaces/getting-started/understanding-the-codespace-lifecycle#rebuilding-a-codespace)."
4048
- The base image policy does not apply to the default image, or the image that's used to recover a codespace if an error is introduced into a dev container configuration which prevents the container from being rebuilt.
4149

4250
{% endnote %}
@@ -56,18 +64,17 @@ For example, you could create an organization-wide policy that restricts the bas
5664
{% data reusables.codespaces.codespaces-org-policies %}
5765
1. Click **Add constraint** and choose **Base images**.
5866
1. Click {% octicon "pencil" aria-label="Edit policy" %} to edit the constraint.
59-
1. In the "Allowed values" field, enter the complete URL of an image you want to allow.
67+
1. In the "Allowed values" field, enter the image reference of the Docker image you want to allow.
6068

61-
![Screenshot of the URL "mcr.microsoft.com/vscode/devcontainers/java" entered in the "Allowed values" field.](/assets/images/help/codespaces/image-allowed-values.png)
69+
![Screenshot of the image reference "mcr.microsoft.com/devcontainers/java" entered in the "Allowed values" field.](/assets/images/help/codespaces/image-allowed-values.png)
6270

63-
{% note %}
71+
You can use the `*` wildcard as the last character of the image reference to match all images that start with the same reference to the left of the wildcard. For example, `mcr.microsoft.com/devcontainers/*`.
6472

65-
**Note**: You must specify an image URL that exactly matches the value specified in a dev container configuration.
73+
1. Click {% octicon "plus" aria-label="Add button" %} to add the value.
74+
1. If required, repeat the previous two steps to add more image references.
6675

67-
{% endnote %}
76+
When you add multiple image references, if an image reference specified in the dev container configuration of a repository does not match any of the references in a policy that applies to the repository, you will not be able to create codespaces for that repository.
6877

69-
1. Click {% octicon "plus" aria-label="Add button" %} to add the value.
70-
1. If required, repeat the previous two steps to add more image URLs.
7178
1. Click outside of the dialog box to close it.
7279
{% data reusables.codespaces.codespaces-policy-targets %}
7380
1. If you want to add another constraint to the policy, click **Add constraint** and choose another constraint. For information about other constraints, see:
@@ -88,7 +95,7 @@ You can edit an existing policy. For example, you may want to add or remove cons
8895
1. Display the "Codespaces policies" page. For more information, see "[Adding a policy to define the allowed images](#adding-a-policy-to-define-the-allowed-images)."
8996
1. Click the name of the policy you want to edit.
9097
1. Beside the "Base images" constraint, click {% octicon "pencil" aria-label="Edit policy" %}.
91-
1. Add or remove image URLs.
98+
1. Add or remove image references.
9299
1. Click **Save**.
93100

94101
## Deleting a policy

content/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,35 @@ The following example uses four instructions:
7777

7878
`ARG` defines a build-time variable.
7979

80-
`FROM` specifies the parent image on which the generated Docker image will be based.
80+
`FROM` specifies the parent image on which the generated Docker image will be based. If a base image policy has been configured, allowing only certain images to be used, the specified image must match one of the image references in the policy. If it does not, codespaces for this repository will be created in recovery mode. For more information, see "[AUTOTITLE](/codespaces/managing-codespaces-for-your-organization/restricting-the-base-image-for-codespaces)."
8181

82-
`COPY` copies a file and adds it to the filesystem.
82+
`COPY` copies a file from the repository and adds it to the filesystem of the codespace.
8383

8484
`RUN` updates package lists and runs a script. You can also use a `RUN` instruction to install software, as shown by the commented out instructions. To run multiple commands, use `&&` to combine the commands into a single `RUN` statement.
8585

8686
```dockerfile copy
87-
ARG VARIANT="16-buster"
88-
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
87+
ARG VARIANT="16"
88+
FROM mcr.microsoft.com/devcontainers/javascript-node:1-${VARIANT}
8989

90-
# [Optional] Uncomment if you want to install an additional version of node using nvm
91-
# ARG EXTRA_NODE_VERSION=10
92-
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
90+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
91+
&& apt-get -y install --no-install-recommends bundler
9392

94-
# [Optional] Uncomment if you want to install more global node modules
95-
# RUN su node -c "npm install -g <your-package-list-here>"
93+
# [Optional] Uncomment if you want to install an additional version
94+
# of node using nvm
95+
# ARG EXTRA_NODE_VERSION=18
96+
# RUN su node -c "source /usr/local/share/nvm/nvm.sh \
97+
# && nvm install ${EXTRA_NODE_VERSION}"
9698

97-
COPY library-scripts/github-debian.sh /tmp/library-scripts/
98-
RUN apt-get update && bash /tmp/library-scripts/github-debian.sh
99+
COPY ./script-in-your-repo.sh /tmp/scripts/script-in-codespace.sh
100+
RUN apt-get update && bash /tmp/scripts/script-in-codespace.sh
99101
```
100102

103+
{% note %}
104+
105+
**Note**: In the above example, the script that's copied to the codespace (`script-in-your-repo.sh`) must exist in your repository.
106+
107+
{% endnote %}
108+
101109
For more information about Dockerfile instructions, see "[Dockerfile reference](https://docs.docker.com/engine/reference/builder)" in the Docker documentation.
102110

103111
#### Using a Dockerfile

data/reusables/codespaces/codespaces-policy-targets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
{% note %}
66

7-
**Note**: If you're adding a constraint to a policy that already contains the "Maximum codespaces per user" constraint, you won't be able to apply the policy to selected repositories. This is because the "Maximum codespaces per user" constraint always applies to all repositories in the organization.
7+
**Note**: If you're adding a constraint to an existing policy that already contains the "Maximum codespaces per user" constraint, you won't be able to apply the policy to selected repositories. This is because the "Maximum codespaces per user" constraint always applies to all repositories in the organization.
88

99
{% endnote %}
1010

0 commit comments

Comments
 (0)