Mon - Fri: 9:00 - 19:00 / Closed on Weekends

4- Docker: Creating your Custom Docker Image with dockerfile - Image Tagging - Logs

So far we have used official docker images from DockerHub. This time we will customize a docker image and create our own image by using dockerfile. We are going to download a nice looking template from internet and copy all of its content and styles into our nginx docker image and create a custom image.

https://startbootstrap.com/ has many nice looking free web site templates that we can download.

Let's download SB Admin 2 template from https://startbootstrap.com/theme/sb-admin-2 on our host machine. Create a new directory on desktop and name the folder "sbadmin2". Extract all the contents of the downloaded zip file into sbadmin2 folder. If you double click the index.html in sbadmin2 folder, you will see a nice looking template.

 

Open up sbadmin2 folder in VSCode and create a new file named dockerfile. Then add the following lines and save the dockerfile.

FROM nginx:latest
ADD . /usr/share/nginx/html
First line instructs to use latest nginx image and second line instructs copying everything from the current folder into /usr/share/nginx/html in the new image that we are going to build.

 

To build the docker image from the docker file run (make sure you add a dot at the end):

docker build --tag <yourimagename>:<version> .

 

Then let me run a container from this custom image and browse 127.0.0.1:8080 to see my custom container.

docker run --name myweb -d -p8080:80 mycustomimage:latest

 

I hope everything is clear so far.  We got an official nginx image and copied some html and css files into the relevant folder and build our own image. Then we run a container from that custom image. 

 

Image Tagging:

docker tag command copies an image with an new tag.

docker tag mycustomimage:latest mycustomimage:v1

 

Now I am going to open up VSCode and edit index.html and change the text "Dashboard" to "My Custom Dashboard" and rebuild my custom image.

docker build -t mycustomimage:v2 .

 

Let's run v1 and v2 containers at different ports on my host machine.

docker run --name mywebV1 -d -p8000:80 mycustomimage:v1

docker run --name mywebV2 -d -p8002:80 mycustomimage:v2

 

 

Container Logs:

If you are having any problems with your container you can run docker logs command with container id or container name. docker logs <container-name>

 

 

We can also use docker exec -it command to access container and check environmental information by using env command.

docker exec -it <container-id> /bin/bash  OR docker exec -it <container-id> /bin/sh

 

docker inspect command displays every  details about your container (ip address, port binding, used image etc)

 

 

  • Hits: 204