Day 16 Task : 16/90 Days
Pranav Lahitkar
Master of Computer Applications ??25' RHCSA | NETWORKING | AWS | DevOps | JAVA | DBMS
#Basics of Docker
Task : 1)Write about Docker and post on linkedin
2) Create basic docker file in vscode using your knowledge which gained while learning about docker and also write commands to run docker file
Docker is a platform that enables developers to package, distribute, and run applications in containers. Containers are lightweight, portable, and self-sufficient environments that encapsulate everything needed to run an application, including the code, runtime, libraries, and dependencies.
Here's a basic rundown on Docker and how to write a simple Dockerfile:
1. Install Docker: Before getting started,make sure Docker is installed on your PC. Refer to Docker Docs
2. Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. Each instruction in the Dockerfile adds a layer to the image
Dockerfile
# Use an existing Docker image as a base
FROM alpine:latest
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed dependencies specified in requirements.txt
RUN apk add --no-cache python3 && \
pip3 install --upgrade pip && \
pip3 install -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
领英推荐
ENV NAME World
# Command to run the application
CMD ["python3", "app.py"]
Lets understand what i have written :
- FROM: Specifies the base image to use for the Docker image. In this case, we're using the latest Alpine Linux image.
- WORKDIR: Sets the working directory inside the container.
- COPY: Copies files from the host machine into the container.
- RUN: Executes commands inside the container during the build process. In this case, we're installing Python and any dependencies listed in requirements.txt.
- EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
- ENV: Sets environment variables inside the container.
- CMD: Provides the default command to execute when the container starts.
3. Building Docker Image: Once you have created the Dockerfile, navigate to the directory containing the Dockerfile in your terminal and run the following command to build the Docker image =>
docker build -t myimage .
This command builds a Docker image with the tag myimage using the Dockerfile in the current directory (`.`).
4. Running a Docker Container: After building the image, you can run a container using the following command =>
docker run -p 4000:80 myimage
This command runs a container based on the myimage Docker image, mapping port 4000 on the host to port 80 in the container.