This tutorial will concentrate on how to build a custom Docker image based on Ubuntu with Apache service installed. The whole process will be automated using a Dockerfile.
Docker images can be automatically built from text files, named Dockerfiles. A Docker file contains step-by-step ordered instructions or commands used to create and configure a Docker image.
Basically, a Docker file contains various instructions in order to build and configure a specific container based on your requirements. The following instructions are the most used, some of them being mandatory:
FROM
= Mandatory as the first instruction in a Docker file. Instructs Docker to pull the base image from which you are building the new image. Use a tag to specify the exact image from which you are building:Ex: FROM ubuntu:20.04
MAINTAINER
= Author of the build imageRUN
= This instruction can be used on multiple lines and runs any commands after a Docker image has been created.CMD
= Run any command when the Docker image is started. Use only one CMD instruction in a Dockerfile.ENTRYPOINT
= Same as CMD but used as the main command for the image.EXPOSE
= Instructs the container to listen on network ports when running. The container ports are not reachable from the host by default.ENV
= Set container environment variables.ADD
= Copy resources (files, directories, or files from URLs).1. First, let’s create some kind of Dockerfile repositories in order to reuse files in the future to create other images. Make an empty directory somewhere in /var
partition where we will create the file with the instructions that will be used to build the newly Docker image.
# mkdir -p /var/docker/ubuntu/apache
# touch /var/docker/ubuntu/apache/Dockerfile
Create a Dockerfile Repository
2. Next, start editing the file with the following instructions:
# vi /var/docker/ubuntu/apache/Dockerfile
Dokerfile excerpt:
FROM ubuntu
MAINTAINER your_name <user@domain.tld>
RUN apt-get -y install apache2
RUN echo “Hello Apache server on Ubuntu Docker” > /var/www/html/index.html
EXPOSE 80
CMD /usr/sbin/apache2ctl -D FOREGROUND
#centos #docker #redhat #virtualization #docker tips #virtualization