Docker Run Image as Container - Create Container From Docker Image

In this post, we are going to see how to create an image and run the image as a container and manage it.

Before we proceed further, it is indispensable, that we are aware of what is an Image and What is a Container. So let us begin from there.

The Index

These are the list of steps we are going to perform as part of this post.

  1. Image vs Container - What are they
  2. How to Create a Docker Image - A Quick command overview of Dockerfile
  3. Defining a Plan - Create a Ubuntu Apache2 HTTPD container
  4. Create a Virtual Host file for our website
  5. Create a Sample HTML file for our website
  6. Create a Dockerfile for our Image
  7. The Final Directory Structure of Our WorkSpace
  8. Build Image from the Dockerfile
  9. List the images and make sure the image is created
  10. Docker run Image - Create and Start a Container from our Image
Docker Run Image
Image Credits to SwapnIl Dwivedi - UnSplash

Image vs Container

The Image is a file more like a manifest,  where you define what are the packages and software and configurations should be available when you create a container from the image. It is more like .a template using which you can create N number of containers

The Running instances of the image are called as a container.

To know more about what is container refer this link 

How to Create a Docker Image?

Creating a Docker image is an easy job.  To create a Docker Image. you need to create a file named Dockerfile and specify some information in it.

What information should the Docker file have?

These are a list of information that we must put in the Dockerfile to create an image.

MAINTAINER - Who Crate and manage this container image

FROM - What is the base image, we are going to use to host our container. you can either use a minimal OS image like CentOS, Alpine or you can create your own from the scratch by mentioning SCRATCH as a value to this.

RUN - Commands to Run to make the image( the future container) in the way you want

EXPOSE Do you want your image or application in the image to expose any port to the external world or at least to the host.  For example if you are building Apache HTTP server image you can EXPOSE port 80

CMD The Default Command of the container which gets created using this image.  Every Container must have a Default Command. the Container would run as long as the Default Command is running.

ADD or COPY The files you want to copy into the container from your host.

There are more instructions and elements you can use in the Dockerfile but these are basics and enough for us to create the image

The Requirement/ The Plan

Let us make some plan on what image we are going to create.  Since Apache is most widely used web server, Let us go with that.

We will see how to create a Dockerfile image to create an Apache httpd Docker Container and Docker image.

In your host server where the Docker CLI and Docker is installed,

Create a new directory (or) choose an existing directory and perform the following steps. In my case the directory name is /apps/docker/MyTestApache


Create a Virtual Host file

Create a file named default-website.conf and paste the following content. This is a minimal version of a virtual host file for apache. you can update the values like ServerName and alias as per your requirement.

 <VirtualHost *:80>

	ServerAdmin [email protected]
	DocumentRoot /var/www/html/devopsjunction
	<Directory "/var/www/html/devopsjunction">
		Order allow,deny
		AllowOverride All
		Allow from all
		Require all granted

	ErrorLog ${APACHE_LOG_DIR}/devopsjunction-error.log
	CustomLog ${APACHE_LOG_DIR}/devopsjunction-access.log combined



Create a sample index.html file for our website

Now for our website, we need a welcome page, So you write one or use the following file and save in the name index.html on the same directory

<head> <title> DevOpsJunction - Home Page </title> </head>
<h2> Welcome to Devops Junciton. Good to have you here </h2>


Create the Dockerfile

Create a Dockerfile with the following content on the same directory

# Define the base image on top of which we are going to customize
FROM ubuntu:latest

# Define the Createor and Maintainer of this image
MAINTAINER Sarav AK <[email protected]>

# Run the command to install apache as a prerequisite
RUN apt-get update && apt-get install -y apache2

# Run the command to Create a DocumentRoot
RUN mkdir -p /var/www/html/devopsjunction

# Copy the file from the Current Host Directory to the Container
ADD default-website.conf /etc/apache2/sites-available/
ADD index.html /var/www/html/devopsjunction

# Run one more command to change the ownership of the file we copied
RUN chown root:root /etc/apache2/sites-available/default-website.conf

