My First Docker Container


The objective of this blog is to learn basic concept of Container, Docker and achieve the following:

  • Build a standalone platform to run Docker container
  • Create a Container Image
  • Run a Containerised Application

What is Container?

A container is a standalone unit of software that contains an application and required dependencies to run the application.

What is Container Image?

A container image is a static file that contains the code to run an application as an isolated process on any supported platform. A container image becomes a container at runtime.

What is Docker?

Docker is a Platform-as-a-Service, that enables operating system level virtualisation to run containers on top of Linux or Windows operating system. It is written in the Go programming language.

What is Docker Engine?

Docker Engine is a container runtime that runs on both Linux and Windows Operating Systems. At a high level Docker Engine consists of the following components.

  • Docker Daemon: This is the server side service that runs on the host operating system.
  • Docker API: This is a RESTful API, which interacts with the Docker daemon or service.
  • Docker CLI: A set of commands to interact and manage containers and container images.

What is Docker Registry?

Server side application that stores and distribute Docker images locally. It is basically an instance of the registry image that runs within Docker.

What is Docker Hub?

Docker Hub is Docker’s official cloud-based Docker image repository. Docker is configured to look for images on Docker Hub by default.

What are Docker Plugins?

Docker Plugins add capabilities to Docker. A plugin is a process running with the Docker Daemon. Below are some common examples

  • Volumes: A mechanism for persisting data generated by and used by Docker containers.
  • Networking: This enables the containers to communicate with each other and the outside world via host machine.

Docker Architecture

Now that we have a fair bit of idea on Containers and Docker, lets start with building a Docker platform.

I am using a Ubuntu VM on Azure as the host operating system. So, let get started.

Install Docker

Login to the Ubuntu VM and run the following commands as root.

# apt-get update
# apt-get install
# systemctl start docker
# systemctl enable docker

Verify Installed Docker Version

Once Docker installation is complete, run the following command to verify the installed Docker version.

# docker --version

This will return the following, which confirms successful installation of mentioned Docker version.

Docker version 19.03.6, build 369ce74a3c

Create Dockerfile

There are lots of container images available in the Docker Hub that are pre-installed with various applications. Here, I will write a dockerfile from scratch by pulling a base CentOS image from Docker Hub.

My objective is to prepare a dockerfile to run my sample JAVA application on Wildfly.

Let’s create a directory to host the dockerfile and create an empty file called dockerfile

# mkdir docker
# cd docker
# vim dockerfile

Now, change the file to ‘insert’ mode and paste the following content and save the file. Follow the comments in the dockerfile to understand how it is written.

#Pulling base image from Docker Hub
FROM centos:7
#Giving the image a label
LABEL "Image Description"="My First Docker Image"
LABEL "Author"="Rohan Islam"
#Installing required dependencies
USER root
run yum -y update; \
yum -y install ca-certificates; \
yum -y install docker; \
yum -y install unzip; \
yum -y install curl; \
yum clean all
#Declaring environment variables
USER root
ENV JAVA_HOME /usr/lib/jvm/java-1.8.0-openjdk
ENV WILDFLY_HOME /opt/wildfly
#Installing OpenJDK and Wildfly
run yum -y install java-1.8.0-openjdk-devel; \
curl -LfsSo /tmp/wildfly-$ $WILDFLY_BINARY_URL; \
unzip /tmp/wildfly-$ -d /opt; \
rm -f /tmp/wildfly-$; \
adduser wildfly -U -M -d $WILDFLY_HOME; \
chown -R wildfly:wildfly $WILDFLY_HOME; \
yum clean all
#Copying java application file in the image
copy ./sampleapp.war $WILDFLY_HOME/standalone/deployments
#Starting Wildfy
USER wildfly
CMD $WILDFLY_HOME/bin/ --server-config=standalone.xml -b

Upload or copy sampleapp.war file under docker directory.

/docker# cp /tmp/sampleapp.war ./
/docker# ls -l
total 16
-rw-r--r-- 1 root root 1203 Mar 5 11:50 dockerfile
-rw-r--r-- 1 root root 8618 Mar 5 11:54 sampleapp.war

So, now I have a dockerfile and the sampleapp.war file under the docker directory.

Build Docker Image

Let’s build my Docker image now by running docker build command. The ‘.’ implies the working directory, which is the docker directory that I created. Then I am tagging the image with version.

# docker build . -t myfirstimage:v1

Once the build is complete run the following command to check the image

# docker image ls
Docker image list

You can run the following command to verify or inspect the image

# docker inspect myfirstimage:v1

Run Docker Container

Run the following command to start the Docker container. This will expose the application using the IP address ( the host on port 80. Note that the backend port is 8080 as I exposed port 8080 within my image.

# docker run -d -p myfirstimage:v1

Once the container started successfully, I can access the application using the IP address and port that are using the expose the application from the container to the outer world.

I get the Wildfly home page on browsing

WildFly home page

Now, if I browse I get the the following page of my sample JAVA application.

Sample Web App

Clean up

Now run the following command to get the list of running containers in the system. Note the container ID in the output.

#  docker ps

To stop a running container run the following command

# docker stop <container ID>

If you want to delete the image, run the following command

# docker image rm -f myfirstimage:v1

Run the following command to clean up all unused containers, networks, images and free up space. Be careful before running this command and use appropriate option that you want to clean up.

# docker system prune

Thanks for reading, give it a 👏 if you like it. Please leave a comment and let me know if you have any feedback.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store