21 Days of Docker-Day 6- Introduction to Dockerfile — Part 2

Prashant Lakhera
2 min readOct 13, 2019

--

Welcome to Day 6 of 21 Days of Docker. On Day 5, I mentioned some of the Dockerfile directives, let extend that concept further

USER: The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile

WORKDIR : The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

FROM centos
RUN useradd -ms /bin/bash centos_user
USER centos_user
ADD index.html /home/centos_user
WORKDIR /home/centos_user
EXPOSE 80

User Directive is used to create a non-privileged user. Rather than using root, we can use a non-privileged user to configure and run an application.

Let’s put together, all the concept we learned so far, in the Dockerfile

FROM centos:centos7
ENV HTTPD_VERSION 2.4.6-90.el7.centos
RUN yum -y update && yum -y install -y httpd-$HTTPD_VERSION
WORKDIR /var/www/html/
ADD index.html ./
EXPOSE 80
CMD ["/usr/sbin/apachectl","-DFOREGROUND"]
HEALTHCHECK CMD curl localhost:80
  • Before building the new image, create the index.html file, as we are using the ADD directive.
  • Now try to build the new image
$ docker build -t myapache .
  • Start the container, using this image
$ docker run -d --name myapache -p 80:80 myapache 
edc76724bc7c970648e4dc5c293afffea28a92df3dcbee0d0ec693e150667731
  • Verify it
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
edc76724bc7c myapache "/usr/sbin/apachectl…" 6 seconds ago Up 3 seconds (health: starting) 0.0.0.0:80->80/tcp myapache
  • Test it
$ curl localhost:80
This is coming from Docker

If you want to read the complete story

This is a good place, to stop for Day 6

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.

Slack:

https://100daysofdevops.slack.com/join/shared_invite/enQtNzg1MjUzMzQzMzgxLWM4Yjk0ZWJiMjY4ZWE3ODBjZjgyYTllZmUxNzFkNTgxZjQ4NDlmZjkzODAwNDczOTYwOTM2MzlhZDNkM2FkMDA

Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet