How to Dockerize a Next.js Application for beginners.
What is Docker and Why Should You Use It for Your Applications?
Docker is like a magic box for your applications! It helps you package everything your app needs – like the right operating system, files, and dependencies – into a neat, portable package. This way, your app stays happy and runs smoothly no matter where it goes, whether on your computer or somewhere else. It's like giving your app its own little world to live in!
Its like setting up your application on a new pc with your desired OS and stuff but its all automated and you just have to define it once.
Docker packages your application along with OS into smaller and portable virtual machine's like infrastructure called Docker Container.
Prerequisites:
Create a Dockerfile
Create a Dockerfile
in root directory of your project, Exact Dockerfile
with no any file extension
Dockerfile
is like a step by step script that tells Docker how it is going to build contianer image.
Go ahead and add the following code into your Dockerfile
.
#node installation FROM node:19-alpine #working directory WORKDIR /app copy package.json into the working directory COPY package*.json ./ #install all the dependencies of your project. RUN npm install #copy all the files from your current directory to the working directory of the container COPY . . #expose port 3000 from your container to local network EXPOSE 3000 #start the development server from your container CMD npm run dev
How to use .dockerignore and its importance
A .dockerignore
is a configuration file that describes files and directories that you want to exclude when building a Docker image.
When you're building a Docker image for your application, the Dockerfile (which contains instructions on how to build the image) is usually placed in the root directory of your project. However, your project directory might contain various files that aren't necessary for your Docker image or that you don't want to include because they could make the image larger than it needs to be.
That's where the .dockerignore
file comes in. It's a way to specify which files or directories should be excluded when building the Docker image. You list the files or patterns in the .dockerignore
file, and Docker will make sure those are not included.
For example, if you have a Node.js project, you might have a .dockerignore
file that looks like this:
node_modules npm-debug.log
Creating your first Docker container
Let's build a home for our project using Docker! Open your command line, go to your project's main folder, and type 'docker build -t playerofcode .' This tells Docker to create a special container for our project named 'playerofcode'. You can pick any name you fancy. The dot at the end means the instructions for building this container are right here in our current folder.
docker build -t playerofcode .
Each time you will run this command, It will create a new image of your container, You can check what images are in your system by running
docker images
ordocker image ls
Running docker container
To run through command line, Open your terminal and run docker run -p 3000:3000
followed by the name and tag of your image
In my case:
docker run -p 3000:3000 playerofcode