Monday, November 26, 2018

DockerFile - Create Docker image and install Apache2 on Ubuntu16.04


Docker: 
Docker is the most revolutionized technology in virtualization world nowadays. Docker is actually an open source project which provides container technology. A container is a lightweight VM(virtual machine) or a process which allows us to install Linux based applications inside it. The container doesn’t have its own Kernel, RAM, CPU and Disk but it uses the underlying OS kernel, RAM, CPU cores and Disk.

In my this blog, I'm going to demonstrate, How to use Dockerfile to build own image with custom software. I'll install Apache2 server using Ubuntu 16.04 official docker image.



Prerequisites: 

  • An Ubuntu box with installed Docker.
  • One User with sudo access.
Step 1- Install Docker in Ubuntu 16.04:

Step 2-  Create Dockerfile:
In order to create Dockerfile, please follow the steps below.
  • Create  Directory and cd into it.
$ sudo mkdir webserver && cd webserver
  • Create index.html file
$ sudo echo "Docker image build by Amar Singh" > index.html
  • Create Dockerfile
$ sudo vi Dockerfile
Add following line into this
# Source Image name
from ubuntu:latest
# Mainter Name
maintainer Amar Singh
# Command to update and install packages
RUN apt-get update && apt-get install apache2 -y
# To copy index.html file to container's directory
copy index.html /var/www/html/
# open port
EXPOSE 80
# Command to run Apache server in background
CMD /usr/sbin/apache2ctl -D FOREGROUND

Save and Exit from the file.
Step 3-  Build Docker image using Dockerfile:
Now, We will build our Image using Dockerfile, let's run following command to build Docker image.

$ sudo docker build -t apache:2.4 .
Where: 

docker build                   -                              Is command to build an image
-t                                     -                              To assign a tag to the Image
apache:2.4                      -                              Image tag name

After successful execution of above command, your Image will be ready.
Let's check Image using command "docker image ls"

$ sudo docker image ls

REPOSITORY      TAG                 IMAGE ID            CREATED             SIZE
apache              2.4                 14c6000afa71        3 days ago          206MB

ubuntu              latest              93fd78260bd1        6 days ago          86.2MB

Our docker image is ready, let's launch a container. 

Step 4- Launch a container using our build image:
In order to run a container, execute the following command on the terminal 

$ sudo docker run -dit -p 80:80 --name web1 apache:2.4
Let's check created container :

$ sudo docker container ls

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
744e0d2ac78d        apache:2.4          "/bin/sh -c '/usr/sb…"   24 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp   web1

Now, open your browser and hit docker main machine IP address: http://10.0.1.170/















Your custom docker image is ready.

Step 5- Push your Custom image to Docker hub account
 You can upload your custom docker image to your own docker hub account and can be used over any system anytime. Let's follow the steps below to push your docker image to Docker hub account.

  • Sign up to https://hub.docker.com/ if you don't have a docker hub account
  • Sign in to your docker hub account on your Linux server.

$ docker login -u amarsingh3d

Password: *******
WARNING! Your password will be stored unencrypted in /home/ishir/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded


Enter your Docker hub login password. After, successful authentication your output will be "Login Succeeded"
  • First, check your available Images:

$ sudo docker image ls

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
apache                  2.4                 14c6000afa71        3 days ago          206MB
ubuntu                  latest              93fd78260bd1        6 days ago          86.2MB

  • Tag your custom image with your docker hub account
$ sudo docker tag apache:2.4 amarsingh3d/apache2.4

Where:
docker tag                      -                is a command to tag Image
apache:2.4                     -                Image name that we will tag with our docker hub account name
amarsingh3d                  -                our docker hub account name
/apache2.4                     -                Tag name 
  • Let's check your tag and image

$ sudo docker image ls

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
amarsingh3d/apache2.4   latest              14c6000afa71        3 days ago          206MB
apache                           2.4                 14c6000afa71        3 days ago          206MB
ubuntu                           latest              93fd78260bd1        6 days ago          86.2MB


  • Now, Push your custom image.
$ sudo docker push amarsingh3d/apache2.4

The push refers to repository [docker.io/amarsingh3d/apache2.4]
a2752ebde643: Pushed
d96b366a0677: Pushed
b9b7103af585: Pushed
ca2991e4676c: Pushed
a768c3f3878e: Pushed
bc7f4b25d0ae: Pushed
latest: digest: sha256:f9ba1973976d6a92877b3e2349e13ad654bb1c29519d847e055c25007c54c9d4 size: 1569

You have successfully pushed your custom docker image to your Docker hub account.

No comments:

Post a Comment