Docker: A Basic Introduction

docker tutorial basics

Docker is a platform that allows developers to automate the deployment, scaling, and management of applications using containers. Containers are lightweight, standalone, and executable packages that include everything needed to run a piece of software: code, runtime, libraries, environment variables, and configuration files.

Why Use Docker?

  • Consistency: Works the same way on your machine, in testing, and in production.
  • Isolation: Each app runs in its own container, avoiding conflicts.
  • Portability: “It works on my machine” is no longer an issue.
  • Efficiency: Containers share the host OS kernel, making them lighter than virtual machines.

How Docker Works

Docker uses a client-server architecture:

  • Docker Engine: The background service that builds, runs, and manages Docker containers.
  • Docker Client: The command-line tool (docker) you use to interact with the engine.
  • Docker Hub: A cloud-based registry service where Docker images are stored and shared.

Getting Started: Installing Docker

1. Install Docker

  • Windows/macOS/Linux: Download and install Docker Desktop.
  • Alternative, on Linux: Use your package manager, e.g.,
  sudo apt-get install docker-ce docker-ce-cli containerd.io

2. Verify Installation

docker --version

You should see something like:

Docker version 24.0.5, build ...

Running Your First Docker Container

1. Pull an Image

Images are the blueprints for containers. Let’s pull the official hello-world image:

docker pull hello-world

2. Run a Container

docker run hello-world

You’ll see a message like:

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

Understanding Docker Commands

CommandDescription
docker pull <image>Downloads an image from Docker Hub
docker run <image>Creates and starts a container from an image
docker psLists running containers
docker ps -aLists all containers (including stopped ones)
docker stop <container>Stops a running container
docker rm <container>Removes a container
docker imagesLists downloaded images

Building Your Own Docker Image

1. Create a Dockerfile

A Dockerfile is a script that contains instructions to build an image.

Example:
Create a file named Dockerfile with the following content:

# Use an official Python runtime as the base image
FROM python:3.10-slim

# Set the working directory
WORKDIR /app

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

# Install any needed packages
RUN pip install flask

# Make port 5000 available to the outside world
EXPOSE 5000

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

2. Build the Image

docker build -t my-flask-app .

3. Run the Container

docker run -p 5000:5000 my-flask-app

Now, open your browser and go to http://localhost:5000 to see your Flask app running inside a Docker container!

Leave a Reply

Your email address will not be published. Required fields are marked *