Docker Setup

Docker Setup

Finally we are going to setup and run our first container. Excited ?!

Alright, lets begin. Bring up the ubuntu terminal we setup in the previous article.

Setup DockerHub Account

DockerHub is a cloud-based registry service to store our images. Consider it as GitHub for images. Here are the steps

  • Login to DockerHub here - DockerHub
  • Click on profile on top left corner and select Account Setting
  • Under Security, find Personal Access Token
  • Generate a token by entering the token name e.g. "Docker CLI"
  • Now copy the values as they won't ever be displayed again
  • Back to our linux terminal

Let's complete the setup now

  1. sudo apt install ca-certificates curl This will install the ca-certificates, which will be used to validate the authenticity of the SSL certificates of the urls. In short, this will enable us to make secure https connections.
  2. sudo install -m 0755 -d /etc/apt/keyrings This will create a directory and provide the owner read/write/execute permission, while group members get read and execute but not write access.
  3. This will download Docker's GPG and save it to the path specified. sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.ascDocker GPG key is used to verify the authenticity of the packages being installed through docker
  4. sudo chmod a+r /etc/apt/keyrings/docker.asc This will update the permission of the file to be accessible to system and apt, so they can use the file to verify the authenticity of the packages
  5. Now let's setup the architecture echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  6. sudo apt update This will again refresh the packages from our new repository
  7. Let's get the docker engine now sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin This command adds the Docker APT repository for our specific Ubuntu version to the /etc/apt/sources.list.d/docker.list file. It ensures Docker packages are sourced from the correct repository and verified with the provided GPG key.
  8. Login to DockerHub Account in the VM execute docker login go to https://login.docker.com/activateenter the login code and submit. once done, back in terminal, we'll get 'Login Succeeded' message


Now our setup is done.

Let's verify.

Run command sudo docker run hello-world. We'll get a very long trail of logs but this is specifically what we are looking for

...
Hello from Docker!
This message shows that your installation appears to be working correctly.
....        

This confirms our setup is now done.

Let's run our own app in docker

To test we'll create our java app. Create a new java project. In pom.xml add the code below so we can have the jar

<packaging>jar</packaging>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>        

This will do things for us

  • Package our app in jar
  • Setup the entry point for jar to run i.e when we run jar it specifies where to start the execution from

Now, simply add a Main class to do "Hello World !" in main/java

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello from java app");
    }
}        

Once done, we need to verify if it works fine. Bring up the windows terminal or mac, navigate to project's root i.e. where pom.xml resides and run mvn clean package this should give us a jar file in target folder

Run java -jar target/<jar name> that should log "Hello World !"

Writing our dockerfile

As discussed earlier, we need to tell docker what all are the dependency our app needs to run properly and what steps need to start it.

Let's go ahead and do that

# Bring in the OS and java setup
# This will bring up a tiny little container with ubuntu and java setup
FROM openjdk:21-jdk-slim

# This will setup location for our next command execution
WORKDIR /app

# Copy the jar with a different name in /app
COPY target/DockerJavaDemo-1.0-SNAPSHOT.jar app.jar

# run the jar
ENTRYPOINT ["java", "-jar", "app.jar"]        

Now we need to create an image docker image. In the same terminal where we ran our maven command run wsl this will open the ubuntu terminal at the same location

Now build the image with docker build -t devdanish.in:dockerJavaDemo .

  • -t : provides the tag name
  • . : specifies the location of the dockerfile

Wait, it might take sometime. Then check if the image is created with docker images | grep dockerJavaDemo

Now time to run our application

docker run devdanish.in:dockerJavaDemo

Hello from java app

Hello from java app        

And that's it. We have now successfully ran our application in docker environment. Now this image can help us execute our application in environment without any external setup required.


In the next article, we'll run Springboot and node applications in docker and learn to pass environment variables.

Since I get time to draft these only on the weekends, you can checkout my notes at technotes.devdanish.in/docker

要查看或添加评论,请登录

Danish Javed的更多文章

  • Spring Boot and Node Applications in Docker

    Spring Boot and Node Applications in Docker

    Now we are finally getting close to a production setup. In microservices we have multiple instance of multiple…

  • Docker Perquisite understanding & Setup

    Docker Perquisite understanding & Setup

    This is continuation of my previous article. if you haven't read that already, I recommend reading that first :…

  • Docker Containers and Images

    Docker Containers and Images

    This is continuation of my previous article. if you haven't read that already, I recommend reading that first :…

  • Docker Overview

    Docker Overview

    Before diving in with what is Docker, we first need to understand why is docker and what problem it is trying to solve…

社区洞察