Docker for Beginners


Docker is a tool that makes application deployment easier with the use of containers. Containers allow a developer to package an application along with its required libraries and system dependencies in an isolated environment, containers can run on a variety of operating system environments with varying configurations which help in limiting the "it works on my machine" problem developers experience.

In this tutorial, you will be learning about Docker's basic concepts and how to serve a simple PHP script using docker.

Docker images

A Docker Image is a file that contains a series of instructions that describes how a container should be built, it specifies the libraries and dependencies required by an application to run. An instance of a docker image is a container, and multiple containers can be created from a single image. Images are usually stored in registries, registries allow developers anywhere in the world to publicly or privately access images for their projects. The major registry for docker images is Docker Hub.

Docker containers

A container is an isolated environment created from a docker image, it possesses all the attributes, dependencies and libraries specified in a given docker image necessary for an application or service to execute within the container.


Heads up

An instance of an image is called a container. You have an image, which is a set of layers as you describe. If you start this image, you have a running container of this image. You can have many running containers of the same image.


  • Intermediate Programming experience
  • One bare metal server running Ubuntu 18.08, quickly setup one on MaxiHost

Step 1 - Install Docker

In this step, you will be installing Docker on your machine. First, SSH into your server using the credentials provided to you or configured by you, then run:

sudo apt-get update

to update the server's package repository. Run the following to allow apt use repositories over a secure connection:

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \

Add Docker's official GPG keys by running:

curl -fsSL | sudo apt-key add -

You will get a response OK. Next, add Docker's official stable repository to enable you to install the latest stable version of Docker as well as receive future software updates by running:

sudo add-apt-repository \
   "deb [arch=amd64] \
   $(lsb_release -cs) \

Update the package index by running:

sudo apt-get update

Finally, you can now install docker along with its recommended packages by running:

sudo apt-get install docker-ce docker-ce-cli

To check the version of docker installed, run:

docker version

Docker version

You have successfully installed Docker on your machine. In the next step, you will be building a simple Hello World application to e deployed on docker.

Step 2 - Build the Hello World App

The application we will be building will print "Hello World" along with the current time using PHP. To get started, create a new directory named /public in your preferred workspace or directory.

mkdir public

Change to the public directory.

cd public

Create a new file titled index.php in the /public directory.

nano index.php

Paste the following code snippet in the index.php file:

<!DOCTYPE html>
   <meta charset="utf-8">
   <title>Hello World</title>
   Hello World. The time is <?php echo date("h:i:sa"); ?>

Step 3 - Create a Docker container

In this step, we will create a file Dockerfile in the root directory of our project. Before we begin, what is a Dockerfile? A Dockerfile is a text file that contains all the commands necessary to compose an image via an automated build.

To create the Dockerfile run:

nano Dockerfile

Paste the following code snippet in the Dockerfile file:

FROM php:7.2-apache
COPY public/ /var/www/html/

Let us break down what these lines in our Dockerfile will do when executed:

  • FROM php:7.2-apache
    This pulls the Apache server image from Docker Hub with PHP 7.2 pre-configured.

  • COPY public/ /var/www/html/
    This copies the contents in /public directory into /var/www/html/ directory which is a virtual directory in our container that Apache will try to serve files from.

Step 4 - Building our docker image

To build our Dockerfile we need to run the following in the root directory of our project:

docker build -t php-hello-world .

Let us break down what this command does bit by bit:

  • docker build
    This command builds and creates an image from a Dockerfile.

  • -t
    This argument tags the name of the image as php-hello-world.

  • .
    This indicates that the Dockerfile is in the current directory along with other files required in the Dockerfile context.

Result from the build

Step 5 - Run the container

To run our container, we must execute the following command:

docker run --name=php-hello-world -d --rm -p 90:80 php-hello-world

Let us break down what this command does bit by bit:

  • docker run
    This command tries to generate and start a container from a given image along with specified configurations.

  • --name=php-hello-world
    This option gives the container a name; in this case, it is named php-hello-world.

  • -d
    This argument runs the container in detached mode. In other words, the container will run in the background.

  • --rm
    This option will ensure that the container is destroyed when it is stopped, this is good to save disk memory.

  • -p 90:80
    This argument binds the internal Apache port of 80 to an external port of 90 so that our container can be accessed from port 90.

  • php-hello-world
    This tells docker which image we are building our container from.

This will generate a container from the image built from our Dockerfile and the name of the container will be php-hello-world. To view the container details run:

docker ps

This will return a table view of the container attributes.

Container attributes

Step 6 - Accessing our application

At this point our application is running in a docker container that can be accessed from port 90, by visiting http://<YOUR-SERVER-IP>:90 on a web browser, we should be able to view our application.

Application in Docker

As shown in the image above, our application is accessible via port 90.

Step 7 - Stop the container

We can stop the running container by running a command in this format:


Therefore, by running:

docker stop php-hello-world

Our application container will be stopped and removed because in Step 4 we specified that the container should be destroyed when it is stopped. So if we run docker ps we will not find any container listed.

No ContainerNo Container

If we try visiting our application on port 90 it will be unreachable because the container has been stopped and destroyed.

Step 8 - Push your custom image to Docker Hub

In this step, you will learn how to upload your custom image to Docker Hub. First, go to Docker Hub to create an account if you don't have one already.

Docker HubDocker Hub

Once you have created an account, go to terminal and run the following command:

docker login

You will be asked to input your username and password from Docker Hub, input your credentials and enter after each prompt.

Docker LoginDocker Login

Once you have logged in you will be able to package and push your custom docker image to Docker Hub.

First, you need to rebuild your image by following this command format docker build -t <username>/<image-name> . In this case, my Docker Hub username is ichtrojan and the name of the image will be php-hello-world.

docker build -t ichtrojan/php-hello-world .

This will build a new image and tag it as ichtrojan/php-hello-world. Next, we have to push our image by running this command format docker push <your-username>/<image-name>. In this case, my Docker Hub username is ichtrojan and the name of the image will be php-hello-world.

docker push ichtrojan/php-hello-world

docker pushdocker push

This will push your custom container to the Docker Hub repository where anyone can access and use your custom image in their projects. If you check your docker hub dashboard on the web you will find your new custom image listed.

docker dashboarddocker dashboard

Anyone running Docker can pull your custom image and run hello world application written in PHP on their local machine without having PHP or Apache installed locally.

Step 9 - Pull and run custom docker image

In this step, you will learn how to pull and run your custom image on another machine running Docker. First, you need to pull your custom image by running this command format docker push <your-username>/<image-name>. username is the image author and image-name is the name of the image. we will be pulling my custom image by running.

docker pull ichtrojan/php-hello-world

docker pulldocker pull

Once that is done executing, you will be able to run the container by running:

docker run -p 90:80 ichtrojan/php-hello-world

docker rundocker run

you will be able to access the application from http://<YOUR-SERVER-IP>:90



Docker enables the deployment of your applications with the use of containers. Applications can be packaged and distributed across multiple environments without any worries and remove setup time from installing dependencies for a project on development and in production.

Did this page help you?