Dockerizing

By Raúl on May 13, 2022
Image courtesy of unsplash

I wanted to work an a simple example for a full end-to-end containerized solution.

Let’s start with the backend that hits a DB and a 3rd party API (to save a secret key for example).

DB container

Start by downloading the image

docker pull postgres

And running docker engine in localhost. -e is for provide ENV vars. -d for running in dettached mode.

docker run --name my-local-postgres -e POSTGRES_PASSWORD=password -d postgres
## Stop and delete the container 
docker stop my-local-postgres
docker rm my-local-postgres

On this case I wanted to be more specific providing a DB and user (for schema)

docker run --name my-local-postgres -e POSTGRES_USER=scott -e POSTGRES_PASSWORD=tiger -d -p 5432:5432 postgres

It’s more useful if database is not empty, so here DB includes a table and initial data

docker run --name my-local-postgres -e POSTGRES_USER=scott -e POSTGRES_PASSWORD=tiger -d -p 5432:5432 -rm -v $(pwd)/init.sql:/docker-entrypoint-initdb.d/init.sql postgres

Alternatively, I could use my own image like this:

Creating a .Dockerfile

FROM postgres 
ENV POSTGRES_USER scott
ENV POSTGRES_PASSWORD tiger 
ENV POSTGRES_DB scott 
COPY init.sql /docker-entrypoint-initdb.d/

running a docker build to generate it

docker build -t my-own-postgres .

and run a container that use such image

docker run -d --name my-local-postgres -p 5432:5432 --rm  my-own-postgres

Node app container

Anyway, as DB is running, let’s do the backend portion (which will connect to the DB)

This is the backend Dockerfile

RUN mkdir -p /app && chown -R node:node /app
USER node
WORKDIR /app

COPY ./package*.json /app/
RUN npm install

COPY --chown=node:node *.js /app/

ENV NPM_CONFIG_PRODUCTION=false

CMD ["npm", "start"]

I build the image

docker build -t mentoring-app-backend .

And run the container

docker run -p 3000:3000 --name bakend -d --rm mentoring-app-backend

It gives me a UUID but the localhost:3000 service does not work.

How to diagnose a Dockerized app?

docker events&
╰─○ docker run -p 3000:3000 --name bakend -d --rm mentoring-app-backend
2022-04-19T20:02:11.480402900-04:00 container create 5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91 (image=mentoring-app-backend, name=bakend)
5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91
2022-04-19T20:02:11.558630300-04:00 network connect bff6cfdffe3d5b9208728118e7e3844fbd464d6c25e4e84f95fa710842e55add (container=5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91, name=bridge, type=bridge)
2022-04-19T20:02:11.813353400-04:00 container start 5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91 (image=mentoring-app-backend, name=bakend)
╭─raul.cifuentes at PRODIGYMAC-C02DT3MFML85 in ~/workspaces/sandboxes/mentoring/back 22-04-19 - 20:02:11
╰─○ 2022-04-19T20:02:13.414486600-04:00 container die 5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91 (exitCode=2, image=mentoring-app-backend, name=bakend)
2022-04-19T20:02:13.471939800-04:00 network disconnect bff6cfdffe3d5b9208728118e7e3844fbd464d6c25e4e84f95fa710842e55add (container=5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91, name=bridge, type=bridge)
2022-04-19T20:02:13.501657100-04:00 container destroy 5d797b8ffe91d3c6840df81ac168b535bc1c796d8717381fa3f7daa5f5843d91 (image=mentoring-app-backend, name=bakend)
2024. Personal website built with Astro & Streamline.