Learn how to dockerize a simple application with this quick guide

Marcos André Martins
Liferay Engineering Brazil
5 min readJan 20, 2022

This tutorial is a quick guide of what you need to do to create, build and start a simple application using Docker.

Prerequisite

The magic behind Docker is that you don’t need anything else except Docker itself. So, if you don’t have it installed, go to Get Docker page, choose your operating system and follow the steps.

What’s Docker?

Ok, we are going to see how to use Docker for starting an application, but what is Docker? Basically, Docker is an open platform for running applications. It allows you to make your applications independent from your infrastructure. With Docker, you can run your software in an isolated environment, which we call container, that is created from an image. The Image is a template with instructions for creating a container, these instructions are defined in a text file called Dockerfile.

Use Case

We are going to use a simple Spring Boot application that is available on GitHub. Basically, it consists in getting a list of books, which were previously loaded in a MySQL database, by consuming a REST endpoint.

At the end of this article, we will have started a Docker container for that application and connect it with a MySQL database.

Dockerfile

As we saw previously, Dockerfile is the file where we put the instructions of an image for creating a container. So, before going on through the Docker command lines, let’s have a look at our Dockerfile and understand what we are doing there.

FROM openjdk:8-jdk-alpine
USER root
COPY . /source
WORKDIR /source
RUN ./gradlew build
EXPOSE 8080
ENTRYPOINT ["java","-jar","build/libs/docker-demo-0.0.1-SNAPSHOT.jar"]

An image is usually created by customizing another image that is available in DockerHub. For this image, we used an Alpine image, a very lightweight Linux distribution, which is usually used for this case. FROM openjdk:8-jdk-alpine creates a basic image with an Alpine instance with JDK 8 already installed. You also can get other versions of Alpine on its DockerHub official page.

USER root tell us we will have root privileges for this image.

Now, we need to find a way to put our code into the Docker image. To do this, we can use COPY . /source, it copies all the files in the current directory to the source directory within the image.

Once we already have our code into the image, we have to define it as our working directory in order to build it. WORKDIR /source can help us with this. It takes us to /source directory inside of the image.

We are using Gradle as build tool, so we need to run ./gradlew build to build our project. TheRUN instruction executes a command within the image, just run RUN ./gradlew build.

The EXPOSE 8080 instruction informs that the container listens on port 8080.

Finally, with ENTRYPOINT ["java", “-jar", “build/libs/docker-demo-0.0.1-SNAPSHOT.jar"], we define which command will be run when we start the container. That’s the point where we start our application within the container.

Executing Docker Commands

Now we understood what we did in Dockerfile, we can go ahead and put this project up!

As I mentioned at the beginning of this article, our application stores its data into a MySQL database. Therefore, we need to have an image of MySQL. You can get the mysql image by running

docker pull mysql

If you would like to see more information about MySQL image, please see its DockerHub offical page.

The next step is creating a network, so we can put MySQL and our project’s containers on that. To do this, just run:

docker network create docker-demo-net

This creates a network called docker-demo-net.

It’s time to start our containers up. We are going to use docker run -d imageNamecommand for it. It just creates and starts a container from imageName. -d argument stands for -detach and is used for not shutting your container down if you leave the terminal.

So, for MySQL, we will use the following command:

docker run -d --env MYSQL_DATABASE=docker-demo --env MYSQL_ROOT_PASSWORD=password --name mysql --network docker-demo-net mysql

It is pretty similar to what we have just seen above with few more arguments. It starts a container from the image mysql. By using --env MYSQL_DATABASE=docker-demo --env MYSQL_ROOT_PASSWORD=password, we define those environments variables for MySQL, which will be used to create a database called docker-demo and set the user root password to password.

We use --name mysql for giving a friendly name to the mysql container. If we don’t do it, the container’s name will be generated randomly.

Passing --network argument, we put the container on a network. We created the network docker-demo-net few steps ago, so we just have to pass its name like this: --network docker-demo-net.

All right! We now have a MySQL container running. You can confirm it by executing docker ps and see the mysql container is there and up.

Now let’s build our docker-demo image. To do this, in your terminal, navigate to docker-demo source directory and run

docker build --tag docker-demo .

This builds a docker image using the Dockerfile we have in the current directory. --tag docker-demo is used here for naming the new image. Execute docker image ls and see docker-demo image was created properly.

The last step is using docker run for creating and starting our docker-demo container:

docker run -d -p 3000:8080 --name docker-demo --network docker-demo-net docker-demo

It is very similar to what we did with MySQL. We used docker-demo image to create this container and set the docker-demo as container name and docker-demo-net as its network. A new thing here is -p 3000:8080. It means when we call the requests to port 3000, they will redirected to port 8080 in Docker.

Run docker ps again and check that docker-demo container was created and is up properly.

That’s it!

We’re done! Run curl http://localhost:3000/list in your terminal and see that it returns a list of books.

--

--