Getting Started with Docker: A Step-by-Step Guide

Learn the basics of Docker in this step-by-step guide. Install Docker, run containers, build custom images, and simplify your application deployment process. Get started with Docker today!

Getting Started with Docker: A Step-by-Step Guide
Getting Started with Docker: A Step-by-Step Guide

Introduction

Welcome to our step-by-step guide on getting started with Docker! Docker is a popular and powerful containerization platform that allows you to package and distribute applications along with their dependencies. Whether you are a developer, sysadmin, or anyone who works with software applications, understanding Docker is becoming increasingly essential in today's technology landscape.

In this guide, we will take you through the basics of Docker, explaining how it works and providing a hands-on tutorial to get you started. By the end, you will have a good foundation to start leveraging Docker in your own projects.

What is Docker?

Docker is an open-source containerization platform that allows you to build, package, and distribute applications using containers. A container is a lightweight and isolated runtime environment that includes everything needed to run an application - code, runtime, libraries, and system tools. Containers provide a consistent and reproducible environment, making it easier to build, deploy, and scale applications.

Key terms to know:

  • Image: A read-only template used to create containers. It contains the application code and all its dependencies.
  • Container: An instance of an image that can be run and executed on a host machine. Containers are isolated from each other and the host system, ensuring security and avoiding conflicts.
  • Registry: A repository for storing and distributing Docker images. Docker Hub is the default public registry, but you can also set up your own private registry if needed.

Installing Docker

Before we dive into the tutorials, let's first install Docker on your local machine. Docker provides installation packages for different operating systems.

1. Windows

If you are using Windows, you can download the Docker Desktop application from the official Docker website (https://www.docker.com/products/docker-desktop). Simply follow the prompts in the installer to complete the installation.

Once installed, Docker Desktop will run in the background and provide you with access to the Docker CLI (Command Line Interface).

2. macOS

For macOS users, the installation process is similar to that of Windows. Visit the Docker website (https://www.docker.com/products/docker-desktop) to download Docker Desktop for macOS. Follow the instructions in the installer, and Docker will be installed on your machine.

Once installed, Docker Desktop will run in the background and provide you with access to the Docker CLI.

3. Linux

Installing Docker on Linux varies depending on the distribution you are using. Docker provides detailed instructions for different Linux distributions on their website (https://docs.docker.com/engine/install/). Simply select your distribution and follow the instructions to install Docker.

After completing the installation, Docker will be up and running on your Linux machine.

Your First Docker Container

With Docker installed, let's create our first Docker container! We will start by pulling an existing Docker image and running it as a container.

1. Pulling an Image

Docker Hub is the default public registry that hosts a wide variety of pre-built Docker images. In this tutorial, we will use the official "hello-world" image to ensure everything is working correctly.

Open your terminal or command prompt and type the following command:

$ docker pull hello-world

Docker will download the latest version of the "hello-world" image from Docker Hub. This may take a moment depending on your internet connection speed.

2. Running a Container

Now that we have the "hello-world" image, let's run it as a container. In your terminal or command prompt, enter the following command:

$ docker run hello-world

Docker will first check if the "hello-world" image is already available locally. If not, it will download the image from Docker Hub. Once the image is available, Docker will create a new container from the image and execute it.

You should see the following output:

Hello from Docker!
This message shows that your installation appears to be working correctly. ...

Congratulations! You have successfully created and run your first Docker container.

Building Your Own Docker Image

While using existing Docker images is convenient, you'll often need to create your own custom images. Docker provides a straightforward process to build your own images using a Dockerfile.

1. Creating a Dockerfile

A Dockerfile is a text file that contains a set of instructions for Docker to build an image. Let's create a simple Dockerfile to build an image with a basic web server.

Open a text editor and create a new file named "Dockerfile" without any file extension.

Copy and paste the following contents into the Dockerfile:

# Use the official Python base image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

# Install the required dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY . .

# Expose a port for the web server
EXPOSE 80

# Run the web server
CMD ["python", "app.py"]

Let's go through the instructions in the Dockerfile:

  • FROM python:3.9-slim: This line specifies the base image we want to use. In this case, we are using the official Python 3.9 slim image.
  • WORKDIR /app: This line sets the working directory within the container to "/app".
  • COPY requirements.txt .: This line copies the "requirements.txt" file from the current directory on your local machine to the "/app" directory in the container.
  • RUN pip install --no-cache-dir -r requirements.txt: This line installs the required dependencies specified in the "requirements.txt" file.
  • COPY . .: This line copies the rest of the files and directories from your local machine's current directory to the "/app" directory in the container.
  • EXPOSE 80: This line exposes port 80 in the container to allow incoming connections.
  • CMD ["python", "app.py"]: This line specifies the command to run when the container starts. In this case, it runs the "app.py" file using the Python interpreter.

Save the Dockerfile.

2. Building an Image

With our Dockerfile ready, let's build the custom image. Open your terminal or command prompt and navigate to the directory containing the Dockerfile.

Run the following command to build the image:

$ docker build -t my-web-server .

The -t flag allows us to specify a tag for the image, in this case, "my-web-server". The dot (.) at the end of the command indicates that the Dockerfile is in the current directory.

Docker will now execute the instructions in the Dockerfile and build the image. This process may take a few moments, depending on the complexity of your Dockerfile and your internet connection speed. Once complete, you should see the image listed when you run the following command:

$ docker images

3. Running a Container from the Custom Image

Now that we have our custom image, let's run it as a container. In your terminal or command prompt, enter the following command:

$ docker run -d -p 8080:80 my-web-server

The -d flag tells Docker to run the container in the background (detached mode), and the -p flag is used to map the container's port 80 to the host machine's port 8080.

If everything goes well, Docker will create a new container from your custom image and start the web server.

You can now access the web server by opening a web browser and navigating to http://localhost:8080. You should see your web server running!

Conclusion

Congratulations on completing our step-by-step guide on getting started with Docker! You have learned the basics of Docker, including how to install Docker, run containers from existing images, and build your own custom images.

Docker is a powerful tool that can greatly simplify the deployment and management of application environments. By using containers, you can ensure your applications run consistently across different environments and streamline your software development processes.

Remember, this guide only scratches the surface of what Docker can do. There is still much more to explore, such as creating multi-container applications with Docker Compose, advanced networking, and orchestration with Docker Swarm or Kubernetes.

Now that you have a solid foundation, it's time to experiment and apply your newfound Docker skills to your own projects. Happy containerizing!