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

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 RUNCMD and ENTRYPOINT instructions that follow it in the Dockerfile

WORKDIR : The WORKDIR instruction sets the working directory for any RUNCMDENTRYPOINTCOPY 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