Getting Started with Docker: A Beginner's Guide


  
MontaF - Aug. 31, 2024

8
0

...

In today's tech landscape, containers have become a game-changer for developers and operations teams alike.

Docker, one of the most popular containerization tools, simplifies the process of creating, deploying, and running applications in a consistent environment.

If you're new to Docker and wondering how to get started, you've come to the right place.

In this guide, we'll walk you through the basics of Docker, including installation, core concepts, and how to run your first container.


What is Docker?

Docker is a platform that uses containerization technology to package applications and their dependencies into a standardized unit called a container. Containers are lightweight, portable, and consistent across different environments, which makes them ideal for development, testing, and production.


Key Docker Concepts

Before diving into the installation process, let’s familiarize ourselves with some key Docker concepts:

  • Docker Image: A read-only template that contains a set of instructions to create a Docker container. Images are built from a Dockerfile and can be shared via Docker Hub or other registries.
  • Docker Container: A running instance of a Docker image. Containers are isolated from each other and from the host system, providing a consistent runtime environment.
  • Dockerfile: A text file with a set of instructions for building a Docker image. It includes commands to set up the environment, install dependencies, and configure the application.
  • Docker Hub: A cloud-based registry where Docker images can be stored and shared. It’s a public repository, but private repositories are also available.



Step 1: Installing Docker

Docker provides packages for various operating systems, including Windows, macOS, and Linux.

Follow the appropriate installation instructions for your platform:


For Windows and macOS

  1. Download Docker Desktop:
  2. Go to the Docker Desktop download page.
  3. Choose the version for your operating system and download the installer.
  4. Install Docker Desktop:
  5. Run the installer and follow the on-screen instructions.
  6. Once installed, Docker Desktop will start automatically.
  7. Verify Installation:
  8. Open a terminal or command prompt and run the following command: docker --version
  9. You should see the Docker version displayed, confirming that Docker is installed correctly.


For Linux

  1. Install Docker Engine:
  2. Follow the instructions specific to your Linux distribution from the Docker documentation.
  3. Start and Enable Docker:
  4. Run the following commands to start Docker and enable it to start on boot:
  5. sudo systemctl start docker
  6. sudo systemctl enable docker
  7. Verify Installation:
  8. Check the Docker version with: docker --version



Step 2: Your First Docker Container

Now that Docker is installed, let’s run your first container to ensure everything is working correctly.

1.Open a Terminal or Command Prompt.

2.Run a Test Container:

Execute the following command to run a simple "Hello World" container:

docker run hello-world

This command will download a test image from Docker Hub, create a container, and run it. You should see a message confirming that Docker is working.



Step 3: Working with Docker Images

Let's build and run a custom Docker image to get a better understanding of how Docker images work.

1.Create a Dockerfile:

Create a new directory for your project and navigate into it:

mkdir my-docker-app 
cd my-docker-app


Create a file named Dockerfile (no file extension) with the following content:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]


2.Create an Application File:

In the same directory, create a file named app.py with the following content:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, Docker World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)


Create a requirements.txt file with the following content to specify the required Python packages:

Flask==2.0.2


3.Build the Docker Image:

Run the following command to build your Docker image:

docker build -t my-docker-app .

The -t flag tags your image with a name, and the . indicates the current directory as the build context


4.Run the Docker Container:

Start a container from your image with:

docker run -p 4000:80 my-docker-app

The -p flag maps port 4000 on your host to port 80 in the container.


5.Access Your Application:

Open a web browser and go to http://localhost:4000. You should see the message "Hello, Docker World!"


Conclusion

Congratulations! You've just built and run your first Docker container. Docker is a powerful tool that can greatly streamline your development workflow by providing a consistent environment across different stages of the software lifecycle. From here, you can explore more advanced Docker features such as Docker Compose, Docker Swarm, and Kubernetes to manage multi-container applications and orchestration.

Happy containerizing!



Comments ( 0 )
Login to add comments