Docker Commands: What Are They For and How to Use Them?

Docker is a platform used to distribute and run software quickly and consistently. It isolates dependencies and infrastructure by running applications in containers, allowing software to run smoothly in different environments. When using Docker, we create images using a file called Dockerfile. Here are the basic commands for creating a Docker image using Dockerfile:

FROM: This is the section where the base image is created. It specifies the image that Docker should base on. For example, the command FROM python:3.9 uses the Python 3.9 image from Docker Hub as the base for building the image.

WORKDIR: Used to switch to a selected directory. This command is like a combination of mkdir and cd. If the specified directory doesn't exist, it will be created. For example, WORKDIR /app switches to the /app directory.

COPY: Used to copy files into the image. For example, the command COPY requirements.txt . copies the requirements.txt file into the image.

RUN: Specifies the command to run while building the image. Here, we specify the command to be executed within the image. For example, with RUN pip install -r requirements.txt, we install Python packages inside the image.

COPY . . : Used to copy all project files from the directory where the Dockerfile is located into the image. It includes all files in the image.

EXPOSE: Specifies the port on which the image will listen for connections. When the container runs, it will listen on the specified port number.

CMD: Specifies the command to run when the container starts. For example, with CMD ["python", "app.py"], we run the app.py file.

ENV: Defines environment variables that can be used within the image.

ADD: Used to download files and URLs and copy them into the Docker image.

ENTRYPOINT: Specifies the command to run when the container starts.

HEALTHCHECK: Checks the status of the Docker image and determines the container’s health status.

MAINTAINER: Contains information about the image owner.

ARG: Used to create arguments (variables).

VOLUME: Allows connecting external volumes to the container. Used for data storage and sharing.

USER: Specifies the identity of the user running inside the container.

LABEL: Adds metadata labels to the image. These can include information such as image version, creator, description, etc.

SHELL: Specifies the shell environment to be used in the Dockerfile.

STOPSIGNAL: Triggers the container to stop running.

ONBUILD: Ensures that a command underneath the Dockerfile will be executed when an image is derived in the future.

app.py:

from flask import Flask, request 
app = Flask(__name__)
@app.route('/')
def myapi():
portname = request.environ.get('portname')
return f"first docker project and opened this port+ {portname}"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)

Dockerfile:

# Base image: Python 3.9
FROM python:3.9
# Set working directory to /app
WORKDIR /app
# Copy required files to the image
COPY requirements.txt .
# Install dependencies - requirements
RUN pip install -r requirements.txt
# Copy project files to the image
COPY . .
# Expose port 8080
EXPOSE 8080
# Run the application
CMD ["python", "app.py"]

This Dockerfile is used to turn your Flask application into a Docker image. To build the image, place this Dockerfile into a folder, then navigate to that folder in your terminal or command prompt and run the following command:

docker build -t flask-app .

This command builds a Docker image named flask-app using the Dockerfile. To run this image as a container, you can use the following command:

docker run -p 8080:8080 flask-app

This command runs your Flask application on port 8080 and forwards that port to the host machine. You can now access this application from a browser or any other HTTP client at http://localhost:8080/.

--

--