When you start a container in Docker from the Base image, Docker fetches the image and its parent image, and repeats the process until it reaches the Base image. Then the Union File System adds a read-write layer on top. This read-write layer, information of its Parent Image, networking configuration, resource limits and unique id is called a Container.
Containers has two states running state and exited state. In running state container includes a tree of processes running on the CPU, isolated from the other processes running on the host. Exited is the state of the file system and its exit value is preserved. You can start, stop, and restart a container with it.
Docker allows you to build the image in one of the following two ways:
- Interactively launch BASH shell under Ubuntu Base image, install Apache and its dependencies, and then save the image.
- Build the image using Dockerfile.
In this tutorial we will build an Ubuntu instance and host a Website running under Apache Web Server using an interactive shell.
Requirements
Ubuntu Server 14.04 with Docker installed on your system.
Running a Docker Instance
First of all, you will need to pull the Ubuntu image before starting anything.
You can easily pull the Ubuntu image from the Docker public registry hub.
To download the Ubuntu image, Run:
sudo docker pull ubuntu
Once the Ubuntu image has been download, You can create an Ubuntu instance in a Docker container and attach a bash shell by running the following command:
sudo docker run -i -t ubuntu bash
Installing Apache
Now, after the Ubuntu base image with instance is ready, you can easily install the Apache Server interactively for it.
To do so, you will need to run following command in a terminal.
apt-get update -y
apt-get install apache2 apache2-utils -y
After installation the installation is finished, you can exit from the current shell by running the following command:
exit
Saving the Image
Now, you can save the changes you made into the Ubuntu instance. To do that, first you will need the Container ID of running Ubuntu instance.
To get that, Run:
sudo docker ps -a
Now, save the changes as a new image with name ubuntu-apache by running the following command:
sudo docker commit df888c72ed5a ubuntu-apache
The output looks something like this:
sha256:00c9fc0494a2953ab8b01ff9ae6471eaaccfa8860d669c1f0da13cca3a5cd886
You can see that the changes are saved using Container ID and image name ubuntu-apache.
To verify the new image is running or not, Run:
sudo docker images
You can see the ubuntu-apache image listed below:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-apache latest 00c9fc0494a2 43 seconds ago 202.6 MB
ubuntu latest 3876b81b5a81 3 weeks ago 187.9 MB
chef/ubuntu-14.04 latest f1f7972ced25 17 months ago 386.6 MB
Adding the Website Contents to the ubuntu-apache image
Now, you have a new image that contains a Apache Web server. You can build a Dockerfile based on that image and add the necessary files. Given the relative path to a tarball of the site content, Docker automatically untars or unzips the files in a source tar or zip file into the target directory.
To do this, you need to create an index.html file on the host system and add it to a tarball called website.tar in current directory:
mkdir website
nano website/index.html
add the following content:
Sample Apache Web Page
Save and close the file.
Now, compress the website directory using tar:
sudo tar -cvf website.tar website
Now, create a Dockerfile to add the web site content to the ubuntu-apache image and launch Apache on port 80:
nano Dockerfile
Add the following content –
FROM ubuntu-apache
ADD website.tar /tmp/
RUN mv /tmp/website/* /var/www/html/
EXPOSE 80
ENTRYPOINT [ "/usr/sbin/apache2ctl" ]
CMD [ "-D", "FOREGROUND" ]
Save and close the file.
In the above Dockerfile, the website content in website.tar will be automatically extracted to /tmp/ folder. Then, the entire site will move to the Apache root directory /var/www/html/ and the expose 80 will open port 80 so that the website will be available as normal. Then, the entrypoint is set to /usr/sbin/apache2 so that the Apache Server will execute.
Building and running a Container
Now, you will build a Container using the Dockerfile you just created in order to add website on it.
To do this, run the following command:
sudo docker build -t website .
You should see the following output:
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM ubuntu-apache
---> 00c9fc0494a2
Step 2 : ADD website.tar /tmp/
---> Using cache
---> be3aeba8dc4e
Step 3 : RUN mv /tmp/website/* /var/www/html/
---> Using cache
---> 761bc3d9ba23
Step 4 : EXPOSE 80
---> Using cache
---> 51d11f0a2b38
Step 5 : ENTRYPOINT /usr/sbin/apache2ctl
---> Using cache
---> dfb777849e98
Step 6 : CMD -D FOREGROUND
---> Using cache
---> 68215963e648
Successfully built 68215963e648
After the image has been built, you can now proceed by creating a container running the Apache instance on it.
sudo docker run -d -P website
Now, use the “docker ps” command to determine the port activated and then use curl to inspect the sample content.
sudo docker ps
You should see the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45dfb7cf3ef1 website "/usr/sbin/apache2ctl" 12 seconds ago Up 10 seconds 0.0.0.0:32775->80/tcp nauseous_euclid
Now, verify your Apache Web server by running the following command:
curl localhost:32775
or
curl "Container IP Address":80
You should see the Apache Web page you have created earlier:
“` language-bash
Sample Apache Web Page