This blog post is aimed at web developers, from beginners to experts, who seek to understand Docker and its vast possibilities. By the end of this article, we'll have mastered the art of containerizing our web applications using Docker. 🚀🐳
Step 1: Install Docker 📦
Let's start by installing Docker on our machine. Depending on your OS, follow the appropriate guide from Docker's official website here.
For instance, if you're on a macOS, the installation command via Homebrew would be:
brew install --cask docker
Step 2: Dockerfile Basics 🏗️
A Dockerfile
is a text file that Docker reads instructions from, to build a Docker image. The image is then used to launch Docker containers.
Let's create a Dockerfile
in our root project directory.
touch Dockerfile
Next, we'll write our first Docker instructions in that file.
# start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# add a volume pointing to /tmp
VOLUME /tmp
# make port 8080 available to the world outside this container
EXPOSE 8080
# application's JAR file
ARG JAR_FILE=target/my-app-0.0.1-SNAPSHOT.jar
# add the application's JAR file to the container
ADD ${JAR_FILE} app.jar
# run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
This Dockerfile
instructs Docker to:
- Take the
openjdk:8-jdk-alpine
image and build upon it 🏗️ - Declare a mount point in the filesystem at
/tmp
📁 - Expose port
8080
to outside the container 🚪 - Specify the
JAR_FILE
path and add it to the docker image root asapp.jar
- Set this application as the container's entry point, meaning once the container is up, this application will run by default.
Step 3: Build and Run Docker Container 🚀
Navigate to your project root directory where your Dockerfile
resides and run the following command.
docker build -t my-app .
This command will tell Docker to build a Docker image using the provided Dockerfile and tag it (name it) as my-app
.
Once the image is built, we can now run it.
docker run -p 8080:8080 my-app
This command tells Docker to run the my-app
image and map the container's port 8080
to our host's port 8080
.
Now your containerized application should be running on http://localhost:8080
.
That's it, folks! Using Docker to containerize your applications may seem daunting at first, but with a little hand-holding, it's a breeze. 🍃 Enjoy the journey and keep exploring the vast world of Docker. 🐳🌐
Happy Coding! 👩💻👨💻
For the curious minds eager to learn more, follow the links below. But, be aware though that technology moves at a quite fast pace, and some information might be outdated as you read this.