Getting Started with Docker Desktop
Tags: docker, microservices, programming
Published: 2022-04-12 20:40:29

###What is Docker?
Docker is a platform used to ship microservices in small containers that most likely hold tiny pieces of an entire application. Docker allows for each microservice container to be ran and closed based on its current usage.

###How do we use it?
####Setup
We'll be looking at Docker Desktop to set up our containers but there is also a free web version on Docker's [website](https://labs.play-with-docker.com/).

If you do not already have Docker Desktop and you want to install it, you can follow the tutorial on Docker's [site](https://docs.docker.com/desktop/windows/install/). If you already have it installed or are using the online 'Play with Docker' feel free to follow along on your own machine.

####Steps
#####DockerFile
The first step is to have a program that we want to ship to Docker. For this example, I'm using a simple to-do app found on github [here](https://github.com/heyMP/ist402-docker/tree/master/labs/5-nodejs-todo-app). If you look through the files on this to-do app, you'll find a 'DockerFile'. This file is needed to be able to ship anything to Docker, this is what tells Docker what to do with your files. 

In this to-do app we have a few lines in our DockerFile:
```
FROM node:10-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "/app/src/index.js"]
EXPOSE 3000
```
These lines define our Docker container configuration and it runs a few commands in our container. `COPY . .` gets our code in the container then the `RUN` line installs all of our dependencies needed for the app, `CMD` runs a few more needed commands for this app and finally `EXPOSE` declares the port we want to run the container on.

#####Build
Next, we want to build an image of our application. We'll build this by using our command line. We can navigate to the to-do app folder, '5-nodejs-todo-app', in our command prompt and run the command:
`docker build -t [appName]`.
`docker build` is what initiates the building of our image. `-t` indicates the name of the image will come next in the command. `[appName]` stands for the place where you insert whatever name you want (usually relating to what the image is).
For this example, I used `docker build -t todoapp`.

If you are using Docker Desktop you'll see a new image appear under the 'Images' tab.

![Docker Images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bgvboqj6dh2oc2cvlhv.png)
#####Run
Finally, we can run our application with Docker. Once the image is built, there are two ways we can create and run a container based on that image. The first we'll look at is in the command prompt. We can run our container by writing the command:
`docker container run --name [nameForContainer] -p [hostPort]:[containerPort] -d [image ID]`
`docker container run` is the command to begin and run our container. `--name` says the name of the container we are making is coming next. `[nameForContainer]` can be any name that describes the container you are currently making (I used todoapp again). `-p` defines the ports we went to use. `[hostPort]` is the port you want to open on your machine to run the Docker container. `[conatinerPort]` is the port in the container you're making that links to the open port on your computer. `-d` says the container should run detached from the command prompt we are in (this is because I'd like to see the feedback in the Docker Desktop rather than the command prompt). `[image ID]` is where we place the ID of the image we made in the previous command. This ID can be found on Docker Desktop under the images tab, named 'Image ID'.

The full command I used is:
`docker container run --name todoapp -p 3000:3000 -d e02b378878b0`

###You're All Set!
Head to `localhost:3000` in your browser and you should see your To-Do app running!

![Todo App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgojwulkcyjq8lwelxj5.JPG)