Home Explore Blog CI



docker

2nd chunk of `content/guides/language-translation.md`
aa198671708cf608b9226758c286e8fc741b56792f6b91ad0000000100000fda
         if input_text.lower() == 'exit':
            print("Exiting...")
            break
   ```

   An infinite loop is established here to continuously prompt you for text
   input, ensuring interactivity. The loop breaks when you type `exit`, allowing
   you to control the application flow effectively.

4. Create an instance of Translator.

   ```python
         translator = Translator()
   ```

   This creates an instance of the Translator class, which
   performs the translation.

5. Translate text.

   ```python
         translated_text = translator.translate(input_text, dest='fr').text
   ```

   Here, the `translator.translate` method is called with the user input. The
   `dest='fr'` argument specifies that the destination language for translation
   is French. The `.text` attribute gets the translated string. For more details
   about the available language codes, see the
   [Googletrans docs](https://py-googletrans.readthedocs.io/en/latest/).

6. Print the original and translated text.

   ```python
         print(f"Original Text: {input_text}")
         print(f"Translated Text: {translated_text}")
   ```

   These two lines print the original text entered by the user and the
   translated text.

7. Create `requirements.txt`. The sample application already contains the
   `requirements.txt` file to specify the necessary modules that the
   application imports. Open `requirements.txt` in a code or text editor to
   explore its contents.

   ```text
   ...

   # 05 language_translation
   googletrans==4.0.0-rc1
   ```

   Only `googletrans` is required for the language translation application.

## Explore the application environment

You'll use Docker to run the application in a container. Docker lets you
containerize the application, providing a consistent and isolated environment
for running it. This means the application will operate as intended within its
Docker container, regardless of the underlying system differences.

To run the application in a container, a Dockerfile is required. A Dockerfile is
a text document that contains all the commands you would call on the command
line to assemble an image. An image is a read-only template with instructions
for creating a Docker container.

The sample application already contains a `Dockerfile`. Open the `Dockerfile` in a code or text editor to explore its contents.

The following steps explain each part of the `Dockerfile`. For more details, see the [Dockerfile reference](/reference/dockerfile/).

1. Specify the base image.

   ```dockerfile
   FROM python:3.8-slim
   ```

   This command sets the foundation for the build. `python:3.8-slim` is a
   lightweight version of the Python 3.8 image, optimized for size and speed.
   Using this slim image reduces the overall size of your Docker image, leading
   to quicker downloads and less surface area for security vulnerabilities. This
   is particularly useful for a Python-based application where you might not
   need the full standard Python image.

2. Set the working directory.

   ```dockerfile
   WORKDIR /app
   ```

   `WORKDIR` sets the current working directory within the Docker image. By
   setting it to `/app`, you ensure that all subsequent commands in the
   Dockerfile
   (like `COPY` and `RUN`) are executed in this directory. This also helps in
   organizing your Docker image, as all application-related files are contained
   in a specific directory.

3. Copy the requirements file into the image.

   ```dockerfile
   COPY requirements.txt /app
   ```

   The `COPY` command transfers the `requirements.txt` file from
   your local machine into the Docker image. This file lists all Python
   dependencies required by the application. Copying it into the container
   lets the next command (`RUN pip install`) install these dependencies
   inside the image environment.

4. Install the Python dependencies in the image.

   ```dockerfile
   RUN pip install --no-cache-dir -r requirements.txt
   ```

   This line uses `pip`, Python's package installer, to install the packages

Title: Application Code, Requirements, and Docker Environment Configuration
Summary
This section details the process of printing translated text in the Python application, including the original and translated versions. It explains the 'requirements.txt' file, which specifies the application's dependencies. Furthermore, it explores the application environment using Docker, detailing the Dockerfile's contents and commands, such as setting the base image, working directory, copying the requirements file, and installing Python dependencies within the Docker image.