Dockerfile,
A Dockerfile is like a recipe that provides instructions to create a consistent and isolated environment for running your application within a Docker container. It's a text file containing a series of commands that define how to assemble an image, which is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, and settings.
Detailed breakdown of attributes in Dockerfile:
FROM base_image:tag -> The FROM
instruction specifies the base image that your image will be built upon
WORKDIR /app -> Set the working directory inside the container
COPY source destination -> Copy files or directories from host to container
RUN command -> Run a command inside the container
ENV key=value -> Set Environment Variables
EXPOSE port_number -> Expose a port on the container
CMD ["executable", "arg1", "arg2"] -> Define a command to run when the container starts/ready
LABEL key=value -> Optionally, specify metadata about the image (labels, author, etc)
RUN package_manager install package_name -> Install dependencies (Specific to the base image package manager)
USER username -> Specify the user that the container should run as
Task:
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Build the image using the Dockerfile and run the container
Verify that the application is working as expected by accessing it in a web browser
Push the image to a public or private repository (e.g. Docker Hub )
Create a Directory: Create a directory for your Node.js app. For example, let's call it
python-app
.Navigate to the Directory: Open a terminal and navigate to the
python-app
directory.Create the Python Code: Inside the
python-app
directory, create a file namedapp.py
and add the following content to create a basic Python web server:Create a Dockerfile: In the same directory, create a file named
Dockerfile
and add the following content:Build and Run Docker Container: In the terminal, navigate to the
python-app
directory and run the following commands:Build the image:
Create a container:
This example demonstrates a simple Python web server running inside a Docker container. The
-p 8080:8080
flag maps port 8080 on your host machine to port 8080 inside the Docker container. You can access the app by navigating tohttp://ec2-public-ip-address:8080
in your web browser.
Push the Docker image to a public or private repository (e.g., Docker Hub):
- Push to Docker Hub
To share your Docker image with others, you can push it to Docker Hub. Make sure you're logged in to Docker Hub, using docker login
and then push the image:
Once you have logged in, tag the image with the repository’s name and push it using the following commands.