Getting Started with Docker: Build and Run Your First Container, Docker For Beginners

Author - Sushmita Rimal
Jun 5. 5 min read


Docker Overview

Docker is a popular platform that simplifies the process of building, shipping, and running applications in a standardized and consistent manner. It does this by providing a way to package an application and its dependencies into a container, which can be easily deployed to any environment without the need for complex setup or configuration.

If you’re new to Docker, this guide will provide an overview of the core concepts and terminology you need to know to get started. We’ll cover everything from installing Docker on your machine to creating and running your first container.

Installing Docker

Before we dive into the details of Docker, let’s first get it installed on your machine. Docker is available for Windows, Mac, and Linux, and can be downloaded from the official website (https://www.docker.com/get-started).

Once you’ve installed Docker, you should be able to run the following command in your terminal to verify that it’s installed and working properly:

docker --version

If Docker is installed correctly, this command should output the version number of your Docker installation.

Docker Concepts

Now that you have Docker installed, let’s take a look at some of the key concepts and terminology you’ll encounter when working with Docker.

Images

At the heart of Docker is the concept of an image. An image is a lightweight, stand-alone, and executable package that contains everything needed to run a piece of software, including the code, libraries, and system tools. Images can be thought of as blueprints for containers.

Containers

A container is a running instance of an image. Containers are isolated environments that are completely self-contained, meaning they have their own file system, network, and processes. Containers can be started, stopped, and destroyed quickly and easily.

Registries

Docker images can be stored and shared in a registry, which is like a library of images. The most popular registry is Docker Hub, which is a public registry that allows you to store and share Docker images. You can also create your own private registry if you need to store images that are specific to your organization or project.

Dockerfile

A Dockerfile is a script that is used to build a Docker image. It contains a series of commands that are executed in order to create an image from a base image. The Dockerfile specifies the base image, copies the application code and any required dependencies into the image, and sets the configuration settings for the image.

Docker Compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. It uses a YAML file to define the services that make up your application and their relationships. With Docker Compose, you can start, stop, and manage all the containers in your application with a single command.

Getting Started with Docker

Now that you understand the core concepts of Docker, let’s create and run your first container.

Step 1: Create a Dockerfile

First, create a file named index.html and copy-paste the following code, replacing "(write your name)" with your own name:


    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello Docker</title>
    </head>
    <body>
        <h1>Hi, my name is ( write your name).</h1>
    </body>
    </html>
        

Create a new file called Dockerfile in your project directory, as same where index.html is created, with the following contents:


    FROM nginx
    COPY index.html /usr/share/nginx/html
        

Step 2: Build the Image

In your terminal, navigate to the directory containing the Dockerfile and run the following command to build the Docker image:

docker build -t my-nginx-image .

This command tells Docker to build an image using the Dockerfile in the current directory and tag it with the name my-nginx-image.

Step 3: Run the Container

Once the image is built, we can run a container from it with the following command:

docker run -d -p 8080:80 my-nginx-image

This command tells Docker to run a container in detached mode (-d) and map port 8080 on the host machine to port 80 in the container (-p 8080:80). The container is created from the my-nginx-image image we just built.

Step 4: View the Application

Finally, open a web browser and navigate to http://localhost:8080 to see the default index.html page served by the NGINX web server running in the container.

Conclusion

Congratulations, you have now created and run your first Docker container! While this guide only scratches the surface of what Docker can do, it should give you a solid foundation to build on as you continue to learn and explore the world of containers.

Remember, Docker is a powerful tool that can greatly simplify the process of building, shipping, and running applications. By taking advantage of its many features and tools, you can streamline your development process and make your applications more scalable, efficient, and reliable.

Follow me on LinkedIn.


People who read this also read

article

Linux Commands Every DevOps Engineer Should Know- Part 1

Sushmita Rimal
25 March | 3 min read
article

Transitioning into DevOps: A Journey from Network and System Background

Sushmita Rimal
07 Jun | 5 min read
canada vs australia

Canada vs Australia for Nepali

Sushmita Rimal
22 July | 15 min read