Docker container: Build images -- Dockerfile 1

docker

This tutorial will guide the reader to create a Docker image with apache web server running on a CentOS operating system.

If you are new to Dockers, please click here to access the installation and basics document.

Create a new folder named /sandbox/docker
[root@docker1]# mkdir /sandbox/docker

Go to the created folder /sandbox/docker
[root@docker1]# cd /sandbox/docker

Create a file named “.Dockerfile” with the following lines in italics:

==== ===== ===

# FROM is the base image to be used
FROM centos

#RUN to run a command before creating the final image
RUN yum -y install httpd
RUN echo “Welcome to my webpage” > /var/www/html/index.html

# COPY to copy a file present in the localhost to the filesystem inside the new image
COPY ./shiju.txt /home

#Command to be run after deploying the comainter with the image
ENTRYPOINT [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]

==== ===== ===

  • The FROM parameter tells what should be the base image to be used
  • The RUN parameter says what commands needs to be run on the base image before we build the new image
  • The CMD parameter says what command needs to be run when the container will be deployed using the new image
  • The WORKDIR is a parameter that can be used to mention the working directory based on which the remaining command/statements needs to be run

Now let us build the initial build named “centos_1” using the “.Dockerfile” file:
[root@docker1 docker]# docker build -t centos_http -f .Dockerfile .

The -t is the parameter to tag the image

Now let us run the image, binding port 1337 to view the webpage

[root@docker1 docker]# docker run -it -p 1337:80 --name myContainer centos_http

You should be able to access the default apache web page by accessing “http://<base host IP>:1337” using a web browser.

The switch -p enables all connections coming to TCP port 1337 of the system hosting the docker to be mapped to port 80 of the new container being deployed.

Tagging the image:

Another way of tagging the same build image was by running the command:

[root@docker1 docker]# docker build -t shiju_docker_id/redis1:latest -f .Dockerfile3 .

Here latest is the version number, which is normally written as latest.

[root@docker1 docker]# docker ps -all

The way to deploy a container using the image is as follows.

 [root@docker1 docker]# docker run shiju_docker_id/redis1

Theoretically the version number is the actual tag.