Docker 101: A Beginner’s Guide to Containerization

Mohammed Affan
AWS in Plain English
7 min readJul 21, 2023

--

Welcome to this beginner’s guide to Docker! In this article, you’ll gain a solid understanding of Docker, why it’s essential, and how to get started using it. And explore why Docker has become an essential tool for modern software development and deployment.

What is docker?

Docker is a containerization platform that allows developers to create lightweight, portable and self-sufficient containers. In containerization, applications are bundled together in a single unit with all the necessary software and runtime components they need to run. This includes libraries, dependencies, and configurations, ensuring that the application operates consistently, regardless of the environment it runs on.

Advantages of docker:

  1. Dependency Management: Docker enables developers to package their applications along with all dependencies into a single unit, known as a container. This simplifies dependency management and ensures that your application runs consistently across various environments.
  2. Isolation: Docker containers run in isolation from each other and from the underlying host system. This means you can run multiple applications with different runtime requirements on the same machine without worrying about conflicts or interference.
  3. Scalability: Docker’s lightweight nature allows for quick and efficient scaling of applications. You can easily scale containers both horizontally (by adding more instances) and vertically (by increasing resources for a single container).
  4. Rapid Deployment: Docker streamlines the deployment process, significantly reducing the time it takes to get your applications up and running. Its ability to create consistent environments ensures seamless deployment across various platforms.
  5. Version Control: Docker images provide versioning capabilities, allowing developers to track changes and easily revert to previous versions if needed. This ensures better control and flexibility during the development lifecycle.

Why do we need docker?

Docker offers a range of advantages that make it an ideal solution for the production of large applications. Have you ever experienced the frustration of developing an application that runs perfectly on your machine, but when it’s handed over to the production team, they struggle with dependency issues? This scenario is quite common for developers.

Docker comes to the rescue by addressing these operational challenges. With Docker, you no longer need to worry about dependency conflicts. It creates a consistent environment for your application to run, ensuring that what works on your machine will work reliably in production.

The deployment process becomes remarkably rapid and smooth with Docker. Instead of manually configuring the production environment and dealing with potential inconsistencies, you can simply package your application into a Docker container, ensuring that all necessary dependencies are included. This container can then be easily deployed to any environment that supports Docker, be it on-premises or in the cloud.

By leveraging Docker, you bridge the gap between development and operations, streamlining the workflow and reducing the chances of errors during deployment. It provides a level of consistency and reliability that traditional deployment methods often struggle to achieve.

Key concepts of Docker:

Docker image: An image is a snapshot or template used to create containers. It includes the application code, runtime, libraries, environment variables, and other dependencies needed to run the application. Think of images as blueprints for containers. They define everything your app needs to run, from the code to the environment variables.

Containers: Containers are the living instances of Docker images. When you run a Docker image, it creates a container. These containers are isolated environments where your applications can run independently, avoiding any interference with other apps. It’s like providing each app with its own cozy space to work peacefully, without causing chaos or conflicts with other applications.

Dockerfile: A Dockerfile is a simple text file that contains instructions to build a Docker image. Think of it like a recipe card for building images. You list all the ingredients (dependencies, configurations) your app requires to create a Docker image.

Registry: Docker Hub is a popular public registry where you can find a wide range of pre-built Docker images. Think of it like a supermarket of ready-to-use images. It’s a public registry where you can find popular images for different applications. You can also set up private registries for your team’s exclusive use.

Volumes: Containers are temporary, and data usually doesn’t stick around. Volumes come to the rescue! They help you store data outside the container, ensuring it survives container restarts and can be shared among containers.

Getting Started with Docker:

Here’s a step-by-step guide to getting started with Docker:

  1. Installation: First things first, get Docker installed on your system. Head over to Docker’s website and follow the simple instructions for your operating system.

2. Hello, Docker!: Open your terminal or command prompt and type:

docker run hello-world

This magical command will pull a tiny “hello-world” image from Docker Hub and run it in a container. If you see a friendly message, Docker is good to go!

3. Creating a Dockerfile: First, you need to clone this app from Git Hub. Then you need to create an empty Dockerfile in the same directory as your app. You should name it exactly ‘Dockerfile’. Open your Dockerfile in any text editor and paste the following content.

# Use the official Apache image as the base image
FROM httpd:latest

# Copy the contents of the website folder to the Apache document root
COPY ./simple-static-site /usr/local/apache2/htdocs/

Your working directory should look like this.

4. Creating a Docker image: Use the following command inside your shell or terminal to create a Docker image.

docker build -t my_static_website .

You’ll see a similar output to this.

In this command, -t stands for tag. We can give our image a name using -t. In this case the name of our image is ‘my_static_website’

5. Creating the container: Once your image is created you can create a container by using the following command.

docker run -d -p 80:80 my_static_website

In this command, the -d flag runs our container in ‘detached’ mode or in the background. And the -p flag exposes the docker container’s port 80 to your computer’s localhost:80. And in the end, we’ve specified the image name.

After running this command You’ll see a similar output like the following.

6. Accessing the app: Now the container is successfully running. We can go to the localhost:80 and see our web page. Type localhost:80 into your browser and you’ll see the following page.

A note about dockerfile:

To create a dockerfile you don’t need to remember the syntax. You can always refer to this comprehensive documentation. Every dockerfile requires a base image. In our case, we are using ‘httpd:latest’. If you are wondering where did I get this image? you can see it on Docker Hub, and if you scroll down you’ll see documentation for that image. Docker Hub contains a lot of images for web servers including httpd, ngnix and tomcat etc. It also have various app images and database images. I encourage you to explore Docker Hub to get some familiarity.

Cleanup:

In this section, we will stop the running container and delete the docker image.

  1. You can see the running containers by using ‘docker ps’ command. Use the ‘docker ps’ command and you’ll see this output.

2. We need to stop the container first to delete it. To stop a container use ‘docker stop <container_id or name>’.

3. Once your container is stopped and you use ‘docker ps’ command you’ll see nothing. Because ‘docker ps’ shows only the running containers. To see all containers both stopped and running use ‘docker ps -a’ command.

4. Now the container is stopped. So we can delete it using ‘docker rm <container_id or name>’

5. The container is deleted! Now it’s turn to delete the image. To list all images use ‘docker images command’.

As you can see I have three different images. To delete the ‘my_static_website’ image use the ‘docker rmi <image_name:tag>’.

Last words:

Congratulations! 🎉️ You’ve made it to the end of the blog. If you like this blog don’t forget to leave a few claps and comments. Follow me on medium for similar content.

I look forward to connecting with you on LinkedIn: https://www.linkedin.com/in/mohammedaffan7/

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

--

--

DevOps Engineer♾️| Cloud Computing☁️| Linux🐧| AWS🖥 | Python🐍| Docker⚓| Kubernets🛞| Terraform⚙️| Connect with me👇 www.linkedin.com/in/mohammedaffan7