Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

dockercontainerizationLinuxBest PracticesWindowsdockerfilemacOS

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion