Skip to content

Commit 158beac

Browse files
committed
get-started: simplify workshop intro, update node version
Signed-off-by: David Karlsson <[email protected]>
1 parent 1ef9019 commit 158beac

File tree

1 file changed

+32
-80
lines changed

1 file changed

+32
-80
lines changed

content/get-started/workshop/02_our_app.md

Lines changed: 32 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
title: Containerize an application
33
weight: 20
44
linkTitle: "Part 1: Containerize an application"
5-
keywords: dockerfile example, Containerize an application, run docker file, running
5+
keywords: |
6+
dockerfile example, Containerize an application, run docker file, running
67
docker file, how to run dockerfile, example dockerfile, how to create a docker container,
78
create dockerfile, simple dockerfile, creating containers
8-
description: Follow this step-by-step guide to learn how to create and run a containerized
9+
description: |
10+
Follow this step-by-step guide to learn how to create and run a containerized
911
application using Docker
1012
aliases:
11-
- /get-started/part2/
12-
- /get-started/02_our_app/
13-
- /guides/workshop/02_our_app/
13+
- /get-started/part2/
14+
- /get-started/02_our_app/
15+
- /guides/workshop/02_our_app/
1416
---
1517

1618
For the rest of this guide, you'll be working with a simple todo
@@ -49,76 +51,26 @@ Before you can run the application, you need to get the application source code
4951

5052
To build the image, you'll need to use a Dockerfile. A Dockerfile is simply a text-based file with no file extension that contains a script of instructions. Docker uses this script to build a container image.
5153

52-
1. In the `getting-started-app` directory, the same location as the `package.json` file, create a file named `Dockerfile`. You can use the following commands to create a Dockerfile based on your operating system.
53-
54-
{{< tabs >}}
55-
{{< tab name="Mac / Linux / Windows (Git Bash)" >}}
56-
57-
In the terminal, run the following commands.
58-
59-
Make sure you're in the `getting-started-app` directory. Replace `/path/to/getting-started-app` with the path to your `getting-started-app` directory.
60-
61-
```console
62-
$ cd /path/to/getting-started-app
63-
```
64-
65-
Create an empty file named `Dockerfile`.
66-
67-
```console
68-
$ touch Dockerfile
69-
```
70-
71-
{{< /tab >}}
72-
{{< tab name="Windows (Command Prompt)" >}}
73-
74-
In the Windows Command Prompt, run the following commands.
75-
76-
Make sure you're in the `getting-started-app` directory. Replace `\path\to\getting-started-app` with the path to your `getting-started-app` directory.
77-
78-
```console
79-
$ cd \path\to\getting-started-app
80-
```
81-
82-
Create an empty file named `Dockerfile`.
83-
84-
```console
85-
$ type nul > Dockerfile
86-
```
87-
88-
{{< /tab >}}
89-
{{< tab name="Windows (PowerShell)" >}}
90-
91-
In PowerShell, run the following commands.
92-
93-
Make sure you're in the `getting-started-app` directory. Replace `\path\to\getting-started-app` with the path to your `getting-started-app` directory.
94-
95-
```console
96-
$ cd \path\to\getting-started-app
97-
```
98-
99-
Create an empty file named `Dockerfile`.
100-
101-
```powershell
102-
$ New-Item -Path . -Name Dockerfile -ItemType File
103-
```
104-
105-
{{< /tab >}}
106-
{{< /tabs >}}
107-
108-
2. Using a text editor or code editor, add the following contents to the Dockerfile:
54+
1. In the `getting-started-app` directory, the same location as the
55+
`package.json` file, create a file named `Dockerfile` with the following contents:
10956

11057
```dockerfile
11158
# syntax=docker/dockerfile:1
112-
113-
FROM node:18-alpine
59+
60+
FROM node:lts-alpine
11461
WORKDIR /app
11562
COPY . .
11663
RUN yarn install --production
11764
CMD ["node", "src/index.js"]
11865
EXPOSE 3000
11966
```
12067

121-
3. Build the image using the following commands:
68+
This Dockerfile starts off with a `node:lts-alpine` base image, a
69+
light-weight Linux image that comes with Node.js and the Yarn package
70+
manager pre-installed. It copies all of the source code into the image,
71+
installs the necessary dependencies, and starts the application.
72+
73+
2. Build the image using the following commands:
12274

