Docker (Guide to Containerization): Part 2

Docker (Guide to Containerization): Part 2

Inputs

In Docker, the -i parameter is used to attach the standard input (stdin) of the host to a running container. This allows you to provide input to a process running inside the container as if you were interacting with it directly.

To use the -i parameter, you simply include it in the docker run command when starting a container. For example:

docker run -i ubuntu /bin/bash

This command starts a new container based on the ubuntu image and runs the /bin/bash command inside the container while attaching the stdin of the host to the container.

Once the container is running, you can provide input to it by typing into the terminal on the host. The input will be sent to the stdin of the container, allowing you to interact with the process running inside the container.

Here are a few examples of use cases for mapping the standard input of your host to a Docker container using the -i parameter:

Running interactive command-line applications

If you need to run a command-line application that requires input from the user, such as a text editor or a database client, you can use the -i parameter to provide input to the application running inside the container.

For example, to run the vi text editor inside a container and provide input to it, you can use the following command:

docker run -i ubuntu vi

This will start a new container based on the ubuntu image and run the vi text editor inside the container, while attaching the stdin of the host to the container. You can then type into the terminal on the host to interact with the vi editor running inside the container.

Providing input to scripts running inside a container

If you need to run a script inside a container that requires input from the user, you can use the -i parameter to provide input to the script.

For example, to run a script called myscript.sh that prompts the user for input inside a container, you can use the following command:

docker run -i myimage ./myscript.sh

This will start a new container based on the myimage image and run the myscript.sh script inside the container, while attaching the stdin of the host to the container. You can then provide input to the script by typing into the terminal on the host.

Debugging applications running inside a container

If you need to debug an application running inside a container and want to interact with it as if you were running it directly on the host, you can use the -i parameter to attach the stdin of the host to the container.

For example, to debug an application called myapp running inside a container and interact with it using a debugger running on the host, you can use the following command:

docker run -i myimage myapp --debug

This will start a new container based on the myimage image and run the myapp application inside the container, while attaching the stdin of the host to the container. You can then use a debugger running on the host to interact with the application and debug any issues.

Docker Image

Docker images are the fundamental building blocks of containers. They are the blueprints for Docker containers, which are lightweight, portable, and can run on any platform with Docker installed. Docker images consist of layers of files, libraries, and dependencies that are stacked on top of each other, forming a cohesive unit that can be deployed as a container.

Creating Your Own Docker Image

Creating your own Docker image is a straightforward process that involves understanding what you want to containerize, creating a Dockerfile that outlines the necessary steps, and building the image using the Docker build command. Here are the steps involved in creating a Docker image:

Identify the Application:

The first step in creating a Docker image is to identify the application that you want to containerize. This could be a web server, a database, or any other type of application.

Understand the Application:

Once you have identified the application, it is essential to understand how the application is built and what dependencies it requires. This will help you determine the steps required to create a Docker image.

Write a Dockerfile:

A Dockerfile is a text file that contains a set of instructions for Docker to build an image. The Dockerfile specifies the base image, the steps required to install dependencies and configure the application, and any other customizations required.

For example, suppose you want to create a Docker image in a directory called 'my-python-app' for a simple Python application. In that case, the Dockerfile within the 'my-simple-app' directory would look something like this:

# Specify the base image

FROM python:3.9-slim-buster


# Set the working directory

WORKDIR /app


# Copy the requirements file

COPY requirements.txt .


# Install the dependencies

RUN pip install -r requirements.txt


# Copy the application code

COPY app.py .


# Set the command to run the application

CMD ["python", "app.py"]

This Dockerfile specifies the base image as Python 3.9, sets the working directory to /app, installs the dependencies from the requirements.txt file, copies the app.py file, and sets the command to run the application.

Build the Docker Image:

Once you have written the Dockerfile, the next step is to build the Docker image using the Docker build command. This command takes the Dockerfile as input and creates a Docker image with a specified tag name.

docker build -t my-python-app .

This command builds the Docker image using the Dockerfile in the current directory and tags it with the name my-python-app.

Run the Docker Container:

Once you have built the Docker image, you can run it as a Docker container using the Docker run command.

docker run -p 5000:5000 my-python-app

This command runs the Docker container and maps port 5000 on the host machine to port 5000 in the container, allowing you to access the application at http://localhost:5000.

