Creating a DockerFile for an Application
Published: 2022-02-25 20:52:25

## What is Docker? ##
Docker is a platform for developing, shipping, and running applications. Docker is a very powerful tool because it allows you to run applications in environments called containers. Numerous containers can be run on a single host, and they provide isolation and security for the applications packaged inside. The containers themselves are lightweight and contain everything needed to run the application, so nothing is required from the host to run anything. Docker provides fast, consistent delivery, and rapid deployment of applications. In this article we will walk through how to deploy an app with Docker, and see how to create a DockerFile for an existing application

## 'Play with Docker' ##
'Play with Docker' is an easy way to use Docker and deploy applications. To start using it, first sign up for [Docker Hub](https://hub.docker.com/signup). Once you have an account, go [here](https://labs.play-with-docker.com/) and log into your Docker account to start using 'Play with Docker'. Sessions last 4 hours, and all work is deleted once the time is up.

## Deploying the NewsAPI Microservice ##
**First Steps**
Within the playground, click '+ADD NEW INSTANCE'. Once the console loads, run `git clone https://github.com/heyMP/news-api-workshop && cd news-api-workshop` to clone the repository with the API to and change directory to the local repo. An API key and the url of your Docker playground need to be specified, so run `cp dot.env.example dot.env` to generate the settings file. A dot.env file was created, so navigate to the 'EDITOR' button and find it. Once opened, you should see a file similar to the following screenshot, with fields for the API key and url (NEWS_ENDPOINT).
![dot.env file inside EDITOR](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayz0jcx3ao45rklg2c6z.png)

**Get API Key and URL of Docker Playground**
Go to the [News API](newsapi.org) site to create an account, login, and generate an API key. Once you obtain the key, paste it into the NEWS_SERVICE_API_KEY field in the dot.env file. To get the url for the news endpoint, go back to your Docker playground and click 'OPEN PORT'. When prompted, set port `4000` to be opened. Click OK, and you should see a blank page. Copy that url and paste it into the NEWS_ENDPOINT field in the dot.env file. Save the dot.env file.

**Run Docker**
Back in the terminal, run `docker-compose up` to build the Docker image. Two ports will show up as opened, `80` and `4000`. If you've done everything properly, you will be taken to a screen that looks like the following screenshot when you click `80`.
![News API working properly](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fnc579zt2q6pfjxyj1n.png)

## Creating a DockerFile for an Existing Application ##
Getting an existing app to work with Docker is simple; you just need a DockerFile! An example DockerFile can be found below:
```
FROM node:12-alpine as dev
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm", "start" ]

FROM node:12-alpine as build
WORKDIR /app
COPY --from=dev /app /app
RUN npm build
CMD [ "npm", "build" ]

FROM nginx as prod
COPY --from=build /app/_site /usr/share/nginx/html
```
This should be named `Dockerfile` and placed in the root directory of your application. Note that this is not the Docker image required to run the application, this is only the text file with a list of commands to be used by a Docker command. The command is `docker build -t <appname> .`, which builds the application using the Dockerfile. **Do not forget the `.` at the end of the command.** After this command executes, a container can be instantiated by running the command `docker run -d -p 3000:3000 <appname>`. Your application is now deployed with Docker!