# Enable this website using a2ensite - command
RUN a2ensite default-website

# Say that our application is exposing a Port

# the Default command and the Command to start the app in our container
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

The final Directory Structure of our working directory

If you are following along, you must have done everything perfect. Let us cross-validate how the work directory of mine ( /apps/docker/MyTestApache) and yours look. They have to be similar

aksarav@middlewareinventory:/apps/docker/MyTestApache $ ls -rlt
total 24
-rw-r--r –  1 aksarav  wheel  447 Apr  9 04:56 default-website.conf
-rw-r--r –  1 aksarav  wheel  150 Apr  9 05:14 index.html
-rw-r--r –  1 aksarav  wheel  861 Apr  9 05:14 Dockerfile

I have three files. From the Top, First One is a Virtual host conf file,  Second on is the sample html page for our website, The Third is our Dockerfile.


Docker build image from Dockerfile

This is how to build an image from Dockerfile. The Dot at the last represents the current directory where our Dockerfile and Conf file is present.

[email protected]:/apps/docker/MyTestApache $ docker build -t myapacheimage .
Sending build context to Docker daemon  4.608kB
Step 1/9 : FROM ubuntu:latest
 ---> 94e814e2efa8
Step 2/9 : MAINTAINER Sarav AK <[email protected]>
 ---> Using cache
 ---> 2f0926915836
Step 3/9 : RUN apt-get update && apt-get install -y apache2
 ---> Using cache
 ---> 22500023dbf5
Step 4/9 : RUN mkdir -p /var/www/html/devopsjunction
 ---> Running in 44461ebbce5d
Removing intermediate container 44461ebbce5d
 ---> 40f424249d6e
Step 5/9 : ADD default-website.conf /etc/apache2/sites-available/
 ---> 41d51868ae15
Step 6/9 : ADD index.html /var/www/html/devopsjunction
 ---> 8cf1ad5a3c6e
Step 7/9 : RUN chown root:root /etc/apache2/sites-available/000-default.conf
 ---> Running in 7e81639c8b8d
Removing intermediate container 7e81639c8b8d
 ---> 03f3c6e6776b
Step 8/9 : EXPOSE 80
 ---> Running in cdf2ee0b0b4c
Removing intermediate container cdf2ee0b0b4c
 ---> 7607f782e0f9
Step 9/9 : CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
 ---> Running in 63cc0be0cff8
Removing intermediate container 63cc0be0cff8
 ---> ba185e532252
Successfully built ba185e532252
Successfully tagged myapacheimage:latest

Now let us see how to verify if the image is created

how to list the available docker images?

use docker images command to list the available docker images in your host

If you want to specifically look out for an image by its name. use docker image ls <imagename> command as follows

aksarav@middlewareinventory:/apps/docker/MyTestApache $ docker image ls myapacheimage
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myapacheimage       latest              ba185e532252        22 minutes ago      210MB


Docker run image - Run the Image and create a container from it

Now we are going to create a container from the image we have created. the command we have to use to create and start the container from the image is as follows.

aksarav@middlewareinventory:/apps/docker/MyTestApache $ docker run  -d -p 8080:80 myapacheimage 


-d : is to run the container in the background

-p : is to publish the Port 80 of the container to the 8080 port of the host so that we can access the website in our host browser( in my case MAC) so the URL

Congratz. You have Successfully created a Container from the Image and as a by-product you have a working Apache2 Ubuntu Container.

Since we have used the ServerName the Apache would listen only on So I have made an entry in my /etc/hosts file like as follows

After doing that I can actually reach my containerized Apache as shown in the following Snapshot.

To know how to run NODEJS into Docker. Refer the following post

Docker NodeJS Example – Run NodeJS on Docker | Devops Junction

Video Guide / VLOG / Screen Casting



Docker CLI Commands -

Further Read On Docker -


Hope this helps.

Rate this article [ratings]

Sarav AK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at

Signup for Exclusive "Subscriber-only" Content