Did you always wanted to run your API using docker π β
π 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...
.
Table of Contents
- What is this projectβ
- Project setup πΌ
- Running docker π
- Running docker Image π½
- Testing (is it working) β β
- 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
π‘ Prequisite / Assumption
- You have
docker
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
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
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
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
β¬οΈ 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 | |
POST | |
PATCH | |
DELETE |
β¬οΈ 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
see the status column : STATUS
Up About a minute ago
Stop usingΒ :
docker stop
π either
1.CONTAINER ID
```shell
docker stop a5a149a5346
```
or
2.NAMES
```shell
docker stop ecstatic_cray
```
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
β¬οΈ back to top
.
The END
Top comments (0)