Docker Project for DevOps Engineers

Docker Project for DevOps Engineers

Dockerfile

A Dockerfile is a text file that contains a set of instructions for building a Docker image. It serves as a blueprint for creating a reproducible and portable containerized application environment.

In a Dockerfile, you define a series of steps and commands that Docker uses to automatically build an image. Each instruction in the Dockerfile adds a new layer to the image, allowing for incremental builds and efficient caching of layers.

Here's an example of a basic Dockerfile:

# Use a base image
FROM ubuntu:latest

# Set the working directory
WORKDIR /app

# Copy the source code into the container
COPY . /app

# Install dependencies and build the application
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

# Expose a port
EXPOSE 8080

# Set the entrypoint command
CMD ["python3", "app.py"]

Let's go through each section:

  • FROM ubuntu:latest: This sets the base image for your Docker container. In this example, we're using the latest version of Ubuntu as the base.

  • WORKDIR /app: This sets the working directory inside the container where the following commands will be executed.

  • COPY . /app: This copies the contents of the current directory (where the Dockerfile is located) into the /app directory inside the container.

  • RUN apt-get update && apt-get install -y python3 python3-pip: This updates the package lists in the container and installs Python 3 and pip.

  • RUN pip3 install --upgrade pip and RUN pip3 install -r requirements.txt: These commands upgrade pip to the latest version and install the dependencies listed in requirements.txt. You'll need to have a requirements.txt file in the same directory as the Dockerfile, containing the Python packages your application requires.

  • EXPOSE 8080: This exposes port 8080 in the container. Note that this is just a metadata declaration; you'll still need to publish the port when running the container.

  • CMD ["python3", "app.py"]: This specifies the command to run when the container starts. In this example, it runs the app.py file using Python 3.

TASKS

  1. Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

FROM python:3.9
COPY . .
RUN pip install -r requirements.txt
EXPOSE 8001
CMD[" python", "manage.py", "runserver", "0.0.0.0:8001"]
  • FROM python:3.9: Specifies the base image as Python 3.9.

  • COPY . .: Copies the contents of the current directory (where the Dockerfile is located) into the working directory of the container.

  • RUN pip install -r requirements.txt: Installs the dependencies listed in requirements.txt using pip.

  • EXPOSE 8001: Exposes port 8001 in the container. Note that this is just a metadata declaration; you'll still need to publish the port when running the container.

  • CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]: Specifies the command to run when the container starts. This example runs the Django development server (manage.py runserver) on 0.0.0.0:8001, making it accessible externally.

  1. Build the image using the Dockerfile and run the container

To build the Docker image, use the following command:

docker build . -t react_django_demo_app:latest

Let's break down the command:

  • docker build: This is the command to build a Docker image.

  • .: The dot (.) represents the build context. It specifies that the build should be performed using the current directory as the context. The build context includes the Dockerfile and any files or directories referenced in the Dockerfile's instructions.

  • -t react_django_demo_app:latest: This option tags the built image with a specific name and tag. In this case, the name is react_django_demo_app and the tag is latest.

After running this command, Docker will build the image based on the Dockerfile located in the current directory and tag it as react_django_demo_app:latest.

docker run -p 8001:8001 -d react_django_demo_app:latest

Let's break down the command:

  • docker run: This is the command to run a Docker container.

  • -p 8001:8001: This option maps port 8001 of the host to port 8001 of the container. It allows you to access the containerized application via http://localhost:8001.

  • -d: This option runs the container in detached mode, meaning it runs in the background without attaching the container's output to your terminal.

  • react_django_demo_app:latest: This specifies the name and tag of the Docker image to use for running the container. In this case, it uses the image named react_django_demo_app with the latest tag.

  1. Verify that the application is working as expected by accessing it in a web browser

    To modify the inbound rules for the AWS EC2 instance, configure it to allow traffic on port 8001 specifically for your IP address, enabling access to your application through the internet.

  1. Push the image to a public or private repository (e.g. Docker Hub )

    To upload/push your Docker image to a public or private repository like Docker Hub, you can follow these general steps:

    1. Make sure you have an account on the desired repository platform (e.g., Docker Hub).

    2. Log in to the Docker command-line interface (CLI) using your repository credentials. Run the following command and provide your Docker Hub username and password when prompted:

       docker login
      

    3. Tag your local Docker image with the repository information. This includes the repository's URL and the desired image name and tag. The general format is:

       docker tag local_image:tag repository_url/username/image_name:tag
      

      For example:

       docker tag react_django_demo_app:latest prajwalzalaki/dem0_django_app:latest
      
    4. Push the tagged image to the repository using the following command:

       docker push repository_url/username/image_name:tag
      

      For example:

       docker push prajwalzalaki/dem0_django_app:latest
      
    5. Docker will upload the image to the repository. The time taken for the upload depends on the size of the image and your internet connection.

Thanks for reading!