This tutorial will concentrate on how to build a custom Docker image based on Ubuntu with Apache service installed. The whole the process will be automated using a Dockerfile.
Docker images can be automatically build form text files, named Dockerfiles. A Docker file contains step-by-step ordered instructions or commands used to create and configure a Docker image.
Docker images can be automatically build form 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 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:
MAINTAINER
= Author of the build imageRUN
= This instruction can be used on multiple lines and runs any commands after Docker image has been created.CMD
= Run any command when 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).
Step 1: Creating or Writing Dockerfile Repository
1. First, let’s create some kind of Dockerfile repositories in order to reuse files in 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.
2. Next, start editing the file with the following instructions:
Dokerfile excerpt:
Now, let’s go through the file instructions:
The first line tells us that we are building from an Ubuntu image. If no tag is submitted, say 14:10 for example, the latest image from Docker Hub is used.
On the second line we’ve added the name and email of the image creator. Next two RUN lines will be executed in the container when building the image and will install Apache daemon and echo some text into default apache web page.
The EXPOSE line will instruct Docker container to listen on port 80, but the port will be not available to outside. The last line instructs the container to run Apache service in foreground after the container is started.
3. The last thing we need to do is to start creating the image by issuing the below command, which will locally create a new Docker image named
ubuntu-apache
based on the Dockerfile created earlier, as shown in this example:
4. After the image has been created by Docker, you can list all available images and identify your image by issuing the following command:
Step 2: Run the Container and Access Apache from LAN
5. In order to run the container continuously (in background) and access the container exposed services (ports) from the host or other remote machine in your LAN, run the below command on your host terminal prompt:
Here, the
-d
option runs the ubuntu-apache
container in background (as a daemon) and the -p
option maps the container port 80 to your localhost port 81. Outside LAN access to Apache service can be reached through port 81 only.
Netstat command will give you an idea about what ports the host is listening to.
After the container has been started, you can also run
docker ps
command to view the status of the running container.
6. The webpage can be displayed on your host from command line using curl utility against your machine IP Address, localhost or docker net interface on port 81. Use ip command line to show network interface IP addresses.
7. To visit the container webpage from your network, open a browser at remote location and use HTTP protocol, the IP Address of the machine where the container is running, followed by port 81 as illustrated on below image.
8. To get an inside of what processes are running inside the container issue the following command:
9. To stop the container issue
docker stop
command followed by the container ID or name.
10. In case you want to assign a descriptive name for the container use the
--name
option as shown in the below example:
Now you can reference the container for manipulation (start, stop, top, stats etc) only by using the assigned name.
Step 3: Create a System-wide Configuration File for Docker Container
11. On CentOS/RHEL 7 you can create a systemd configuration file and manage the container as you normally do for any other local service.
For instance, create a new systemd file named, let’s say,
apache-docker.service
using the following command:
apache-docker.service file excerpt:
12. After you finish editing the file, close it, reload the systemd daemon to reflect changes and start the container by issuing the following commands:
This was just a simple example on what you can do with a simple Dockerfile but you can pre-build some pretty sophisticated applications that you can fire-up in just a matter of seconds with minimal resources and effort.
No comments:
Post a Comment