Docker has revolutionized how applications are developed, tested, and deployed. With Docker, developers can package applications and all their dependencies into lightweight, portable containers. These containers can run consistently across different environments—whether on a developer’s laptop, a test server, or a production environment.
In this blog post, we’ll walk you through the steps of building and deploying your first Dockerized application. By the end of this guide, you’ll have a fully functional, containerized app that you can run and deploy anywhere.
What You’ll Need
Before we begin, here are the prerequisites:
• Docker installed on your machine. You can download Docker here.
• Basic knowledge of command-line tools.
• A simple application you want to Dockerize. For this tutorial, we’ll use a simple Python Flask application.
Step 1: Create a Simple Application
For the sake of simplicity, let’s create a basic Python application using Flask, a lightweight web framework for Python.
- Create a directory for your project:
- mkdir dockerized-app
- cd dockerized-app
- Create a Python file (app.py) in this directory with the following content:
- from flask import Flask
- app = Flask(name)
- @app.route(‘/’)
- def hello_world():
- return ‘Hello, Dockerized World!’
- if name == ‘main‘:
- app.run(debug=True, host=’0.0.0.0′)
This simple app creates a web server that listens for requests and returns the message “Hello, Dockerized World!” when accessed. - Create a requirements file (requirements.txt) for the dependencies:
- Flask==2.1.1
Step 2: Create a Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. The Docker image defines the environment in which your application will run. - In the same directory as app.py and requirements.txt, create a file named Dockerfile (without any extension).
- Add the following contents to the Dockerfile:
- # Use an official Python runtime as a parent image
- FROM python:3.9-slim
- # Set the working directory in the container
- 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 the container
- EXPOSE 5000
- # Define environment variable
- ENV NAME DockerizedApp
- # Run app.py when the container launches
- CMD [“python”, “app.py”]
Let’s break down the instructions:
o FROM python:3.9-slim: This sets the base image for the container. In this case, it’s a minimal Python 3.9 image.
o WORKDIR /app: This sets the working directory inside the container to /app.
o COPY . /app: This copies all files from your local project directory into the container’s /app directory.
o RUN pip install -r requirements.txt: This installs the Python dependencies in the container.
o EXPOSE 5000: This exposes port 5000 to make your Flask app accessible.
o CMD [“python”, “app.py”]: This runs the application when the container starts.
Step 3: Build the Docker Image
Now that you have your Dockerfile, it’s time to build the Docker image. - Open a terminal and navigate to the directory containing your Dockerfile, app.py, and requirements.txt.
- Run the following command to build the Docker image:
- docker build -t dockerized-flask-app .
Here’s a breakdown of the command:
o docker build: This tells Docker to build an image.
o -t dockerized-flask-app: This tags the image with the name dockerized-flask-app.
o .: This indicates that the build context is the current directory.
Docker will go through the instructions in the Dockerfile, pull the Python image, install the dependencies, and create a new image. This process may take a few minutes.
Step 4: Run the Docker Container
Once the image is built, you can run the application inside a Docker container. - Run the container with the following command:
- docker run -d -p 5000:5000 dockerized-flask-app
Here’s what this command does:
o docker run: Runs the container from the specified image.
o -d: Runs the container in detached mode (in the background).
o -p 5000:5000: Maps port 5000 on your local machine to port 5000 inside the container, so you can access the app through your browser.
o dockerized-flask-app: This is the name of the image we just built. - To verify that the container is running, use the docker ps command:
- docker ps
This will show a list of running containers. You should see your dockerized-flask-app container running. - Open a web browser and go to http://localhost:5000. You should see the message:
- Hello, Dockerized World!
Congratulations! You’ve successfully Dockerized your first application!
Step 5: Push the Image to Docker Hub (Optional)
If you want to share your Docker image with others or deploy it on other systems, you can push the image to Docker Hub, Docker’s public registry. - First, create an account on Docker Hub, if you don’t have one already.
- Log in to Docker Hub using the following command:
- docker login
- Tag your image with your Docker Hub username (replace yourusername with your Docker Hub username):
- docker tag dockerized-flask-app yourusername/dockerized-flask-app
- Push the image to Docker Hub:
- docker push yourusername/dockerized-flask-app
After the image is uploaded, you’ll be able to access it from anywhere by pulling it from Docker Hub:
docker pull yourusername/dockerized-flask-app
Step 6: Deploying Your Dockerized Application
Once you have Dockerized your application, you can deploy it to any environment that supports Docker, including cloud platforms like AWS, Azure, Google Cloud, or even your own servers.
Some platforms, like Heroku, provide simple deployment options for Dockerized applications. If you’re looking for more robust orchestration and scaling, you can use Kubernetes to manage your containers in production.
Conclusion
Dockerizing your application is a great way to ensure consistency across different environments, streamline deployment, and simplify scaling. In this tutorial, we walked through the process of creating a simple Flask application, writing a Dockerfile, building a Docker image, and running it in a container.
With Docker, you can now take your application and deploy it anywhere—whether locally, in the cloud, or across different systems—without worrying about environment inconsistencies.
As you grow more familiar with Docker, you can start experimenting with more complex use cases, such as working with databases, deploying multi-container applications, and leveraging Docker’s orchestration tools like Docker Compose and Kubernetes. Happy Dockerizing! B`