How to write a Dockerfile: A Beginner's Guide | Part 1
Introduction
Docker has revolutionized the way we develop, deploy, and manage applications. It provides a platform for packaging, distributing, and running applications within isolated containers. In this blog, we'll explore Docker and Dockerfile, focusing on how to containerize a Java application. By the end of this article, you'll have a clear understanding of the fundamental concepts and practical examples.
What is Docker?
Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers are isolated environments that include everything required to run an application, such as code, runtime, libraries, and system tools. Docker simplifies application deployment and scaling, making it a popular choice for DevOps and development teams.
Dockerfile: Blueprint for Docker Containers
A Dockerfile is a text file that contains instructions for building a Docker container image. It specifies a base image, adds application code, sets environment variables, and configures the container's behavior. Let's create a Dockerfile for a simple Java application.
Example: Dockerizing a Java Application
Suppose you have a basic Java application with the following structure:
my-java-app/
└── src/
| └── Main.java
└── Dockerfile
1. Create a Dockerfile
Open a text editor and create a file named Dockerfile within your project directory. This is the starting point for containerization.
Note: Dockerfile doesn't have any file extension
# Use the official OpenJDK base image
FROM openjdk:11
# Set the working directory
WORKDIR /app
# This will run and build jar with all dependencies
RUN mvn install
# Copy the application JAR file into the container
COPY target/my-java-app.jar /app
# Specify the command to run when the container starts
CMD ["java", "-jar", "my-java-app.jar"]
Let's break down the Dockerfile:
- FROM openjdk:11: This line specifies the base image as OpenJDK 11, which is suitable for a Java application.
- WORKDIR /app: Sets the working directory inside the container to /app.
- RUN mvn install: This will run and build jar file with all dependencies which is used to help run the application.
Recommended by LinkedIn
- COPY target/my-java-app.jar /app: Copies the application JAR file into the container's /app directory.
- CMD ["java", "-jar", "my-java-app.jar"]: Defines the command to execute when the container starts, running your Java application.
2. Build the Docker Image
Now that you have a Dockerfile, you can use it to build a Docker image. Open a terminal and navigate to your project directory. Run the following command:
# This command will build the docker image
docker build -t my-java-app .
This command instructs Docker to build an image named my-java-app using the current directory (denoted by .) as the build context.
3. Run the Docker Container
Once the image is built, you can run a Docker container based on it. Use the following command:
# docker run -p host_port:container_port image_name
docker run -p 7000:7000 my-java-app
The -p option is essential when you want to make services inside containers accessible to the outside world or when you're working with multiple containers that need to communicate over specific ports.
host_port is the port number on the host machine to which you want to map the container's port.
container_port is the port number on the container where the service/application is listening.
Congratulations! You've successfully containerized your Java application. Docker handles the creation and execution of the container, ensuring that it runs consistently across different environments.
Conclusion:
Docker and Dockerfile are powerful tools for containerizing applications, making it easier to develop, test, and deploy your software. In this blog, we've covered the basics of Docker and created a Dockerfile for a simple Java application. With this knowledge, you can start containerizing your own applications and take full advantage of the benefits Docker offers in terms of portability, scalability, and consistency.
Containerization is a fundamental skill for modern software development, and learning Docker is a significant step toward mastering it. Start experimenting with Docker and discover how it can streamline your development and deployment processes. Happy containerizing!
Medium:
CSV Consultant at VRR LIFE SCIENCE SERVICES LLP
1yVery useful
Risk Advisory Consultant | ISO 27001:2022 | CEH Certified | TPRM | GRC
1yLove this