Docker from scratch — All you need to know

What is Docker?

Abhishek
Abhishek
Nov 30, 2020 · 5 min read
  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 ?

Image for post
Image for post

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
Image for post
Image for post

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.
Image for post
Image for post
Image for post
Image for post
  • 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.
Image for post
Image for post

Step 1. Lets create a Dockerfile

  • I have created a simple file named Dockerfile, with the following content
Image for post
Image for post
  • 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
Image for post
Image for post

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.

Image for post
Image for post

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))
Image for post
Image for post
Image for post
Image for post
  • Build using docker build -t apache-httpd
Image for post
Image for post
  • 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
Image for post
Image for post
  • Result
Image for post
Image for post

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
Image for post
Image for post
2. See all running Docker Containers using: docker container ls
Image for post
Image for post

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
Image for post
Image for post

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.

Image for post
Image for post

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.

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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