Backlinks
- How to kill the process currently using a port on localhost in windows
- đ§ WSL + Fish Shell Setup Notes
- Golang Setup on Linux
Docker is an incredibly powerful tool that has revolutionized how developers build, ship, and run applications. If youâve ever heard phrases like âit works on my machine!â or struggled with environment setup, Docker is here to save the day.
This guide will break down Docker into simple, understandable concepts and get you started with your first containers.
What is Docker? (The Simple Analogy)
Imagine youâre shipping a fragile, complex product â say, a custom-built computer. If you just throw it in a box, it might break or not work when it arrives because of how it was packed or the environment it lands in.
Now, imagine using a standardized shipping container. You put your computer inside, along with everything it needs to run (power supply, specific cables, instructions). This container is sealed and ensures that no matter where it goes â by truck, train, or ship â it arrives in the same condition and works exactly as expected when unpacked.
Docker is like that standardized shipping container for your software.
- Your application and all its dependencies (libraries, configuration, specific OS settings) are bundled together into a single, isolated âcontainer.â
- This container runs consistently on any machine that has Docker installed, regardless of the underlying operating system.
Why Use Docker? (The Benefits)
- âIt works on my machine!â Solved: Docker ensures your application runs the same way everywhere â development, testing, and production. No more environmental inconsistencies.
- Isolation: Each application runs in its own isolated container, meaning they wonât interfere with each other or with your host system.
- Portability: Docker containers can be easily moved and run on any machine with Docker installed (Linux, Windows, macOS).
- Efficiency: Containers are lightweight and start up quickly, using fewer resources than traditional virtual machines.
- Faster Development Cycles: Developers can quickly set up consistent development environments and easily share them.
- Scalability: Docker makes it easier to scale your applications by running multiple instances of containers.
Core Docker Concepts for Beginners
Before we dive into commands, letâs understand the essential building blocks:
-
Images
- Think of it as: A blueprint or a recipe.
- An image is a read-only template that contains a set of instructions for creating a container. It includes the application, libraries, dependencies, and configuration needed to run the application.
- You build images, and then you run containers from them.
- Examples: An image for a Python application, an image for a MySQL database, an image for a web server like Nginx.
-
Containers
- Think of it as: A running instance of an image.
- A container is a live, executable instance created from an image. When you run an image, it becomes a container.
- You can start, stop, move, and delete containers.
- A container is isolated from other containers and from the host system.
-
Dockerfile
- Think of it as: The instruction manual for building an image.
- A simple text file that contains a sequence of commands Docker uses to build an image. It defines exactly what goes into your image and how it should be configured.
-
Docker Hub
- Think of it as: A public registry or library for Docker Images.
- Like GitHub for code, Docker Hub is where you can find and share Docker images. You can pull (download) existing images (e.g., official images for Ubuntu, Node.js, MySQL) or push (upload) your own custom images.
Getting Started: Installation
The easiest way to get Docker running on your personal computer (Windows or macOS) is by installing Docker Desktop. For Linux, you typically install the Docker Engine directly.
-
Download Docker Desktop:
- Go to the official Docker website: https://www.docker.com/products/docker-desktop/
- Download the appropriate installer for your operating system (Windows, Mac with Intel chip, or Mac with Apple chip).
- Follow the installation instructions. You might need to enable virtualization (Hyper-V on Windows, Virtualization in BIOS/UEFI) or grant permissions.
-
Verify Installation:
- Open your terminal or command prompt.
- Type:
docker --version- You should see the Docker version printed.
- Type:
docker run hello-world- This command is a great way to verify that Docker is working correctly. It will:
- Check if the
hello-worldimage exists locally. - If not, pull (download) it from Docker Hub.
- Create and run a new container from that image.
- The container will print a âHello from Docker!â message and then exit.
- Check if the
- This command is a great way to verify that Docker is working correctly. It will:
Your First Docker Commands
Letâs get hands-on with some fundamental Docker commands.
-
docker run- Run a containerThis is the most fundamental command. It creates and starts a new container from an image.
docker run ubuntu- This will download the
ubuntuimage (if not already present) and then run a container from it. - Wait, nothing happened? The
ubuntuimage is just a base operating system. It runs, does nothing, and then exits. To interact with it, we need to add a flag:
docker run -it ubuntu bash-i: (interactive) Keeps the standard input open.-t: (tty) Allocates a pseudo-TTY, giving you a shell.ubuntu: The image to run.bash: The command to run inside the container (opens a bash shell).- You are now inside the Ubuntu container! Try commands like
ls,pwd,apt update. - Type
exitto leave the container. The container will then stop.
- This will download the
-
docker ps- List containersShows you the currently running containers.
docker ps- Youâll likely see nothing, as our
ubuntucontainer exited.
To see all containers (running and stopped):
docker ps -a- You should see your
hello-worldandubuntucontainers listed, showing their status (Exited).
- Youâll likely see nothing, as our
-
docker stop- Stop a running containerYou need the container ID or name (from
docker ps).docker stop <container_id_or_name> -
docker rm- Remove a containerRemoves a stopped container.
docker rm <container_id_or_name>- To remove all stopped containers:
docker container prune
- To remove all stopped containers:
-
docker images- List imagesShows all images downloaded to your local machine.
docker images -
docker rmi- Remove an imageRemoves an image. You canât remove an image if there are containers (even stopped ones) based on it. Remove containers first.
docker rmi <image_id_or_name>- To remove all unused images:
docker image prune -a
- To remove all unused images:
-
docker pull- Download an imageDownloads an image from Docker Hub without running it.
docker pull alpinealpineis a very small Linux distribution, great for testing.
Dockerizing a Simple Application (Python Flask Example)
Letâs build a simple web application and put it into a Docker container.
Goal: Create a Flask (Python web framework) âHello Worldâ application and run it in a container.
Steps:
-
Create Project Files: Create a new directory (e.g.,
my-flask-app). Inside it, create two files:-
app.py(Our Flask application)from flask import Flask import os app = Flask(__name__) @app.route('/') def hello(): return "Hello from Flask inside Docker! (Running on port {})".format(os.environ.get("FLASK_PORT", "5000")) if __name__ == '__main__': # Get port from environment variable or default to 5000 port = int(os.environ.get("FLASK_PORT", 5000)) app.run(debug=True, host='0.0.0.0', port=port) -
requirements.txt(Python dependencies)Flask -
Dockerfile(Instructions for building our image)# Use an official Python runtime as a parent image FROM python:3.9-slim-buster # Set the working directory in the container to /app 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 5000 available to the world outside this container EXPOSE 5000 # Define environment variable ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 ENV FLASK_PORT=5000 # Run app.py when the container launches CMD ["flask", "run"]
-
-
Build the Docker Image: Open your terminal, navigate to the
my-flask-appdirectory (where yourDockerfileis), and run:docker build -t my-flask-app .docker build: The command to build an image.-t my-flask-app: Tags the image with the namemy-flask-app. This makes it easy to reference later..: Tells Docker to look for theDockerfilein the current directory.
Youâll see a lot of output as Docker executes each step in your
Dockerfile. If successful, it will end with a message like âSuccessfully tagged my-flask-app:latestâ. -
Run the Docker Container:
docker run -p 5000:5000 my-flask-appdocker run: Start a new container.-p 5000:5000: This is crucial! It maps port 5000 on your host machine to port 5000 inside the container. Without this, you wouldnât be able to access the web app from your browser.my-flask-app: The name of the image we want to run.
-
Verify the Application: Open your web browser and go to
http://localhost:5000. You should see âHello from Flask inside Docker!âTo stop the container, go back to your terminal where itâs running and press
Ctrl+C. -
Run in Detached Mode (Background): If you want the container to run in the background without tying up your terminal, use the
-d(detached) flag:docker run -d -p 5000:5000 my-flask-app- Now you can close your terminal, and the app will keep running.
- To stop it, first find its ID:
docker ps. Then:docker stop <container_id>.
Beyond the Basics (Whatâs Next?)
This guide just scratches the surface. As you get more comfortable, explore these concepts:
- Volumes: For persistent data storage (e.g., database files). Containers are ephemeral; volumes keep your data safe.
- Networks: To allow containers to communicate with each other securely.
- Docker Compose: For defining and running multi-container Docker applications (e.g., a web app + a database + a caching layer).
- Docker Hub: Learn how to push your own images to Docker Hub to share them.
- Docker Swarm / Kubernetes: For orchestrating and managing large-scale container deployments.
Tips for Beginners
- Start Small: Donât try to containerize your entire complex application on day one. Start with a simple âHello Worldâ or a single service.
- Use Official Images: When building your
Dockerfile, start with official base images from Docker Hub (e.g.,python:3.9-slim-buster,node:16-alpine,nginx). They are well-maintained and secure. - Read the Docs: The official Docker documentation is excellent and comprehensive.
- Clean Up: Docker can consume disk space. Regularly prune stopped containers, unused images, and volumes:
docker system prune(cleans up a lot, but be careful!)
docker logs <container_id>: Use this command to view the output and errors from a running container.docker exec -it <container_id> bash: If a container is running in the background and you want to inspect whatâs happening inside, this command lets you open a shell within it.
Docker might seem intimidating at first, but with practice, it quickly becomes an indispensable tool. Keep experimenting, and youâll soon appreciate its power and elegance! Happy containerizing!