Very very basic docker implementation
This guide assumes you have a directory that you want to containerize.
Creating docker file
A Dockerfile
contains all the instructions Docker needs to build a container. It's a user-defined text document that includes commands (you can see the list here) that need to be executed on the command line to assemble an image.
You can manually create this file or run the following command in the terminal:
touch Dockerfile
Writing the Dockerfile
Now that you have the Dockerfile ready, let's decide what to write in it. To run my project locally, I need:
- Node.js
- The actual project directory
First, we'll add Node.js. Decide which Node.js version you need for your project. In my case, I'm using 20.13.1 lts-alpine. You can choose whichever version suits your needs. Add this to the file:
FROM node:lts-alpine
Next, create a working directory for your app. Add this to the file:
FROM node:lts-alpine
# what should be the work directory
WORKDIR /usr/src/smart-brain-api
Now, copy your project files into the container and install the dependencies:
FROM node:lts-alpine
# what should be the work directory
WORKDIR /usr/src/smart-brain-api
# what we want from current dir into container
COPY ./ ./
#what should we run into container
RUN npm install
For the Alpine version of Node.js, we need to install bash since it is not included by default. Add the following command:
FROM node:lts-alpine
# what should be the work directory
WORKDIR /usr/src/smart-brain-api
# what we want from current dir into container
COPY ./ ./
#what should we run into container
RUN npm install
# because apline image don't have bash installed, we have to do it manually
RUN apk update && apk add bash
Finally, set the command to run when the container starts. In this case, we want to open a bash shell. The final Dockerfile looks like this:
FROM node:lts-alpine
# what should be the work directory
WORKDIR /usr/src/smart-brain-api
# what we want from current dir into container
COPY ./ ./
#what should we run into container
RUN npm install
# because apline image don't have bash installed, we have to do it manually
RUN apk update && apk add bash
CMD ["/bin/bash"]
To check if you have everything working or not let's build the docker container using build command
docker build -t <name_of_container> .
Now that the docker image is built we can run that image and check it out:
docker run -it <name_of_container>
The flag -it takes you inside the container.
Assuming all goes well you can run the command of your choice to check if your project is running or not in the container.
# in my case i am starting a node server
npm run start
Summary
By following these steps, you've created a basic Docker container for your project. This setup uses the lightweight Alpine version of Node.js, installs necessary dependencies, and ensures that a bash shell is available for interaction. Happy containerizing!
© Aditya Nagar.RSS