The Startup
Published in

The Startup

Docker from scratch — All you need to know

What is Docker?

  1. Docker is an open platform for developing, shipping, and running applications.
  2. It enables us to separate applications from the infrastructure so anyone can deliver software quickly.

Why Docker?

In a nutshell: Write Code Once -> Create Docker Container -> Run Anywhere

  1. Anyone write code locally and share their work with anyone using Docker containers.
  2. They can use Docker to push their applications into a test environment and can execute automated or manual tests.
  3. When testing is complete, getting the fix (For e.g. to the customer) is as simple as pushing the updated image to the production environment.

How Docker Works ?

  1. Docker provides the ability to package and run an application in a loosely isolated environment called a container.
  2. The isolation and security allow us to run many containers simultaneously on a single host.

Sounds Interesting but how does it differ from Virtual Machines ?

Some advantages of Docker Containers over Virtual Machines

  • Fast Execution Speed
  • Low RAM and Drive memory usage
  • Very Fast Startup Time (generally in milliseconds :-)

Dockerfile, Image & Container

  • A Docker Image is basically a set of rules or template for creating an environment
  • We can create docker images using Dockerfile
  • Dockerfile is basically a text file containing a set of instructions for creating the Image
  • A Docker Container is basically a running instance of an Image

Enough Talk… Lets Create some Conatiners

Python Hello World inside a Docker Container

  • For this I have created a python file ‘main.py’ which contain a print statement.
  • Under Simple Tags section, we can see different types of versions & flavours of python image available. We will we using 3.8-alpine since alpine’s images are very less in size.

Step 1. Lets create a Dockerfile

  • I have created a simple file named Dockerfile, with the following content
  • FROM is used to refer the Docker Image from DockerHub repository.
  • WORKDIR is basically our working directory inside the image. It is similar to cd(change directory command) in Linux / Windows.
  • COPY command is use to copy files/folders inside the image
  • CMD is used to run any command in the container’s terminal.

Step 2. Building the Image

The command to build the above dockerfile isdocker build -t python_hello .-t : specifies the name of image (python_hello)
.’ : is the location of dockerfile

Step 3. Lets Run the Container

The Command isdocker run python_hello

Here is the output we got. The container starts, executed our command and exited.

Some More Examples of Dockerfile

  1. Creating an Apache HTTP Server
  • Dockerfile (Expose 80 tell the container to listen on port 80 (default HTTP port))
  • Build using docker build -t apache-httpd
  • Run using docker run -p 8080:80 apache-httpd
  • -p 8080:80 forwards the 80 port inside the container to 8080 port on Host machine
  • Result

Inspecting a running Docker Container

Now your docker container is successfully up & running. How great it will be if we can see its running state and perform various operations like

  • listing the directory structure
  • see & modifying the content of any file
  • running any base unix/linux command (depends upon the host OS and base docker image) etc.

Lets consider our Docker Image for Apache HTTP Server is up & running.

1. See all Docker Images using command: docker image ls
2. See all running Docker Containers using: docker container ls

As you can see our Apache Container is running with Container ID 25d3219ce4e7. Note this Container ID which can be used to uniquely identify every running docker instance.

3. Now to run any command you can use docker exec
For e.g. (considering Host OS is Linux)
+ Get Current Directory / WORKDIR
docker exec 25d3219ce4e7 pwd
+ List every file in WORKDIR inside container
docker exec 25d3219ce4e7 ls
+ Print Content of a file inside container
docker exec 25d3219ce4e7 cat htdocs/index.html
+ Ping google.com from inside the container
docker exec 25d3219ce4e7 ping www.google.com
+ We can even get a complete shell of that container using
docker exec 25d3219ce4e7 bash (OR)
docker exec 25d3219ce4e7 sh

Now, what’s Next?

Please also have a look at my next blog of this series as well. In this article we will experience the magic of docker-compose by using it to combine multiple docker images.

Congratulation on sucessfully building your docker container & Thanks for sticking till the end.

Please let me know about your views or queries in the comment section.

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Java Data API Annotation Processor

Open Source is at the heart of the way we work — by Sam Debruyn

Quika: Being Discontinued…

READ/DOWNLOAD#^ Kotlin in Action FULL BOOK PDF & FULL AUDIOBOOK

What’s New At Releem | May 2022

How to get SegNet running in year 2020 (or 2021)

Implementing and Testing IP SafeLists in ASP.NET Core

📣CONTEST TIME!!!!📣

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
Abhishek

Abhishek

❤️#!/bin/bash❤️

More from Medium

Heroku Deploy using CLI

_mysql is not defined, Django on Mac M1 with MySQL DB

Installing Gitea Docker Image

How to connect Django application with Microsoft SQL Server with Domain Credentials and also…