In the she code Africa Cloud School program,
We recently learnt about Containerisation and how important it is in the software development lifecycle, especially in regards to environment related errors such as various versions of runtimes installed on machines.
So i decided why not make an image out of my full stack web application and push the image to the registry then later deploy the app.
For starters i know that i have to define a Dockerfile that defines the environment my app should run in.
Inside the docker file, we add
- the runtime which will be nodejs 16 though we can use a lightweight image which is lts-alpine.
FROM node:lts-alpine
- then create a working directory to store our app in the image.
WORKDIR /usr/src/app
- next we copy our package.json file into that working directory
COPY package*.json ./
- then we install the dependencies using the RUN command
RUN npm install
- we can now copy the application code into the directory
COPY . .
- Then we can now expose the port on our container that will allow us to connect to the web app.
EXPOSE 3000
- Then to build the application
npx tsc
- Finally, we set the command to run the application.
CMD [ "node", "start" ]
Next we create a dockerignore file to prevent copying some resources into our application such as node_modules.
Now its time to build our image usign docker build with an image name as mssql_nodejs_app
docker build . -t node_mssql
Building....
Once the build finishes, to see our image we use
docker images
To run the application locally,
docker run -d -p 3000:3000 node_mssql
the -d represents running it in background and prints container id
the -p represents the port number the container exposes to localhost in this case port 3000 (the second)on container is bound to port 3000(the first) on local host.
After this our app should be visible on localhost 3000
So after building this container image, i am now thinking of pushing it to the container registry in azure and create an app service with it.
Super excited!!!!!!!!!!!!!!!!!
Top comments (0)