Docker
Dockerize your Project
Create a Dockerfile
- Suppose you have a NodeJs project and you want to dockerize it.
- Create a Dockerfile and .Dockerignore in the root of your project.
- Add the following code to the Dockerfile.
Code
Build Image
- it by default search
Dockerfile
in the current directory,if you used above command.
Custom Dockerfile Name
- Suppose i create a dockerfile with name
Dockerfile.dev
in the root of my project. - Then i can build the image with the following command.
-f
is used to specify the dockerfile name.-t
is used to specify the image name.
Run Container
Note:
FROM
is used to specify the base image.WORKDIR
Means location of your project in container.COPY
is used to copy the whole project in container.*
is used to copy all the files in the current directory.RUN
is used to run the command.EXPOSE
is used to expose the port.CMD
It is the default command that will be executed when the container is run.node:18-alpine
is the base image. andalpine
is the lightweight version of the image.
Important Points
- Docker build any image
Layerwise
means it will build the image in layers. - Count the number of lines in the dockerfile = Number of layers.
- In the above code we have 7 layers.
- So when you build the image it will build the image layer by layer.
- It is most important to follow the correct order of the code in the dockerfile.
Setup Env Value
- You can setup the env value in the dockerfile by using the following command.
Note:
ENV
is used to setup the env value in the dockerfile.PORT=3000
is used to setup the port value in the dockerfile.EXPOSE 3000
is used to expose the port of the app.CMD ["npm", "start"]
is used to start the app.
While running image
- We can also set multiple env value.
Using an environment file
Port Mapping
Problem
- Like, you run any Node.js app image that is exposing or running the project on port
3000
, but when accessing this3000
port from your local machine, you will not be able to access that port. This is happening because your Node.js image is running in a virtual container — suppose the container is a virtual laptop — and there is no connection between your local machine and the virtual laptop. That is why you are not able to access that port.
Solution
- You can solve this problem by doing
port mapping
. You can map your local machine port to the virtual laptop (container). Then you will be able to access that Node.js project properly and use it.
Custom Port Mapping
Note:
-p
is used to map the port of the container to the port of the local machine.3000:3000
is used to map the port of the container to the port of the local machine.- You can use whatever port you want to use in the local machine. but make sure it is mapped to the port of the container.
- You can run the same image multiple times with different ports.
Random Port Mapping
- It will map assign the random port to your local machine and map it to the port of the container.
Push Image to Docker Hub
- 1st Make sure you have a docker hub account and login to it.
- Then you can push your image to docker hub.
- You can push your image to docker hub by using the following command.
Here
gautamxd/nodejsapp
is the repository name where you want to push your image.my-node-app
is the image name.