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,75 +51,28 @@ 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.
1. In the `getting-started-app` directory, the same location as the
55
+
`package.json` file, create a file named `Dockerfile`. You can use the
56
+
following commands to create a Dockerfile based on your operating system.
107
57
108
58
2. Using a text editor or code editor, add the following contents to the Dockerfile:
109
59
110
60
```dockerfile
111
61
# syntax=docker/dockerfile:1
112
-
113
-
FROM node:18-alpine
62
+
63
+
FROM node:lts-alpine
114
64
WORKDIR /app
115
65
COPY . .
116
66
RUN yarn install --production
117
67
CMD ["node", "src/index.js"]
118
68
EXPOSE 3000
119
69
```
120
70
71
+
This Dockerfile starts off with a `node:lts-alpine` base image, a
72
+
light-weight Linux image that comes with Node.js and the Yarn package
73
+
manager pre-installed. It copies all of the source code into the image,
74
+
installs the necessary dependencies, and starts the application.
75
+
121
76
3. Build the image using the following commands:
122
77
123
78
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.
@@ -127,11 +82,12 @@ To build the image, you'll need to use a Dockerfile. A Dockerfile is simply a te
127
82
```
128
83
129
84
Build the image.
85
+
130
86
```console
131
87
$ docker build -t getting-started .
132
88
```
133
89
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.
90
+
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
91
136
92
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
93
@@ -146,43 +102,42 @@ Now that you have an image, you can run the application in a container using the
146
102
1. Run your container using the `docker run` command and specify the name of the image you just created:
147
103
148
104
```console
149
-
$ docker run -dp 127.0.0.1:3000:3000 getting-started
105
+
$ docker run -d -p 127.0.0.1:3000:3000 getting-started
150
106
```
151
107
152
108
The `-d` flag (short for `--detach`) runs the container in the background.
153
109
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.
110
+
prompt.
156
111
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.
112
+
The `-p` flag (short for `--publish`) creates a port mapping between the
113
+
host and the container. The `-p` flag takes a string value in the format of
114
+
`HOST:CONTAINER`, where `HOST` is the address on the host, and `CONTAINER`
115
+
is the port on the container. The command publishes the container's port
116
+
3000 to `127.0.0.1:3000` (`localhost:3000`) on the host. Without the port
117
+
mapping, you wouldn't be able to access the application from the host.
163
118
164
119
2. After a few seconds, open your web browser to [http://localhost:3000](http://localhost:3000).
165
120
You should see your app.
166
121
167
122

168
-
169
123
170
124
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
125
172
-
173
126
At this point, you have a running todo list manager with a few items.
174
127
175
128
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
129
177
130
{{< tabs >}}
178
131
{{< tab name="CLI" >}}
179
132
180
-
Run the following `docker ps` command in a terminal to list your containers.
133
+
Run the `docker ps` command in a terminal to list your containers.
181
134
182
135
```console
183
136
$ docker ps
184
137
```
138
+
185
139
Output similar to the following should appear.
140
+
186
141
```console
187
142
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
188
143
df784548666d getting-started "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 127.0.0.1:3000->3000/tcp priceless_mcclintock
@@ -204,8 +159,8 @@ In this section, you learned the basics about creating a Dockerfile to build an
0 commit comments