12375
In the terminal, make sure you're in the `getting-started-app` directory. Replace `/path/to/getting-started-app` with the path to your `getting-started-app` directory.
12476

@@ -127,11 +79,12 @@ To build the image, you'll need to use a Dockerfile. A Dockerfile is simply a te
12779
```
12880

12981
Build the image.
82+
13083
```console
13184
$ docker build -t getting-started .
13285
```
13386

134-
The `docker build` command uses the Dockerfile to build a new image. You might have noticed that Docker downloaded a lot of "layers". This is because you instructed the builder that you wanted to start from the `node:18-alpine` image. But, since you didn't have that on your machine, Docker needed to download the image.
87+
The `docker build` command uses the Dockerfile to build a new image. You might have noticed that Docker downloaded a lot of "layers". This is because you instructed the builder that you wanted to start from the `node:lts-alpine` image. But, since you didn't have that on your machine, Docker needed to download the image.
13588

13689
After Docker downloaded the image, the instructions from the Dockerfile copied in your application and used `yarn` to install your application's dependencies. The `CMD` directive specifies the default command to run when starting a container from this image.
13790

@@ -146,43 +99,42 @@ Now that you have an image, you can run the application in a container using the
14699
1. Run your container using the `docker run` command and specify the name of the image you just created:
147100

148101
```console
149-
$ docker run -dp 127.0.0.1:3000:3000 getting-started
102+
$ docker run -d -p 127.0.0.1:3000:3000 getting-started
150103
```
151104

152105
The `-d` flag (short for `--detach`) runs the container in the background.
153106
This means that Docker starts your container and returns you to the terminal
154-
prompt. You can verify that a container is running by viewing it in Docker
155-
Dashboard under **Containers**, or by running `docker ps` in the terminal.
107+
prompt.
156108

157-
The `-p` flag (short for `--publish`) creates a port mapping between the host
158-
and the container. The `-p` flag takes a string value in the format of
159-
`HOST:CONTAINER`, where `HOST` is the address on the host, and `CONTAINER` is
160-
the port on the container. The command publishes the container's port 3000 to
161-
`127.0.0.1:3000` (`localhost:3000`) on the host. Without the port mapping,
162-
you wouldn't be able to access the application from the host.
109+
The `-p` flag (short for `--publish`) creates a port mapping between the
110+
host and the container. The `-p` flag takes a string value in the format of
111+
`HOST:CONTAINER`, where `HOST` is the address on the host, and `CONTAINER`
112+
is the port on the container. The command publishes the container's port
113+
3000 to `127.0.0.1:3000` (`localhost:3000`) on the host. Without the port
114+
mapping, you wouldn't be able to access the application from the host.
163115

164116
2. After a few seconds, open your web browser to [http://localhost:3000](http://localhost:3000).
165117
You should see your app.
166118

167119
![Empty todo list](images/todo-list-empty.webp)
168-
169120

170121
3. Add an item or two and see that it works as you expect. You can mark items as complete and remove them. Your frontend is successfully storing items in the backend.
171122

172-
173123
At this point, you have a running todo list manager with a few items.
174124

175125
If you take a quick look at your containers, you should see at least one container running that's using the `getting-started` image and on port `3000`. To see your containers, you can use the CLI or Docker Desktop's graphical interface.
176126

177127
{{< tabs >}}
178128
{{< tab name="CLI" >}}
179129

180-
Run the following `docker ps` command in a terminal to list your containers.
130+
Run the `docker ps` command in a terminal to list your containers.
181131

182132
```console
183133
$ docker ps
184134
```
135+
185136
Output similar to the following should appear.
137+
186138
```console
187139
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
188140
df784548666d getting-started "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 127.0.0.1:3000->3000/tcp priceless_mcclintock
@@ -204,8 +156,8 @@ In this section, you learned the basics about creating a Dockerfile to build an
204156

205157
Related information:
206158

207-
- [Dockerfile reference](/reference/dockerfile/)
208-
- [docker CLI reference](/reference/cli/docker/)
159+
- [Dockerfile reference](/reference/dockerfile/)
160+
- [docker CLI reference](/reference/cli/docker/)
209161

210162
## Next steps
211163

0 commit comments

Comments
 (0)