DEV Community

Cover image for Part-1 Overview of Docker
Vrukshali Torawane
Vrukshali Torawane

Posted on

Part-1 Overview of Docker

What is Docker?

Docker is a platform designed to help developers build, share, and run modern applications.

More simple definition:
Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in same ways you manage your applications. You can ship, test, and deploy code quickly, and significantly reduce delay between writing code and running it in production.

Docker

The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

What is container?

  1. Docker Containers contain binaries, libraries and configuration files along with the application itself.
  2. Docker Container is a standardised unit which can be created on the fly to deploy a particular application or environment. It could be an Ubuntu container, Debian container, etc. to full-fill the requirement from an operating system point of view.
  3. Docker Containers share resources with other containers in the same host OS and provide OS level process isolation.

Then what are Virtual Machines?

VM

  1. Virtual Machines (VMs) run on Hypervisors, which allow multiple Virtual Machines to run on a single Machine along with its own operating system.
  2. Each VM has its own copy of an operating system along with the application and necessary binaries, which makes it significantly larger and it requires more resources.
  3. They provide Hardware-level process isolation and are slow to boot.

Difference between Docker vs Virtual Machine

Difference

What is a Dockerfile?

  1. It is a text document that contains necessary commands which on execution helps assemble a Docker Image.
  2. Docker image is created using a Docker file.

What is Container Registry?

  1. Docker Hub is the official online repository where you can find other Docker Images that are available for use.
  2. It makes it easy to find, manage and share container images with others.

Dockerhub

Let's create a Docker image using Dockerfile:

Step 1: Make a directory

mkdir test and cd test
Enter fullscreen mode Exit fullscreen mode

Step 2: Now create a file Dockerfile
(File name is hard coded do not change the file name)

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a sample web page with name index.html

Custom docker image created using Dockerfile!!!
Enter fullscreen mode Exit fullscreen mode

Step 4: Edit the Dockerfile using following instructions

FROM centos:7
MAINTAINER Vrukshali
RUN yum install httpd -y
COPY index.html /var/www/html/
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]
EXPOSE 80
Enter fullscreen mode Exit fullscreen mode

Dockerfile commands explanation:

MAINTAINER — This command is used to give the information about the author or manager who is managing this image. MAINTAINER Vrukshali

RUN — Before building an image if want some configuration that needs to be present in the image. Inside the image we need to install Apache web server image the command to install that image is RUN yum install httpd -y

COPY - This command is used to copy a file from host os to docker container COPY index.html /var/www/html

EXPOSE - This command is used to specify the port number in which the container is running its process. Anybody can come from outside and connect to this port. Apache webserver is launched at port 80 by default that is why we need to expose container at port 80. EXPOSE 80

CMD - To run a command as soon as container is launched. CMD command is different from RUN because RUN is used at the time of building an image and CMD used to run command when container is started.
/usr/sbin/httpd - This command is used to start the web server
-DFOREGROUND— This is not a docker command this is http server argument which is used to run webserver in background. If we do not use this argument the server will start and then it will stop.
CMD [“/usr/sbin/httpd”,” -D”,” FOREGROUND”]

Step 5: Build the image using docker build.

docker build -t webserver:v1 .
Enter fullscreen mode Exit fullscreen mode

. is Dockerfile is present n current location and –t option is to tag or name the image.

Output:
last few lines

...
...
Step 6/6 : EXPOSE 80
 ---> Running in ed9972b66ac1
Removing intermediate container ed9972b66ac1
 ---> 5ca87e671dc1
Successfully built 5ca87e671dc1
Successfully tagged webserver:v1
Enter fullscreen mode Exit fullscreen mode

You can now run docker images command.
Output:

REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
webserver    v1        5ca87e671dc1   About a minute ago   392MB
centos       7         eeb6ee3f44bd   4 months ago         204MB
Enter fullscreen mode Exit fullscreen mode

Step 6: Run a container using docker image created by Dockerfile

docker run –dit –p 1234:80 webserver:v1
Enter fullscreen mode Exit fullscreen mode

-p: This argument is used to port forwarding. Which means anybody from outside who comes for 1234 its request is forwarded to port 80. Port 80 is default port number where apache webserver runs.

Step 7: To See the Result :
curl http://<host ip>:1234/ or on webpage http://<host ip>:1234/

Output:

Custom docker image created using Dockerfile!!!
Enter fullscreen mode Exit fullscreen mode

Now we need to push this image to central repository, here I will be using DockerHub as central registry.

Step 1: You need a DockerHub Account
You can create this on https://hub.docker.com/

Step 2: Login to DockerHub via terminal

docker login --username=yourhubusername --email=youremail@company.com
Enter fullscreen mode Exit fullscreen mode

With your own username and email that you used for the account. Enter your password when prompted. If everything worked you will get a message similar to

WARNING: login credentials saved in /home/username/.docker/config.json
Login Succeeded
Enter fullscreen mode Exit fullscreen mode

Step 3: Tag your image

docker tag <image id or image name>:<version> yourhubusername/<image_name>:<version>
Enter fullscreen mode Exit fullscreen mode
docker tag webserver:v1 vrukshali26/web:v1
Enter fullscreen mode Exit fullscreen mode

You can see using docker images

REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
webserver         v1        5ca87e671dc1   14 minutes ago   392MB
vrukshali26/web   v1        5ca87e671dc1   14 minutes ago   392MB
centos            7         eeb6ee3f44bd   4 months ago     204MB
Enter fullscreen mode Exit fullscreen mode

Step 4: Push your image to the DockerHub

docker push yourhubusername/<image_name>:<version>
Enter fullscreen mode Exit fullscreen mode

Output:

The push refers to repository [docker.io/vrukshali26/web]
a59ac102fa99: Pushed 
951641dc1264: Pushed 
174f56854903: Mounted from library/centos 
v1: digest: sha256:29169ceace4c1c0e29022d77453be7dcc97070d9ab1f5bbb640051ff13178f64 size: 948
Enter fullscreen mode Exit fullscreen mode

Now you can see on DockerHub, your first custom docker image has been pushed.

Top comments (5)

Collapse
 
maran143 profile image
maran143

Its a good introduction and clearly articulated for beginners.

Collapse
 
kelvinparmar profile image
Kelvin Parmar

Great blog vrukshali

Collapse
 
vrukshali26 profile image
Vrukshali Torawane

Thankyou!

Collapse
 
eteimz profile image
Youdiowei Eteimorde

Awesome article can't wait for part 2. In section Difference between Docker vs Virtual Machine would it be possible to add text explaining the difference between docker and VM.

Collapse
 
dexter38 profile image
Pascal Gautherot

Hi, in first step 3 for beginners:
echo "Custom docker image created using Dockerfile!!!" > index.html