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.

Requirements

  • Install Docker and Learn Docker Container Manipulation – Part 1
  • Deploy and Run Applications under Docker Containers – Part 2

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:

  1. 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
  1. MAINTAINER = Author of the build image
  2. RUN = This instruction can be used on multiple lines and runs any commands after a Docker image has been created.
  3. CMD = Run any command when the Docker image is started. Use only one CMD instruction in a Dockerfile.
  4. ENTRYPOINT = Same as CMD but used as the main command for the image.
  5. EXPOSE = Instructs the container to listen on network ports when running. The container ports are not reachable from the host by default.
  6. ENV = Set container environment variables.
  7. ADD = Copy resources (files, directories, or files from URLs).

Step 1: Creating or Writing Dockerfile Repository

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 Dockerfile Repository

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

How to Automatically Build and Configure Custom Docker Images with Dockerfile - Part 3
1.75 GEEK