How to make our own Docker container | Docker Tutorial
How to make our own Docker container | Docker Tutorial
We will be making a docker container containing a basic website.
If you want to learn the basics of docker you can follow Docker TutorialList of Contents
- How to make a docker container
- Making a docker file
- Building our docker image
- Making a container out of the image
How to make a Docker Container
In our previous tutorials, we have used the prebuild docker images, we have used someone's docker image to make the docker container.
Step 1:- We have to make a file in which we specify the commands that are needed in making the docker image.
Step 2:- We build that file to obtain our docker image.
Step 3:- Now we can make a container out of that docker image.
Making a Dockerfile
You first have to make a file named Dockerfile notice the case you exactly have to use this case.
After we have made the file successfully now we have to write some commands inside this file to make the Docker image from it.
Step 1:- Create a Dockerfile
Step 2:-
As you can see first we have first used another image for building our own image ( Most of the Docker images are based on another image )
- From nginx:1.21.6-alpine ( You can use any other image but for this purpose, I used this image but you might have to do some other configurations also )
- Copy HTML/ /usr/share/nginx/html
The first command is getting the nginx image based on alpine as it is pretty short in size.
The second command is copying the source file to the destination ( the destination defined is the default directory where the Nginx stores its file )
Step 3:-
I have made a folder of HTML where the website is stored.
You can see the folder structure above as how I have arranged the stuff.
Now as we have done this stuff we are now ready to build our docker image out of the Dockerfile
Building our Docker Image
To build the docker image out of the Dockerfile we just created we have to move into the terminal or CMD OR Powershell if you are in Windows.
Note:- Before running the below command you have to move into the directory/folder where you have stored the Dockerfile
- sudo docker build .
This command is saying the docker client to build the image using the current path ( That's what the . represents at the end of the command ), it will look up the Dockerfile and build our image according to the instruction given in it.
Now we have our image created, but it doesn't have a name & tag specified.
- sudo docker build -t "REPOSITORY_NAME" .
We have to specify the REPOSITORY as we are building the image
Now as you can see our image has both the REPOSITORY_NAME & the TAG rather than <none> as we saw previously.
Making a Container out of the Image
Now as we have successfully created the docker image, we can run it & see the website that we have created.
- sudo docker run -d -p 80:80 "Image ID"
As you can see above we have used -p 80:80 in our command which is opening the ports inside of the docker container & our host machine. By default it used TCP.
Now you are able to open the website in your browser.
You can also go inside the container & change the files there directly.
As nginx files are stored in the /usr/share/nginx/html directory you can see the Website files there you can also modify them.
Conclusion:-
We have successfully created our own Dockerfile, Image, and Container & we are able to see the Website in our browser as we browse to localhost:80