Welcome to the official Docker project template for the Coding United Club! I am John Swindell, a fellow Club member, and will be walking you through this template guide!
- Overview
- What is Docker?
- Prerequisites
- Key Files in This Template
- Step 1: Get the Template
- Step 2: Explore the Sample Application
- Step 3: Build the Docker Image
- Step 4: Run the Docker Container
- Step 5: Accessing the Application
- Customizing for Your Own Project
- Common Docker Commands
- Getting Help
- License
Before we get started, the goal of this template provides a starting point for containerizing applications, especially Python applications, specifically using Docker.
Don't worry, I know it's scary, but I've got your back and I'll walk you through it.
Let's go!
This template helps you:
- Understand the basics of Docker and
Dockerfilestructure. - Containerize a simple Python web application.
- Learn how to build Docker images and run containers.
- Provide a consistent environment for your applications.
- Last, but certainly not least, will prepare you to work with us on our upcoming club website project!
Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. You can think of this continer as a lightweight, little portable box, and inside is everything your application needs to run properly. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. This makes it easy to create, deploy, and run applications in different environments consistently.
Some cool benefits:
- Consistency: The classic: "Well, it works on my computer" is no longer an issue!
- Portability: Run your container anywhere Docker is installed.
- Isolation: Dependencies for one app don't conflict with others.
- Scalability: Easier to scale applications.
Before you begin, ensure you have the following installed:
- Docker Desktop (or Docker Engine on Linux): Download from docker.com.
- Git: Download from git-scm.com (if you plan to clone).
- A Terminal / Command Prompt for running Docker commands.
Dockerfile: This is the blueprint for building your Docker image. It contains a set of instructions Docker uses to assemble the image..dockerignore: Similar to.gitignore, this file lists files and directories that should be excluded when building the Docker image (i.e., not copied into the image).app/directory: Contains the sample Python application to be containerized.app/app.py: A very simple Python HTTP server.app/requirements.txt: (Optional) For listing Python dependencies. Our current sampleapp.pyhas no external dependencies.
README.md: This file!
- Clone this repository:
(Of course, make sure that
git clone [https://github.com/codindUnited/docker-template.git](https://github.com/codingUnited/docker-template.git) your-docker-project cd your-docker-projectcodingUnitedis the actual GitHub organization name. If for some reason it's changed, this won't work. Ah, andyour-docker-projectwith your desired folder name 👍).
The app/app.py file contains a very basic Python HTTP server. It's designed to run inside the Docker container and serve a simple HTML page. It uses environment variables defined in the Dockerfile (APP_NAME, GREETING_MESSAGE) and listens on port 8000.
The docker build command builds an image from a Dockerfile.
-
Navigate to the root directory of this template (where the
Dockerfileis). -
Run the following command:
docker build -t coding-united-app .-t coding-united-app: This tags (names) your imagecoding-united-app. You can choose any name:tag combination..: This specifies that the build context (the set of files to send to the Docker daemon) is the current directory.
You should see Docker stepping through the instructions in your
Dockerfile.
Once the image is built, you can run it as a container using docker run.
docker run -d -p 8080:8000 --name my-python-app coding-united-app
* `docker run`: The command to run a container.
* `-d`: Runs the container in "detached" mode (in the background).
* `-p 8080:8000`: This **publishes** a port. It maps port `8080` on your host machine to port `8000` inside the container (where our Python app is listening, as defined by `EXPOSE 8000` in the Dockerfile and the `app.py` script). You can change the host port (e.g., `-p 3000:8000`).
* `--name my-python-app`: Gives your running container a memorable name.
* `coding-united-app`: The name of the image you want to run.Open your web browser and go to: http://localhost:8080
You should see the greeting message from the Python application running inside the Docker container! If you used a different host port in the docker run command (e.g., -p 3000:8000), then access it via http://localhost:3000.
To use this template for your own Python (or other type) application:
-
Replace
app/contents:- Delete the sample
app/app.pyandapp/requirements.txt. - Place your own application files into the
app/directory. For example, if you have a Flask or Django project, its main files would go here.
- Delete the sample
-
Update
Dockerfile:- Base Image: If your application isn't Python, or requires a specific Python version, change the
FROMinstruction (e.g.,FROM node:18-slimfor a Node.js app). - Dependencies:
- If your Python app needs packages, list them in
app/requirements.txt. Then, ensure these lines are uncommented and correct in yourDockerfile:COPY ./app/requirements.txt /app/requirements.txt RUN pip install --no-cache-dir -r requirements.txt
- For other languages, use their respective package managers (e.g.,
npm installfor Node.js,bundle installfor Ruby).
- If your Python app needs packages, list them in
EXPOSEPort: Change theEXPOSEinstruction if your application inside the container listens on a port other than8000.CMDInstruction: Modify theCMDinstruction to correctly start your specific application. For example, for a Flask app typically run withflask run --host=0.0.0.0 --port=5000, yourCMDmight beCMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]. Ensure the port in theCMDmatches yourEXPOSEport.
- Base Image: If your application isn't Python, or requires a specific Python version, change the
-
Rebuild your image (
docker build -t your-new-app-name .) and run the new container (docker run -p <host_port>:<container_port> your-new-app-name).
Here's a quick reference for some frequently used Docker commands:
- List Images:
docker images
- List Running Containers:
docker ps
- List All Containers (running or stopped):
docker ps -a
- Stop a Running Container:
(e.g.,
docker stop <container_name_or_id>
docker stop my-python-app) - Remove a Stopped Container:
docker rm <container_name_or_id>
- Remove an Image:
- First, make sure no containers are using the image (stop and remove them).
docker rmi <image_name_or_id>
- View Logs of a Container:
(For detached containers, this is how you see their output.)
docker logs <container_name_or_id>
- Execute a Command in a Running Container (e.g., get a shell):
(This gives you an interactive terminal session inside the container, if
docker exec -it <container_name_or_id> bash
bashis available in the image. For minimal images likealpine, usesh.)
If you have questions, run into issues, or want to learn more:
- Ask your fellow Coding United Club members!
- Refer to the official Docker documentation: Docker Docs
- Explore Docker Hub for pre-built images: Docker Hub
- Feel free to @ me or ask in our: Discord
- Find more information on our: Club Site
This project is licensed under the MIT License - see the LICENSE file for details.