# Dockerizing an application

Containers are made up of images. Images contain your project and it's dependencies.&#x20;

{% tabs %}
{% tab title="Exercise" %}
* Add a Dockerfile to the start project. The Dockerfile should do the following...
  * Include a base Node image
  * Copy all the project files into the image
  * Run an npm install
  * Expose port 3000
  * Run npm start
* Build the image
{% endtab %}

{% tab title="Answer" %}
* Open the Command Palette (**Cmd/Ctrl + Shift + P**)
* Select "Add Dockefiles to workspace"

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5k9ehyr0vtJqcloRd%2Fimage.png?alt=media\&token=04a4b1df-c092-4460-af0f-dc745fdc0e6d)

* Select the "start" project

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5kNgnZz-MyvIpclKp%2Fimage.png?alt=media\&token=a3810ed2-04c1-4180-9653-e2f278d553c3)

* Select "Node.js" from the prompt

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5kW5xSWtLZs8hLKPW%2Fimage.png?alt=media\&token=de3db870-aab7-4e84-9d0f-174c9a62a387)

* Enter "3000" in the prompt

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5kdiTNV98EX7NC8Qc%2Fimage.png?alt=media\&token=0ffd91c1-46d9-46c3-941f-95711cff79fe)

* VS Code will add "Dockerfile", "docker-compose.yml" and "debug.docker-compose.yml" files to the project
* Open the "Dockerfile" in the "start" project
* Add two ENV variables, "API\_BASE" and "PORT"

```
FROM node:10.13-alpine
ENV NODE_ENV production
ENV API_BASE https://lifx-lamp-api.azurewebsites.net/api
ENV PORT 3000
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 3000
CMD npm start
```

* Open the Command Palette (**Cmd/Ctrl + Shift + P**)
* Select "Docker Images: Build Image"

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5kztfOb6ykQWVN5dr%2Fimage.png?alt=media\&token=e106ad64-fb0c-4820-9d1a-53d7e4cd2f7b)

* Select "start" as the workspace folder

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5lH52aZYdl5Q4XU2D%2Fimage.png?alt=media\&token=7dc6e3a1-c0e2-4902-ade6-86a589e3e167)

* Take the default image tag

![](https://1151923643-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LlvtEHKkq5bvzPq1pJ4%2F-Lm6AZcBldO9D1ZxI2KQ%2F-Lm5lNxLbtU21WKSWUQy%2Fimage.png?alt=media\&token=94cace0e-c5d8-4846-b85d-042bb9344997)

* An integrated terminal instance will open and Docker will build the image

{% hint style="info" %}
Note that if you get an error saying "cannot connect to Docker daemon", make sure that Docker is installed and running on your machine.
{% endhint %}
{% endtab %}
{% endtabs %}



