Docker Basics and Application Deployment Guide¶
1.Overview¶
Note
This guide assumes a basic understanding of Docker and assumes you have Docker Desktop installed on your Windows machine.
Abstract
Docker is a containerization platform that enables developers to package applications and their dependencies into isolated environments called containers. These containers are lightweight, portable, and consistent across development, staging, and production environments.
Docker Desktop Link: https://www.docker.com/products/docker-desktop
Figure: Comparison between Docker Containers and Virtual Machines
This document provides a comprehensive guide to Docker fundamentals and walks through the process of deploying a Python-based Dash data visualization application using Docker on a local Windows environment.
2.Core Concepts¶
- Image: A static snapshot that contains all the files, environment variables, and configuration needed to run an application.
- Container: A running instance of a Docker image.
- Dockerfile: A script containing a set of instructions to assemble a Docker image.
- Volume: A mechanism for persistent data storage used by containers.
- Docker Hub: The default public registry for sharing Docker images.
3.Essential Docker Commands¶
System Info and Help¶
Image Management¶
Container Lifecycle¶
Tip
Always stop containers before removing them to avoid data loss.
Note
docker run can be used solo without docker pull . If the image is not found locally, Docker will attempt to pull it from Docker Hub.
Port Mapping¶
When you run a container, by default it is isolated from your host machine. If an application inside the container listens on a port (for example, a web app on port 8000), you need to map that port to your host machine so you can access it.
Tip
Port mapping allows you to access services running inside a container from your host machine. The format is <host_port>:<container_port>.
The general syntax when starting a container is:
Mount a volume¶
The -v (or --volume) flag in Docker is used to mount a volume — basically, to connect a directory or file from your host machine into a running container.
This allows your container to read/write data to your host filesystem, so data is not lost when the container stops.
Tip
The general syntax when using the -v flag is:
<host_path>: The path to the directory or file on your host machine.
- <container_path>: The path where the data should be mounted inside the container.
- <image_name>: The name of the Docker image to use.
Example 1: Mounting a directory
If you have code in /home/user/app on your machine and want it available inside the container at /app:/home/user/app- Container directory:/app- Any changes inside/app` in the container will reflect on your host, and vice versa.
Example 2: Mounting a single file
Example 3: Named volumes Instead of using a full path, you can let Docker manage storage using a named volume:
-mydata is the name of the volume.
- /app is the directory inside the container where the volume will be mounted.
- docker inspect mydata will show the details of the volume.
- docker volume rm mydata will remove the volume.
- docker volume list will list all volumes.
Utilities¶
-d run container in detached mode
- --restart=always restart container if it stops
- --name my-container name the container
- myimage the image to use
- --restart unless-stopped restart container unless it is stopped
4.Dockerfile¶
Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Tip
Dockerfile is used to create a Docker image. Dockerfile is a text file that contains a list of instructions that Docker uses to build an image. Dockerfile is a recipe for Docker to build an image.
Basic argument of Dockerfile:
- FROM specifies the base image to use.
- WORKDIR sets the working directory for the container.
- COPY copies files from the host to the image.
- RUN executes a command in the image.
- CMD specifies the default command to run when the container starts.
5.Example: Deploying a Dash App Using Docker¶
Example
This section demonstrates how to containerize and run a Dash data visualization application.
Directory Structure¶
app.py¶
requirements.txt¶
Dockerfile¶
6.Build and Run Workflow¶
Step 1: Build the Docker Image¶
Step 2: Run the Container¶
--namegives the container a human-readable name-pmaps port 8050 of the container to the host--restart=alwaysensures the container restarts on reboot
Step 3: Verify Application Availability¶
Open a browser and navigate to:
You should see the Dash UI indicating successful deployment.
Always Start the docker container after reboot¶
Tip
This command is used to check a Docker container (restart policy) in the background.
7.Best Practices¶
- Use
.dockerignoreto exclude unnecessary files from your image build context - Pin versions in
requirements.txtto ensure deterministic builds - Use tagged images (e.g.,
myapp:1.0) for version tracking - Clean up unused images and containers using
docker system prune - Use Docker Compose for multi-container applications
8.Summary¶
Docker is a powerful and versatile tool that simplifies application deployment by encapsulating runtime environments. By following the practices outlined in this guide, developers can reliably package and deploy Dash or any other web application across systems with consistent behavior and minimal configuration.