DEV Community

Cover image for Building Container Images Using Dockerfiles
Sami Ullah Saleem for AWS Community Builders

Posted on

Building Container Images Using Dockerfiles

1- Create Dockerfile

~ vi Dockerfile
Enter fullscreen mode Exit fullscreen mode
FROM httpd:2.4
RUN apt update -y && apt autoremove
Enter fullscreen mode Exit fullscreen mode
  • Dockerfile always start from FROM keyword and after it we tell the image name like we are telling here httpd of which we want to create our container.
  • RUN whatever we are going to write after RUN it will be run in our container.
~ docker build -t wigetfactory .
Enter fullscreen mode Exit fullscreen mode
  • Here you can see that we are creating our image from our Dockerfile and giving it a name which is wigetfactory

2- Let's update our Dockerfile and put our website content in it.

FROM httpd:2.4
RUN apt update -y && apt autoremove
rm -f /usr/local/apache2/htdocs/
WORKDIR /usr/local/apache2/htdocs/
COPY ./web .
Enter fullscreen mode Exit fullscreen mode
  • Here you can see that I am changing my container work directory using *WORKDIR * command and then copy my website content from my host directory to the container location which is htdocs.

  • Let's run our Dockerfile again.

~ docker build -t widgetfactory:0.3 .
Enter fullscreen mode Exit fullscreen mode

Here will be the output of it

Dockerfile output

  • Now run container using the image.
docker  run --name weebproject -d -p 80:80 widgetfactory:0.3
Enter fullscreen mode Exit fullscreen mode

Follow me for more content

Top comments (3)

Collapse
 
im6h profile image
Vu

How about if I do not use Dockerfile?

Collapse
 
samiullahsaleem profile image
Sami Ullah Saleem

Whatever you have written in the docker file. You have to do from your host terminal. But commands will be different.

Collapse
 
samiullahsaleem profile image
Sami Ullah Saleem

You can read my this article
dev.to/aws-builders/hosting-site-o...