- Published on
#6-Building Container Images Using Dockerfiles
- Authors
- Name
- Surya Harahap
- @suryaharahap18
Creating a container image by hand is possible, but it requires manual processes. There has to be a more automatic way to build images. Manual processes do not scale and are not easily version controlled. Docker provides a solution to this problem - the Dockerfile. In this lab, you will create a Dockerfile to build an image, and host a static website.
Build a First Version
Change to the widget-factory-inc directory:
cd widget-factory-inc
Create a Dockerfile that uses httpd:2.4 as the base image:
vim Dockerfile
In the new file, insert the following:
FROM httpd:2.4 RUN apt update -y && apt upgrade -y && apt autoremove -y && apt clean && rm -rf /var/lib/apt/lists*
Save the file:
ESC :wq
Verify that the file was saved successfully:
cat Dockerfile
Build the
0.1
version of thewidgetfactory
image using the Dockerfile:docker build -t widgetfactory:0.1 .
Set variables to examine the image's size and layers:
export showLayers='{{ range .RootFS.Layers }}{{ println . }}{{end}}' export showSize='{{ .Size }}'
Compare the httpd and widgetfactory images:
docker images
Show the widgetfactory image's size:
docker inspect -f "$showSize" widgetfactory:0.1
Show the layers:
docker inspect -f "$showLayers" widgetfactory:0.1
Show the layers of the httpd:2.4 image:
docker inspect -f "$showLayers" httpd:2.4
Compare the layers. Are they the same?
Load the Website into the Container
- Open the Dockerfile:
vim Dockerfile
- Remove the Apache welcome page from the image by adding the following: RUN rm -f /usr/local/apache2/htdocs/index.html
- Save the file:
ESC :wq
- Build version 0.2 of the widgetfactory image:
docker build -t widgetfactory:0.2 .
- Inspect both versions of the widgetfactory image to see the differences in size and layers:
docker images
- Show the widgetfactory:0.1 image's size:
docker inspect -f "$showSize" widgetfactory:0.1
- Compare it to the image size for widgetfactory:0.2: docker inspect -f "showLayers" widgetfactory:0.1 Show the layers for the widgetfactory:0.2 image and compare the two: docker inspect -f "showSize" widgetfactory:0.2 Compare it to the image size for widgetfactory:0.3: docker inspect -f "showLayers" widgetfactory:0.2 Show the layers for the widgetfactory:0.3 image and compare the two: docker inspect -f "$showLayers" widgetfactory:0.3 Using an interactive terminal, check the htdocs folder for widgetfactory:0.3: docker run --rm -it widgetfactory:0.3 bash Are the website files in the folder?: ls -l
Run a Container from the Image
Run a container from the widgetfactory:0.3 image. What commmand does it use to run the server? Remember to publish the web server port: docker run --name web1 -p 80:80 widgetfactory:0.3 Exit the container: CTRL+C Check the status of the container: docker ps -a Start the container: docker start web1 Using docker exec connect to the web1 container: docker exec -it web1 bash View the website files in the container: ls -l Exit the container: exit Retrieve the main website page from the container: wget localhost Compare it to the copy on the server: diff index.html web/index.html Read Me | Note: Many of the things done in this lab are intentionally inefficient, These tasks are done to demonstrate certain aspects of Docker, and highlight common pitfalls. These do not represent best practices.
END