Skip to content

Commit 0136309

Browse files
authored
Merge pull request #9 from paulbouwer/fixes-and-updates
Fixes and updates
2 parents ec9d949 + 30a94cc commit 0136309

File tree

10 files changed

+788
-756
lines changed

10 files changed

+788
-756
lines changed

.devcontainer/Dockerfile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#-------------------------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
4+
#-------------------------------------------------------------------------------------------------------------
5+
6+
FROM node:13
7+
8+
# Avoid warnings by switching to noninteractive
9+
ENV DEBIAN_FRONTEND=noninteractive
10+
11+
# The node image includes a non-root user with sudo access. Use the "remoteUser"
12+
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
13+
# will be updated to match your local UID/GID (when using the dockerFile property).
14+
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
15+
ARG USERNAME=node
16+
ARG USER_UID=1000
17+
ARG USER_GID=$USER_UID
18+
19+
# Configure apt and install packages
20+
RUN apt-get update \
21+
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
22+
#
23+
# Verify git and needed tools are installed
24+
&& apt-get -y install git iproute2 procps \
25+
#
26+
# Remove outdated yarn from /opt and install via package
27+
# so it can be easily updated via apt-get upgrade yarn
28+
&& rm -rf /opt/yarn-* \
29+
&& rm -f /usr/local/bin/yarn \
30+
&& rm -f /usr/local/bin/yarnpkg \
31+
&& apt-get install -y curl apt-transport-https lsb-release \
32+
&& curl -sS https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/pubkey.gpg | apt-key add - 2>/dev/null \
33+
&& echo "deb https://dl.yarnpkg.com/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
34+
&& apt-get update \
35+
&& apt-get -y install --no-install-recommends yarn \
36+
#
37+
# Install eslint globally
38+
&& npm install -g eslint \
39+
#
40+
# [Optional] Update a non-root user to UID/GID if needed.
41+
&& if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then \
42+
groupmod --gid $USER_GID $USERNAME \
43+
&& usermod --uid $USER_UID --gid $USER_GID $USERNAME \
44+
&& chown -R $USER_UID:$USER_GID /home/$USERNAME; \
45+
fi \
46+
# [Optional] Add add sudo support for non-root user
47+
&& apt-get install -y sudo \
48+
&& echo node ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
49+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
50+
#
51+
# Clean up
52+
&& apt-get autoremove -y \
53+
&& apt-get clean -y \
54+
&& rm -rf /var/lib/apt/lists/*
55+
56+
# Switch back to dialog for any ad-hoc use of apt-get
57+
ENV DEBIAN_FRONTEND=dialog

.devcontainer/devcontainer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
2+
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/javascript-node-12
3+
{
4+
"name": "Node.js 13",
5+
"dockerFile": "Dockerfile",
6+
7+
// Use 'settings' to set *default* container specific settings.json values on container create.
8+
// You can edit these settings after create using File > Preferences > Settings > Remote.
9+
"settings": {
10+
"terminal.integrated.shell.linux": "/bin/bash"
11+
},
12+
13+
// Use 'appPort' to create a container with published ports. If the port isn't working, be sure
14+
// your server accepts connections from all interfaces (0.0.0.0 or '*'), not just localhost.
15+
"appPort": [ 8080 ],
16+
17+
// Uncomment the next line to run commands after the container is created.
18+
// "postCreateCommand": "yarn install",
19+
20+
// Uncomment the next line to have VS Code connect as an existing non-root user in the container.
21+
// On Linux, by default, the container user's UID/GID will be updated to match your local user. See
22+
// https://aka.ms/vscode-remote/containers/non-root for details on adding a non-root user if none exist.
23+
// "remoteUser": "node",
24+
25+
// Add the IDs of extensions you want installed when the container is created in the array below.
26+
"extensions": [
27+
"dbaeumer.vscode-eslint"
28+
]
29+
}

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
FROM node:8.1.0-alpine
1+
FROM node:13.6.0-alpine
22

33
ARG IMAGE_CREATE_DATE
44
ARG IMAGE_VERSION
55
ARG IMAGE_SOURCE_REVISION
66

77
# Metadata as defined in OCI image spec annotations - https://github.com/opencontainers/image-spec/blob/master/annotations.md
88
LABEL org.opencontainers.image.title="Hello Kubernetes!" \
9-
org.opencontainers.image.description="Provides a demo image to deploy to a Kubernetes cluster. It displays a message, the name of the pod and details of the node it's deployed to." \
9+
org.opencontainers.image.description="Provides a demo image to deploy to a Kubernetes cluster. It displays a message, the name of the pod and details of the node it is deployed to." \
1010
org.opencontainers.image.created=$IMAGE_CREATE_DATE \
1111
org.opencontainers.image.version=$IMAGE_VERSION \
1212
org.opencontainers.image.authors="Paul Bouwer" \
@@ -28,4 +28,5 @@ RUN npm install
2828
# Bundle app source
2929
COPY . /usr/src/app
3030

31+
USER node
3132
CMD [ "npm", "start" ]

README.md

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
# Hello Kubernetes!
22

3-
This image can be deployed on a Kubernetes cluster. It displays:
3+
This container image can be deployed on a Kubernetes cluster. When accessed via a web browser on port `8080`, it will display:
44
- a default **Hello world!** message
55
- the pod name
66
- node os information
77

8-
The default "Hello world!" message displayed can be overridden using the `MESSAGE` environment variable.
8+
![Hello world! from the hello-kubernetes image](hello-kubernetes.png)
99

10-
The default port of 8080 can be overriden using the `PORT` environment variable.
10+
The default "Hello world!" message displayed can be overridden using the `MESSAGE` environment variable. The default port of 8080 can be overriden using the `PORT` environment variable.
1111

1212
## DockerHub
1313

1414
It is available on DockerHub as:
1515

16-
- [paulbouwer/hello-kubernetes:1.5](https://hub.docker.com/r/paulbouwer/hello-kubernetes/)
16+
- [paulbouwer/hello-kubernetes:1.6](https://hub.docker.com/r/paulbouwer/hello-kubernetes/)
1717

1818
## Deploy
1919

20-
You can deploy the image to your Kubernetes cluster one of two ways:
20+
### Standard Configuration
2121

22-
Deploy using the hello-kubernetes.yaml, which contains definitions for the service and deployment objects:
22+
Deploy to your Kubernetes cluster using the hello-kubernetes.yaml, which contains definitions for the service and deployment objects:
2323

2424
```yaml
2525
# hello-kubernetes.yaml
26-
2726
apiVersion: v1
2827
kind: Service
2928
metadata:
@@ -52,7 +51,7 @@ spec:
5251
spec:
5352
containers:
5453
- name: hello-kubernetes
55-
image: paulbouwer/hello-kubernetes:1.5
54+
image: paulbouwer/hello-kubernetes:1.6
5655
ports:
5756
- containerPort: 8080
5857
```
@@ -61,30 +60,20 @@ spec:
6160
$ kubectl apply -f yaml/hello-kubernetes.yaml
6261
```
6362

64-
Or, deploy by executing the following `run` and `expose` commands on `kubectl`.
65-
66-
```bash
67-
$ kubectl run hello-kubernetes --replicas=3 --image=paulbouwer/hello-kubernetes:1.5 --port=8080
68-
$ kubectl expose deployment hello-kubernetes --type=LoadBalancer --port=80 --target-port=8080 --name=hello-kubernetes
69-
```
70-
7163
This will display a **Hello world!** message when you hit the service endpoint in a browser. You can get the service endpoint ip address by executing the following command and grabbing the returned external ip address value:
7264

7365
```bash
7466
$ kubectl get service hello-kubernetes
7567
```
7668

77-
## Custom Message
78-
79-
You can customise the message displayed by the `hello-kubernetes` container as follows:
69+
### Customise Message
8070

81-
Deploy using the hello-kubernetes.custom-message.yaml, which contains definitions for the service and deployment objects:
71+
You can customise the message displayed by the `hello-kubernetes` container. Deploy using the hello-kubernetes.custom-message.yaml, which contains definitions for the service and deployment objects.
8272

8373
In the definition for the deployment, add an `env` variable with the name of `MESSAGE`. The value you provide will be displayed as the custom message.
8474

8575
```yaml
8676
# hello-kubernetes.custom-message.yaml
87-
8877
apiVersion: v1
8978
kind: Service
9079
metadata:
@@ -113,7 +102,7 @@ spec:
113102
spec:
114103
containers:
115104
- name: hello-kubernetes
116-
image: paulbouwer/hello-kubernetes:1.5
105+
image: paulbouwer/hello-kubernetes:1.6
117106
ports:
118107
- containerPort: 8080
119108
env:
@@ -125,14 +114,7 @@ spec:
125114
$ kubectl apply -f yaml/hello-kubernetes.custom-message.yaml
126115
```
127116

128-
Or, deploy by executing the following `run` and `expose` commands on `kubectl`, with the environment variable `MESSAGE` provided as part of the `run` command.
129-
130-
```bash
131-
$ kubectl run hello-kubernetes --replicas=3 --image=paulbouwer/hello-kubernetes:1.5 --port=8080 --env="MESSAGE=I just deployed this on Kubernetes!"
132-
$ kubectl expose deployment hello-kubernetes --type=LoadBalancer --port=80 --target-port=8080 --name=hello-kubernetes
133-
```
134-
135-
## Custom Port
117+
### Specify Custom Port
136118

137119
By default, the `hello-kubernetes` app listens on port `8080`. If you have a requirement for the app to listen on another port, you can specify the port via an env variable with the name of PORT. Remember to also update the `containers.ports.containerPort` value to match.
138120

@@ -155,7 +137,7 @@ spec:
155137
spec:
156138
containers:
157139
- name: hello-kubernetes
158-
image: paulbouwer/hello-kubernetes:1.5
140+
image: paulbouwer/hello-kubernetes:1.6
159141
ports:
160142
- containerPort: 80
161143
env:
@@ -164,16 +146,24 @@ spec:
164146
```
165147
166148
167-
## Build
149+
## Build Container Image
168150
169151
If you'd like to build the image yourself, then you can do so as follows. The `build-arg` parameters provides metadata as defined in [OCI image spec annotations](https://github.com/opencontainers/image-spec/blob/master/annotations.md).
170152

171153
Bash
172154
```bash
173-
$ docker build --no-cache --build-arg IMAGE_VERSION="1.5" --build-arg IMAGE_CREATE_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`" --build-arg IMAGE_SOURCE_REVISION="`git rev-parse HEAD`" -f Dockerfile -t "hello-kubernetes:1.5" .
155+
$ docker build --no-cache --build-arg IMAGE_VERSION="1.6" --build-arg IMAGE_CREATE_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`" --build-arg IMAGE_SOURCE_REVISION="`git rev-parse HEAD`" -f Dockerfile -t "hello-kubernetes:1.6" .
174156
```
175157

176158
Powershell
177159
```powershell
178-
PS> docker build --no-cache --build-arg IMAGE_VERSION="1.5" --build-arg IMAGE_CREATE_DATE="$(Get-Date((Get-Date).ToUniversalTime()) -UFormat '%Y-%m-%dT%H:%M:%SZ')" --build-arg IMAGE_SOURCE_REVISION="$(git rev-parse HEAD)" -f Dockerfile -t "hello-kubernetes:1.5" .
179-
```
160+
PS> docker build --no-cache --build-arg IMAGE_VERSION="1.6" --build-arg IMAGE_CREATE_DATE="$(Get-Date((Get-Date).ToUniversalTime()) -UFormat '%Y-%m-%dT%H:%M:%SZ')" --build-arg IMAGE_SOURCE_REVISION="$(git rev-parse HEAD)" -f Dockerfile -t "hello-kubernetes:1.6" .
161+
```
162+
163+
## Develop Application
164+
165+
If you have [VS Code](https://code.visualstudio.com/) and the [Visual Studio Code Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension installed, the `.devcontainer` folder will be used to build a container based node.js 13 development environment.
166+
167+
Port `8080` has been configured to be forwarded to your host. If you run `npm start` in the VS Code Remote Containers terminal, you will be able to access the website on `http://localhost:8080`. You can change the port in the `.devcontainer\devcontainer.json` file under the `appPort` key.
168+
169+
See [here](https://code.visualstudio.com/docs/remote/containers) for more details on working with this setup.

hello-kubernetes.png

25.4 KB
Loading

0 commit comments

Comments
 (0)