Using Nothing But Docker For Projects
Raphael Amoedo loves learning new tools and technologies and working with them. He is passionate about sharing with the community spreading knowledge and More aboutRaphael
Level One Using Alias With Docker
Imagine the following situation you start working on a new project maybe also with a different programming language youre not used to. So the project is there and you should be able to run it.
Website Designing Company In Ludhiana
You hope theres any documentation telling you what to do which is not that common and if/when theres any it usually doesnt work. You need to know what to install where to install it how to set it up etc. Its not an uncommon scenario and you can actually expect that at some point. But what if there was a way to ensure this wont happen again
Throughout this post well see different approaches we could use to make this easier using only Docker.
Lets consider a Java project for example. Usually to run a Java application you run java -jar application.jar.
Website Development Cost
To generate the jar file and manage project dependencies you can use a lot of different tools with the most known being Maven and Gradle. Lets consider Maven for this example. Lets see some Maven commands
Level Two Using Docker For Running The Application
This way you can run any Maven and Java commands without having to install Java or Maven. You can test the commands by running java -version or mvn -version. Usually the official Docker image of these tools gives you instructions on how to run it and you can just create an alias for that.
Its a fair approach especially if you know what youre doing. But that doesnt come with the project itself. So lets try to improve it a bit.
Website Development Company
Meet Image Optimization Addy Osmanis brand new practical guide to optimizing and delivering high-quality images on the web. From formats and compression to delivery and maintenance everything in one single 528-pages book.
Thats where Dockerfile starts to shine. We know how to run commands using only Docker but how to run the application
And you can build it the way you normally build a docker image using docker build -t my-application . for example.
Level Three Using Docker For Building And Running The Application
You can see that it depends on an existing JAR file to build it. As you saw on the previous topic we know how to generate it but we would be in trouble if it were another programming language or tool.
It seems like a really minor improvement but that helps a lot already as you can see in its pros/cons
Its a good approach. You can merge Level One and Level Two to achieve a better result. The project should have Dockerfile and life gets a bit easier already. But lets see how life can be even easier.
What if you didnt know anything about Java and Maven and you were still able to build the application with just one command you already know
How can that help us Well lets consider the previous Dockerfile. We needed a JAR file to build the Docker image. With Multi-Stage Build Dockerfile itself can be responsible for generating it. In a simple approach that Dockerfile would look like this
Appendix Using Docker For Every Major Command
From the first FROM to the first RUN statement its doing things related to Maven copying the files that need to be copied and running the command that downloads the dependencies and builds the application. Its doing that by using the maven3-jdk-11-slim image and its setting the name builder.
After that you see the second FROM statement using the openjdk11-jre-slim image. You also see a COPY statement which copies from a place called builder. But whats that place Whats that builder
Thats the name we set for the Maven image at the first FROM statement. Its copying the jar file from that container. So you can literally play with different FROM entries to build whatever you want and the command to build the Docker image is still the same docker build -t my-application ..
Worth saying that you can also use this Dockerfile with Docker Compose which can be really powerful especially if your application needs to expose ports sharing volumes or it depends on other images.
Now that you know how to play with different FROM statements another possible Dockerfile could be
So now we have four different steps dependencyresolver tester builder and the application itself.
If we want either build the application or test it we need the project dependencies. Thus theres a dependencyresolver step there. In the second and the third FROM statements you can see that they depend on dependencyresolver.
Important If you try to build the docker image with docker build -t my-application . only the first the third and the last step dependencyresolver builder and the application itself respectively would run.
When you try to build the image it will try to see what the end state of the image is which would be the application itself. As we know and can see the last step depends on builder COPY statement. If we check the builder step we will see that it depends on dependencyresolver FROM statement. So it will run in this order
We saw how to use only Docker to build and run applications and commands removing the need for previous knowledge in some tool or programming language. Also worth saying that even though we used Docker for these examples this would also work with other container runtimes like Podman.
...Read More

