🐋Table of content
Introduction
- What is an image?
Dockerfile
Writing a Dockerfile:
Understanding the Dockerfile:
Commands
Pushing Image to Docker Hub
🐋Introduction:
Hello, DevOps learners! Today, we’re going to learn about Docker, a tool that helps you package software and create its image. The key to doing this is a file called a Dockerfile. We'll explain what a Dockerfile is, how to create one, and what it does.
✅What is an image?
A Docker image is like a snapshot of an app that contains everything needed to run it. This includes the code, libraries, tools, and settings.
Docker images are made from Dockerfiles, which are instructions to create the image step by step. Think of an image as a recipe, and when you run the image, it becomes a container. A container is like a mini-computer running your app in its own isolated space.
✅Dockerfile:
A Dockerfile is a simple text file with instructions on how to build a Docker image. It tells Docker what to include and how to set up the environment.
✅Writing a Dockerfile:
A Dockerfile is a text file that contains a series of instructions for building a Docker image. Each instruction in the Dockerfile adds a new layer to the image, allowing you to specify the environment, dependencies, and commands needed to run your application.
Here's an example of a simple Dockerfile for a Python application:
#Use the official Python base image
FROM python:3.9-slim
# 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 pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
✅Understanding the Dockerfile:
FROM
: Starts with a base image, like the basic setup for your app.WORKDIR
: Sets the working directory for subsequent instructions.COPY
: Copies files from the host system into the container.RUN
: Runs commands to set up your app in the container.EXPOSE
: Tells Docker which port the container will use.ENV
: Sets environment variables in the container.CMD
: Specifies the command to run when the container starts.
DOCKERFILE SYNTAX :
FROM <base_image>
WORKDIR /app
COPY . .
RUN [command]
CMD ["",""]
DOCKERFILE Example:
In this example we are creating an image of an imaginary python app
FROM ubuntu:latest
WORKDIR ./app
COPY . .
RUN apt-get -y update && apt-get install -y python
CMD ["python","app.py"]
✅Commands:
To create a Docker image, use the docker build
command. Here’s how:
Go to the folder with your Dockerfile and other files.
Build the image using this command:
docker build -t myapp:latest .
-t myapp:latest
: Names the imagemyapp
with the taglatest
..
: Means the current directory.
If your Dockerfile has a different name or is in another folder, specify the path like this:
docker build -t myapp:latest -f /path/to/Dockerfile .
✅ Pushing Image to Docker Hub
- Log in to Docker Hub:
docker login
- Tag the image:
docker tag local-image:tag username/repository:tag
local-image:tag
: The name and tag of your local image.username/repository:tag
: Your Docker Hub username and repository name.
- Push the image to Docker Hub:
docker push username/repository:tag
This uploads your Docker image to Docker Hub, where others can access it.
Happy Learning!😊