You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
9
11
application using Docker
10
12
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/
14
16
---
15
17
16
18
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
49
51
50
52
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.
51
53
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.
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:
109
56
110
57
```dockerfile
111
58
# syntax=docker/dockerfile:1
112
-
113
-
FROM node:18-alpine
59
+
60
+
FROM node:lts-alpine
114
61
WORKDIR /app
115
62
COPY . .
116
63
RUN yarn install --production
117
64
CMD ["node", "src/index.js"]
118
65
EXPOSE 3000
119
66
```
120
67
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:
122
74
123
75
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.
124
76
@@ -127,11 +79,12 @@ To build the image, you'll need to use a Dockerfile. A Dockerfile is simply a te
127
79
```
128
80
129
81
Build the image.
82
+
130
83
```console
131
84
$ docker build -t getting-started .
132
85
```
133
86
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.
135
88
136
89
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.
137
90
@@ -146,43 +99,42 @@ Now that you have an image, you can run the application in a container using the
146
99
1. Run your container using the `docker run` command and specify the name of the image you just created:
147
100
148
101
```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
150
103
```
151
104
152
105
The `-d` flag (short for `--detach`) runs the container in the background.
153
106
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.
156
108
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.
163
115
164
116
2. After a few seconds, open your web browser to [http://localhost:3000](http://localhost:3000).
165
117
You should see your app.
166
118
167
119

168
-
169
120
170
121
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.
171
122
172
-
173
123
At this point, you have a running todo list manager with a few items.
174
124
175
125
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.
176
126
177
127
{{< tabs >}}
178
128
{{< tab name="CLI" >}}
179
129
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.
181
131
182
132
```console
183
133
$ docker ps
184
134
```
135
+
185
136
Output similar to the following should appear.
137
+
186
138
```console
187
139
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
188
140
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
0 commit comments