DEV Community

Millan
Millan

Posted on • Updated on

Run your first RESTful API / service using docker

Did you always wanted to run your API using docker πŸ‹ ❓
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ run your own micro-service or REST API's
handling : GET , POST , PUT / PATCH , DELETE i.e all CRUD operations ❓

or

πŸ‘‰ you always ran them locally, and wanted to run them inside docker / dockrized with docker images ❓ ❓

Or

πŸ‘‰ you wanted a service to *run locally and do the API testing *, by hitting a API running (in_side_) a docker container ❓ ❓ ❓


.

This article is πŸ’― for you. πŸ‘‡

I will be calling it as node-app-http-docker

I am also going to share the complete source code with you as well. :)

In this article you will learn:

1. How to run a **RESTful service** locally using `docker` πŸ‹ 
     1.1 Code available on **GitHub**
2. `docker build` to create a `docker`image of your project
2. `docker image` to verify if the image was created 
3. `docker run` to run an existing docker image
4. `docker start` and `docker stop` as they sound

πŸ™Œ And few tricks on the way we learn all of the above...
Enter fullscreen mode Exit fullscreen mode


.

Table of Contents

  1. What is this project❓
  2. Project setup πŸ’Ό
  3. Running docker πŸ‹
  4. Running docker Image 🎽
  5. Testing (is it working) βœ… ❎
  6. STOPPING docker (running container) πŸ›‘


.

What is this project❓

node-app-http-docker is a working project (non Prod use)

For getting started with a RESTFUL api server locally using docker

  • It πŸƒruns a server (docker) using nodejs [v16]
  • Exposes following RESTFUL endpoints ( no database required) with all CRUD operations
Rest API call CRUD operation REST endpoints
GET Read http://0.0.0.0:8080/
http://0.0.0.0:8080/health
http://0.0.0.0:8080/api/todos
http://0.0.0.0:8080/api/todos/{id}
PATCH/PUT Update http://0.0.0.0:8080/api/todos/{id}
POST {with body} Create http://0.0.0.0:8080/api/todos
DELETE Delete http://0.0.0.0:8080/api/todos/{id}

You may get 3 types of response

Response Code Response Status
200 OK
201 Created
404 Not Found

⬆️ back to top


.

Project setup πŸ’Ό

Clone the repository on your machine

Using Comand
via https git clone https://github.com/eaccmk/node-app-http-docker.git
via ssh git clone git@github.com:eaccmk/node-app-http-docker.git
cd node-app-http-docker
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Prequisite / Assumption

  • You havedocker installed and running on your machine.

If not, its highly recommended to Get docker

⬆️ back to top


.

Running docker πŸ‹

docker build . -t node-app-http-docker
Enter fullscreen mode Exit fullscreen mode

To know why we used -t Allocate a pseudo-TTY read this stackoverflow thread

Verify docker Image πŸ–ΌοΈ

After docker build is completed, verify if a docker image is created and listed

run docker images

docker images
REPOSITORY                      TAG         IMAGE ID      CREATED         SIZE
localhost/node-app-http-docker  latest      8f74146744df  18 minutes ago  928 MB
Enter fullscreen mode Exit fullscreen mode

You may have more than one row in result, but make sure you have the one with REPOSITORY localhost/node-app-http-docker

also see you got a random (uniqie) IMAGE ID assigned to the image you just created, in my case it was 8f74146744df

⬆️ back to top


.

Running docker Image 🎽

Now that you have a IMAGE ID, lets run that image

docker run -p 8080:8080 8f74146744df
Enter fullscreen mode Exit fullscreen mode

docker run -p <your-port-external>:<docker-internal-port-exposed-for-access> IMAGE_ID

For more details on -p read Publish or expose port (-p, --expose)πŸ”—

❗ open a new tab on terminal and verify this docker (running)

docker ps
Enter fullscreen mode Exit fullscreen mode

⬆️ back to top


.

Testing (is it working βœ… ❎)

Lets hit the docker image as a client / User

Test Type (Positive /Negative) CLIENT On terminal Response SERVER (if Docker running with logs)
βœ… Home Page curl http://0.0.0.0:8080 Welcome, this is your Home page CalledGET : /
❎ Invalid endpoint curl http://0.0.0.0:8080/dascbajb {"message":"Route not found"} CalledGET : /dascbajb
This endpoint is not implemented / unavailable at the moment !!
βœ… health check curl http://0.0.0.0:8080/health {"uptime":29.560686169,
"message":"OK","timestamp":1644057630652}
CalledGET : /health

Some Screen shots from Postman

API Screen
GET Image description
POST Image description
PATCH Image description
DELETE Image description

⬆️ back to top


.

STOPPING docker (docker container) πŸ›‘

First lets find the running one
docker ps

CONTAINER ID  IMAGE                                  COMMAND      CREATED            STATUS                 PORTS                   NAMES
a5a149a53466  localhost/node-app-http-docker:latest  node app.js  About an hour ago  Up About a minute ago  0.0.0.0:8080->8080/tcp  ecstatic_cray
Enter fullscreen mode Exit fullscreen mode

see the status column : STATUS

Up About a minute ago
Enter fullscreen mode Exit fullscreen mode

Stop usingΒ :

docker stop
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ either

1.CONTAINER ID

```shell
docker stop a5a149a5346
```
Enter fullscreen mode Exit fullscreen mode

or

2.NAMES

```shell
docker stop ecstatic_cray
```
Enter fullscreen mode Exit fullscreen mode

In case you want to confirm ---->> run docker ps it should show no running image

docker ps
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES
Enter fullscreen mode Exit fullscreen mode

⬆️ back to top

.

The END

❀️ Follow me πŸ‘‰ https://dev.to/millankaul

Top comments (0)