top of page

Lab 7 - Containerization with Docker - Day 80

Writer's picture: vPvP

Welcome back to the #90DaysOfDevOps series! Today, we'll be discussing about containerization with Docker. If you've been following along, you've already gained valuable insights into various DevOps practices. Now, let's roll up our sleeves and explore the power of Docker.


Step 1: Install Docker on Your Machine

Before we proceed further, we need to install it on our machine. Docker provides a seamless experience across different operating systems. Head over to the official Docker website and follow the installation instructions specific to your operating system. Once installed, you're ready to start containerizing!


Step 2: Create a Dockerfile for a Simple Web Application

Imagine you have a basic web application, and you want to containerize it. The first step is to create a Dockerfile that defines the configuration for your Docker image. Let's create a simple example for a Python web application using Flask.

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory 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 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Here, we are using the official Python image, copying our application files into the container, installing dependencies from requirements.txt, exposing port 80, setting an environment variable, and finally specifying the command to run our application.


Step 3: Build a Docker Image and Run Containers from It

Now that we have our Dockerfile, let's build our Docker image. Open your terminal, navigate to the directory containing your Dockerfile, and run the following command:

docker build -t mywebapp .

This command instructs Docker to build an image tagged as mywebapp using the Dockerfile in the current directory (.).


With the image built, it's time to run a container:

docker run -p 4000:80 mywebapp

This command runs a container based on our mywebapp image, mapping port 4000 on your host machine to port 80 in the container.


Visit http://localhost:4000 in your web browser, and you should see your web application running inside a Docker container!


Step 4: Push the Docker Image to Docker Hub

Now, let's share our containerized application with the world by pushing it to Docker Hub. Ensure you have a Docker Hub account and log in via the terminal:

docker login

Tag your image with your Docker Hub username and repository name:

docker tag mywebapp yourusername/mywebapp

Finally, push the image to Docker Hub:

docker push yourusername/mywebapp

Voila! Your Docker image is now available on Docker Hub for others to use and build upon.


In just a few steps, you've experienced the power of Docker, from creating a Dockerfile to running containers and sharing your application with the global developer community. Containerization with Docker opens up a world of possibilities for efficient and consistent deployment of applications.


Stay tuned for more exciting DevOps adventures in the remaining days of #90DaysOfDevOps!


Thank you for reading!


*** Explore | Share | Grow ***

5 views0 comments

Opmerkingen

Beoordeeld met 0 uit 5 sterren.
Nog geen beoordelingen

Voeg een beoordeling toe
vp1.2_23.png

vPundit

Explore | Share | Grow

Thanks for submitting!

vPundit

bottom of page