Docker series (Part 10) : Create your own Dockerfile and create a container from it
Published: 2022-06-10 15:38:19

In this blog, we are going to go through this process

We will create a file named Dockerfile and open it in terminal . We are using vim to edit it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgd19fs4qqzrznw9ie6i.png)
 


1.First we will import node 18-alpine 
You can see the latest version of node from here : https://hub.docker.com/_/node


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ao3axwoutk1w0kr4d4e.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6sgqkfoscq00y9df9ju5.png)

 So, lets import the node image and tag: 18-alpine . Alpine is a very light distribution.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjiwgt86v6b8zy3pe8nz.png)

2. We will expose port 3000 for this app. Assume it as an instruction


 
3. Now, we will use alpine package manage to install 

```
RUN apk add --uppdate tini
```
We will also create directory to copy usr/src/app using 

```
RUN mkdir -p usr/src/app
```
and then go into it using

```
WORKDIR usr/src/app
```



 
4.Node uses a package manager and thus we will create a package.json file and copy it to the package.json file.

Then we will install dependencies using

```
RUN npm install
```

Also we will keep it small and clean thus

```
RUN npm cache clean
```

or, in short use

```
RUN npm install && npm cache clean
```


 

5. We will then copy everything from the current node:18-alpine using

```
COPY ..
```

 

6. Now we need to start the container using 'tini -- node ./bin/www'

We will use the json format to add this

```
CMD ["tini","--","node","./bin/www"]
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ou3xpezt9r5y4ozpwe15.png)
 
Now, save this file and go to terminal. We will build the dockerfile using this command

```
docker build -t <random image name you wish> .
```
"." is given to build it in the current directory



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6mr0q86arx0txzkwm0c.png)

We have got a bit problem
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fazkud04kowz91ast3py.png)

What happened?

Lets see the images


 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfdof2yphnc69sh4zaxl.png)

You may see an image <none> with tag <none>
As the problem occured, we faced this issue. We will solve this by making a simple change in our docker file


```
RUN npm install && npm cache clean --force
```
We will force this line to be implemented and to clear our doubts, we will now create a new image with a new name called "testimage"


 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ufflrqhkpzq4egwp6mf.png)

This time it worked perfectly and added the latest tag.

Now, look at the images



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvuqivc4hyvkwvqlb7mp.png)

we can see our image on top with latest tag.

So, image is created! Congratulations!

Now, lets create a container using this image

```
docker container run -rm -p 80:3000 --name test_container testimage
```

-rm is used when we want to stop the container till the moment we exit the container.

-p 80:3000 is used because it will run on 80 but listens from 3000 . Also, we exposed 3000 for that in our Dockerfile


We also have set a container name to our our container and used the image "testimage"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yfe4de7gjiwu8ak5st6.png)

Now go to your web browser and type "localhost", you can see this


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ntvqch3hzvagz780to7.png)

Go to your terminal and you will see this logs



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tn34thqh7kjec0rk72fd.png)
Now press control+ C to stop it .


Now lets put this image uploaded to our Dockerhub id with a new name


```
docker tag testimage <dockerhub username/new image name>
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0ge9f6nohaiz4yyys4d.png)
 
Now, lets push it
using

```
docker push <docker hub username/image name>
```




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utcithgkr5hilz0k6a4a.png)
 
Go to your dockerhub and see the image there

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nauqczutvuu8wvtlblp.png)



Congratulations! You have completed it
<img src="https://media0.giphy.com/media/Nfs5y1kzJKfu/giphy.gif?cid=ecf05e472d3w835ptrri5m1p6ifnwl9mi7ope9pifvhpcxf6&rid=giphy.gif&ct=g">


If you get stuck, you can see our final Dockerfile

```
From node:18-alpine


EXPOSE 3000

RUN apk add --update tini

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json package.json

RUN npm install && npm cache clean --force
COPY . .

CMD ["tini","--","node","./bin/www"]



```
See you again

<img src="https://c.tenor.com/Tvd3VKwtse0AAAAC/cat-cute.gif">