How to: Docker, typescript, and node
Tags: docker, typescript, node, tutorial
Published: 2023-05-04 17:28:33

## Why Docker?

To put it simply, we want to replicate the same environment on every machine our application will run on. 

Imagine my teammate an I are working with two different operating systems/machines, each of us has a different version of node installed. Well everything might be fine and dandy on my end but they might run into all kinds of node mismatch issues on their's. This is where Docker comes in.

The Docker container replicates a desired standard environment for which our application will run in. So instead of directly running our application on whatever local machine and it's environment, we will run a docker container with that replicates the same environmental conditions (every time, everywhere) in which our application will run inside.

## Part 1: Checking your scripts

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ffzxbg3xphtpk3erzv0.png)

My build script will spit out a transpiled `index.js` file in   `./app/server/build` folder.

My start script will run the built `index.js` file found in said folder.

Make sure your scripts are working correctly as Docker will run these scripts to run your application inside the container.

## Part 2: The dockerfile & building your image

The dockerfile is just a text document that contains the commands for docker to build your docker image. We can have different images with varying settings.

**Hold on, what is a docker image?**
* a docker image is an immutable file that contains the source code, libraries, dependancies, tools, and other files that are needed for a container to run. Essentially containers are **instances** of images.

Create the `dockerfile`, and inside... 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5czqnygses9o9cy953y.png)

So let's go line by line and figure out what's happening here.

1. `FROM node:18-alpine`
 * We specify a node version, alpine is just a lightweight/barebones node version used to run inside our container

2. `WORKDIR /usr/src/app`
 * In our instanced environment we need to create a new directory for our files to live in

3. `COPY package.json yarn.lock ./`
 * Copy first our `package.json` and `yarn.lock` files into the root directory we just created, so we can install our dependencies needed for our application

4. `RUN yarn install`
 * Run the given command to install dependancies

5. `COPY . .`
 * Copy over the rest of our files

6. `EXPOSE 8080`
 * Which port do we want our application to run on? In my node app I specified port 8080 so here I will do the same

7. `RUN yarn build`
 * Now we build to get our `index.js` file spat out into the build directory

8. `CMD ["yarn", "start"]`
 * And lastly we start our application

## Part 3: .dockerignore file

We want to keep our container instance slim, so we'll went to exclude unnecessary files. We can do that by first creating a `.dockerignore` file in the root directory and filling it out with...


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbb2hrnshrjx7g9e1dz2.png)


## Part 4: Running our container

If you haven't already you'll want to download the latest version of Docker Desktop for your computer, and then start the application.

1. Navigate to where your dockerfile is located in your terminal and then run the docker command `docker build . -t imageNameOfChoice` the `-t` flag allows you to give a name/tag to your image that you are about to build

2. Once that has finished building we want to instance/run a container from the image that we just built. Run the docker command `docker run -d imageNameOfChoice`. the `-d` flag runs our container in detached mode, meaning it is disconnected from the current terminal so we can continue to use other commands. This mode means the container receives no input and displays no output.
 * Detached mode will spit out a long id that references your container

3. You can check the currently running docker container/containers by running the docker command `docker ps`. You will see the randomly generated name of your running container along with the id that was given to you from running in detached mode.

4. Visit `localhost:8080` and see your container running.

4. To stop your container simply run `docker stop randomlyGeneratedContainerName`

## Conclusion

So far we've learned how to write a setup a dockerfile to create an image and build and run a container. Stay tuned for more on how to connect other services like a DB/Redis in the future!