Cloud Native 6 min read

Master Docker: From Basics to Dockerfile Best Practices

This guide introduces Docker’s core concepts, walks through installing Docker on Linux, Windows, and macOS, explains Dockerfile syntax with common commands, and shares practical tips for building efficient container images.

Ray's Galactic Tech
Ray's Galactic Tech
Ray's Galactic Tech
Master Docker: From Basics to Dockerfile Best Practices

Core Concepts

Image : read‑only template that contains all instructions to create a container.

Container : a running instance of an image that can be started, stopped, or removed.

Repository : storage location for images (e.g., Docker Hub, private registries such as Harbor).

Installation

Linux (Ubuntu)

Remove old versions:

sudo apt-get remove docker docker-engine docker.io containerd runc

Install dependencies:

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Add Docker GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add stable repository:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Install Docker Engine:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Verify installation:

sudo docker run hello-world
Tip: To run Docker without sudo, add your user to the docker group:
sudo usermod -aG docker $USER
newgrp docker

Windows / macOS

Download Docker Desktop.

Run the installer and follow the wizard.

Start Docker Desktop.

Verify with docker --version and docker run hello-world.

Dockerfile Overview

A Dockerfile is a plain‑text script that defines a sequence of instructions to build an image.

Common Instructions

FROM : specify base image (e.g., FROM ubuntu:20.04).

RUN : execute a command during build (e.g., RUN apt-get update && apt-get install -y python3).

COPY : copy files from build context into the image (e.g., COPY . /app).

ADD : similar to COPY but supports URLs and automatic extraction (e.g., ADD https://example.com/file.tar.gz /tmp/).

WORKDIR : set working directory (e.g., WORKDIR /app).

ENV : set environment variables (e.g., ENV PATH /usr/local/bin:$PATH).

EXPOSE : declare listening port (e.g., EXPOSE 80).

CMD : command executed when container starts (single command, e.g., CMD ["python", "app.py"]).

ENTRYPOINT : fixed entrypoint that cannot be overridden by docker run arguments (e.g., ENTRYPOINT ["python"]).

ARG : build‑time variable (e.g., ARG version=latest).

Sample Dockerfile

# Base image
FROM python:3.8-slim

# Set work directory
WORKDIR /app

# Copy project files
COPY . /app

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

# Expose port
EXPOSE 80

# Environment variable
ENV NAME World

# Startup command
CMD ["python", "app.py"]

Building and Running

# Build the image
docker build -t my-python-app .

# Run the container, mapping host port 4000 to container port 80
docker run -p 4000:80 my-python-app

Best Practices

Use a .dockerignore file to exclude unnecessary files from the build context.

Run a single main process per container.

Combine multiple RUN commands with && to reduce image layers.

Prefer official base images.

Avoid the latest tag; pin specific versions for reproducibility.

Leverage multi‑stage builds to minimize final image size.

Use volumes for persistent data instead of writing inside the container.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockercontainerizationLinuxbest practicesWindowsDockerfilemacOS
Ray's Galactic Tech
Written by

Ray's Galactic Tech

Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow together!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.