Environmental Variables

Environmental variables are an essential part of Docker that enable users to define and customize various aspects of the container's behaviour. These variables are set at runtime and can be used to configure the container's behaviour or pass configuration information to applications running within the container.

Here are some of the major environmental variables in Docker and their use cases:

HOME:

This variable sets the user's home directory in the container.

Example:

docker run -e HOME=/app my-image

PATH:

This variable sets the container's search path for executables.

Example:

docker run -e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin my-image

LANG:

This variable sets the container's default language.

Example:

docker run -e LANG=en_US.UTF-8 my-image

TZ:

This variable sets the container's timezone.

Example:

docker run -e TZ=America/Los_Angeles my-image

TERM:

This variable sets the terminal type in the container.

Example:

docker run -e TERM=xterm my-image

HTTP_PROXY and HTTPS_PROXY:

These variables set the container's HTTP and HTTPS proxy settings.

Example:

docker run -e HTTP_PROXY=http://proxy.example.com:8080 -e HTTPS_PROXY=http://proxy.example.com:8080 my-image

MYSQL_ROOT_PASSWORD:

This variable sets the MySQL root password.

Example:

docker run -e MYSQL_ROOT_PASSWORD=mysecretpassword my-mysql-image

NODE_ENV:

This variable sets the Node.js environment.

Example:

docker run -e NODE_ENV=production my-node-app

REDIS_URL:

This variable sets the Redis URL.

Example:

docker run -e REDIS_URL=redis://redis.example.com:6379 my-redis-app

It is essential to understand these variables and their use cases to take full advantage of the power of Docker.

Command vs Entrypoint

In Docker, the ENTRYPOINT and CMD instructions are used to define the default command that will be executed when a container is started. Both of these instructions are critical components of the Dockerfile, but they serve different purposes.

CMD Instruction

The CMD instruction is used to specify the default command that should be executed when a container is started. This command can be overridden by passing a different command to the docker run command. If the CMD instruction is not specified in the Dockerfile, the default command will be /bin/sh -c, and any arguments that are passed to the docker run command will be appended to this command.

Example:

FROM ubuntu

CMD ["echo", "Hello, World!"]

In this example, the CMD instruction specifies that the command echo "Hello, World!" should be executed when the container starts. If the Dockerfile is built and run, the output will be:

Hello, World!

ENTRYPOINT Instruction

The ENTRYPOINT instruction is used to specify the command that will always be executed when a container is started, regardless of any arguments that are passed to the docker run command. This is different from the CMD instruction, which can be overridden by passing a different command to the docker run.

Example:

FROM ubuntu

ENTRYPOINT ["echo", "Hello,"]

CMD ["World!"]

In this example, the ENTRYPOINT instruction specifies that the command echo "Hello," should always be executed when the container starts, regardless of any arguments passed to the docker run command. The CMD instruction specifies that the argument World! should be appended to the ENTRYPOINT command.

If the Dockerfile is built and run without any additional arguments, the output will be:

Hello, World!

However, if an argument is passed to the docker run command, such as:

docker run my-image John

The output will be:

Hello, John!

The Difference between CMD and ENTRYPOINT

The main difference between the CMD and ENTRYPOINT instructions is that the ENTRYPOINT instruction specifies a command that cannot be overridden by any arguments passed to the docker run command, while the CMD instruction specifies a default command that can be overridden.

Additionally, the ENTRYPOINT instruction is typically used to specify the main executable for the container, while the CMD instruction is used to provide default arguments to the ENTRYPOINT command.

When to Use CMD and ENTRYPOINT

In general, the CMD instruction should be used to provide default arguments to the ENTRYPOINT command. This allows users to easily override the default behaviour of the container by passing different arguments to the docker run command.

The ENTRYPOINT instruction should be used to specify the main executable for the container. This ensures that the container always executes the same command when it starts, regardless of any arguments that are passed to the docker run command.

We're just scratching the surface on this topic. It's like trying to fit an elephant into a matchbox - it just won't work! So, we'll have to save the rest of the juicy details for our next article. Stay tuned and get ready to feast your mind on some more exciting Docker knowledge!

For more detailed information on Dockerfile visit:๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

https://spacelift.io/blog/dockerfile

ย