This is a tutorial on how to „dockerize“ an existing application. We will use Ubuntu as the platform.
Install Docker
If this is the first time you’re working with Docker, I suggest to use the „stable“ update channel of Docker CE (Community Edition). This will give you best stability and updates once a quarter.
Install using official repository
Although you can install manually using a .deb package, using the repository will ease future updates significantly. This is the recommended approach.
Set up repository
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Install Docker CE
sudo apt-get update
sudo apt-get install docker-ce
Verify installation
To verify your installation, download and run a test image:
sudo docker run hello-world
This should – among other information – output „Hello from Docker!“. To list all images that are available locally, execute a
sudo docker image ls
Create the image
In the following steps we will use the j-lawyer.org server as a test application, which means we will run it inside of a docker container. The first step is to create a docker image running a Wildfly Application Server. Note that we are using an older version of Wildfly because j-lawyer.org has not been upgraded to the latest Wildfly AS yet.
Create a Dockerfile
mkdir j-lawyer-docker
cd j-lawyer-docker
touch Dockerfile
Now copy and paste the following into your Dockerfile:
# Use latest jboss/base-jdk:8 image as the base FROM jboss/base-jdk:8 # Set the WILDFLY_VERSION env variable ENV WILDFLY_VERSION 9.0.2.Final ENV WILDFLY_SHA1 b2039cc4979c7e50a0b6ee0e5153d13d537d492f ENV JBOSS_HOME /usr/local/j-lawyer-server/wildfly-9.0.2.Final ENV JBOSS_HOME_PARENT /usr/local/j-lawyer-server USER root # Add the WildFly distribution to /usr, and make wildfly the owner of the extracted tar content # Make sure the distribution is available from a well-known place # RUN cd $HOME \ # && curl -O https://download.jboss.org/wildfly/$WILDFLY_VERSION/wildfly-$WILDFLY_VERSION.tar.gz \ # && sha1sum wildfly-$WILDFLY_VERSION.tar.gz | grep $WILDFLY_SHA1 \ # && tar xf wildfly-$WILDFLY_VERSION.tar.gz \ # && mv $HOME/wildfly-$WILDFLY_VERSION $JBOSS_HOME \ # && rm wildfly-$WILDFLY_VERSION.tar.gz \ # && chown -R jboss:0 ${JBOSS_HOME} \ # && chmod -R g+rw ${JBOSS_HOME} RUN cd $HOME RUN curl -O https://download.jboss.org/wildfly/$WILDFLY_VERSION/wildfly-$WILDFLY_VERSION.tar.gz # RUN sha1sum wildfly-$WILDFLY_VERSION.tar.gz | grep $WILDFLY_SHA1 RUN cd $HOME RUN tar xf wildfly-$WILDFLY_VERSION.tar.gz RUN mkdir -p $JBOSS_HOME_PARENT # RUN mv $HOME/wildfly-$WILDFLY_VERSION $JBOSS_HOME - this did not work... it always extracted in /opt/jboss instead of /root RUN mv /opt/jboss/wildfly-$WILDFLY_VERSION $JBOSS_HOME_PARENT RUN rm wildfly-$WILDFLY_VERSION.tar.gz RUN chown -R jboss:0 ${JBOSS_HOME} RUN chmod -R g+rw ${JBOSS_HOME} # Ensure signals are forwarded to the JVM process correctly for graceful shutdown ENV LAUNCH_JBOSS_IN_BACKGROUND true USER jboss # Expose the ports we're interested in EXPOSE 8080 # Set the default command to run on boot # This will boot WildFly in the standalone mode and bind to all interface CMD ["/usr/local/j-lawyer-server/wildfly-9.0.2.Final/bin/standalone.sh", "-b", "0.0.0.0"]
Build the image
Now let’s build the image, giving it a meaningful name:
sudo docker build -t j-lawyer-server .
After a successful build, your image is in your local machines image registry:
sudo docker image ls
Debugging Docker build errors
Docker builds the image in layers. In case of errors during a build, determine the ID of the last successful layer. You will find it in outputs like the following:
---> Running in 634d76cdfb03
Then run a bash in this layer:
docker run --rm -it 634d76cdfb03 bash -il
Use this bash to identify the issue with the next RUN command in your Dockerfile, then update your Dockerfile and re-run the build.
Clean up
If you ran into build issues, you probably also have failed images on your disk. To remove those and free disk space, run
docker system prune
Run the image
Run the application and map your local machines port 8000 to the containers exposed port 8080 using -p:
sudo docker run -p 8000:8080 j-lawyer-server
You can now point your browser to http://localhost:8000 to see the Wildfly welcome page, served by Wildfly AS running in your docker.
Is the j-lawyer Docker image also available from the official repo?
We are currently using it internally for running automated tests.
Official images are on the roadmap but depend on migrating to the latest Wildfly, so stay tuned 🙂