Portable development environment with Docker and IntelliJ

Mateusz Joniak - October 15, 2018

For many developers, managing the environment required to work productively is a quite tedious task.

Sure, the IDE does the lion’s share of preparations for you. But still, you need to install a specific version of the database, then make sure you have the right compiler and runtime environment. In some cases, you would also have to work on a given version of the operating system. Gods help you if you find yourself in the hell of “hidden” dependencies, like libraries you’re supposed to have installed separately. Configuring all of this takes hours, so forget about first-day productivity, and if you fail, all you’re going to get are some cryptic error messages that you have to type into Google. Sounds familiar?

This becomes even more problematic when you’re part of a team. How can you make sure everyone is working with the same configuration? Sometimes even differences between minor Java versions can cause different app behavior. Sometimes your code won’t even run on your colleague’s workstation, because there’s a bug in her older JRE version. And who knows what will happen on the production server, which has even different config - say, Linux, as opposed to the Windows 10 you’re using?

All this tangled mess could be solved by virtualization. However, full-blown VMs have their share of problems, most of them are performance and network-related.

So let’s explore some alternatives.

Enter Docker

Docker is a lightweight, OS-level virtualization engine. Your application is packaged together with all its dependencies, configuration files, tools, and whatnot in an “image”. Then, you can run the whole package on any system, and Docker ensures everything works as intended. Therefore, you can be sure the behavior is the same everywhere.

Images are described in Dockerfiles: small text files with minimal syntax, that allow you to specify what components you need in the package. Everyone using the same Dockerfile will get the same version of JVM, same config files, etc. Additionally, you can also set-up Docker on your production machine to run the application from the same image, so there are no surprises after deployment.

First steps

First, you need to download Docker from https://store.docker.com/. There are available installations for Windows, Mac and several Linux distros. So go there and find what you need. Unfortunately, you will probably have to create an account before downloading.

IntelliJ IDEA is an extremely popular IDE for all JVM languages, like Java, Scala, and Kotlin. It also has variants for web development, Python, and even .NET. So if you don’t have it already, you can obtain a free copy of the Community edition on https://www.jetbrains.com/idea/download.

If you want those two to work together, you also need a Docker integration plugin. It can be installed from IntelliJ, just go to File -> Settings -> Plugins.

Before using Docker, your user account must belong to the docker-users group. If you’re on Windows, you need to do this manually: find the Computer Management in the Start menu, run it with Admin privileges, and then go to Local Users and Groups. Find your account and add it to docker-users. Afterward, you need to login to Windows again to refresh your group membership.

Docker settings on Windows

Another thing is, sadly, the current Docker integration plugin version doesn’t handle encryption properly. This problem can be circumvented if you disable TLS on the Docker daemon socket. Right click on Docker in the tray, select Settings and then enable the checkbox “Expose daemon without TLS”. Don’t do it on production though.

Now, assuming you have an app and want to try it out with Docker, you can add a Dockerfile to the project. For more info on the syntax and capabilities, take a look at the documentation.

Below a simple Dockerfile for a Spring Boot application.

First, you can declare the base image using the FROM instruction. In this case, it will download an OpenJDK 10 setup from the official repository.

ADD instructions allow you to copy specific files from host to the image. Those can be compiled JARs, config files, etc. You can also specify environment variables through ENV.

Finally, let’s set the ENTRYPOINT. This is the command that will be run when the container (deployed image) starts.

Of course, this is only an elementary example. Dockerfiles come with great flexibility and give you a lot of options. So feel free to explore and experiment.

If you open the Dockerfile in IntelliJ with the plugin installed, it will automatically add a Run Configuration that you can use to deploy and test the container.

Dockerfile Run Configuration in IntelliJ

Networking

Each container runs in isolation, which means that by default you can’t access your application’s ports from outside. However, in contrast to full VMs, Docker makes it very easy to connect your container with the network: you only need to forward some ports.

If you’re using IntelliJ, this can be done via editing the Port Bindings section in Run Configuration.

Setup the Port Bindings

Typically, you need to bind the TCP port that your app is listening on, to the corresponding port on the host. For example, in a Spring Boot web app, you would need to bind the TCP 8080 socket.

If you want to use the debugger (more on this in the next section), the relevant port - in our example, TCP 5005 - should be forwarded as well.

Another thing to remember is that now localhost always refers to the container, not the host system. Therefore, if you have a local DB instance on your computer, you won’t be able to connect to it from your app.

To solve this, you can either use a specific address defined by your Docker distribution: docker.for.win.localhost on Windows, docker.for.mac.localhost on Mac, or host.docker.internal, which should work on both systems.

Debugging

Ok, so you can now run your dockerized app straight from the IDE. But there always comes the point in your professional life, when just staring at the code is not enough to find this irritating heisenbug that’s been ruining your team’s blood for the last few weeks. You need to sit down, attach an old-fashioned debugger, and hunt the beast down in the impenetrable jungle that your codebase has somehow become.

Unfortunately, the tooling does not provide us with too many options in this area. The only working solution is to set up the containerized JDK for remote debugging.

You would need to modify the Dockerfile’s ENTRYPOINT:

Now, the virtual machine will be listening for a debugger connection on TCP 5050. This socket needs to be exposed as described before.

Next, you need to manually add a new Debug Config in IntelliJ, using the Remote template. Default settings with port 5005 should work correctly.

Remote debugging template

Remember that this only works if the app is already running. So you need to first deploy the container with standard Run Configuration, and only then attach the remote debugger.

Conclusion

All in all, if you frequently face issues related to synchronizing your environment and config with your colleagues, or between your development computer and production server, dockerizing may be the way to go.

However, because the toolset is still in many ways immature, debugging can be quite clunky. Moreover, at the moment the plugin is a bit glitchy. You may notice disappearing logs and other bugs.

Therefore, it may be best to take a middle road and keep developing the software traditionally, while only testing the changes in the dockerized environment before committing to the common repo or deploying to the server.

Tempted to work
on something
as creative?

That’s all we do.

join nexocode

Find us on

Need help with implementing AI in your business?

Let's talk blue circle

This site uses cookies for analytical purposes.

Accept Privacy Policy

In the interests of your safety and to implement the principle of lawful, reliable and transparent processing of your personal data when using our services, we developed this document called the Privacy Policy. This document regulates the processing and protection of Users’ personal data in connection with their use of the Website and has been prepared by Nexocode.

To ensure the protection of Users' personal data, Nexocode applies appropriate organizational and technical solutions to prevent privacy breaches. Nexocode implements measures to ensure security at the level which ensures compliance with applicable Polish and European laws such as:

  1. Regulation (EU) 2016/679 of the European Parliament and of the Council of 27 April 2016 on the protection of natural persons with regard to the processing of personal data and on the free movement of such data, and repealing Directive 95/46/EC (General Data Protection Regulation) (published in the Official Journal of the European Union L 119, p 1); Act of 10 May 2018 on personal data protection (published in the Journal of Laws of 2018, item 1000);
  2. Act of 18 July 2002 on providing services by electronic means;
  3. Telecommunications Law of 16 July 2004.

The Website is secured by the SSL protocol, which provides secure data transmission on the Internet.

1. Definitions

  1. User – a person that uses the Website, i.e. a natural person with full legal capacity, a legal person, or an organizational unit which is not a legal person to which specific provisions grant legal capacity.
  2. Nexocode – NEXOCODE sp. z o.o. with its registered office in Kraków, ul. Generała Henryka Kamieńskiego 51, 30-644 Kraków, entered into the Register of Entrepreneurs of the National Court Register kept by the District Court for Kraków-Śródmieście in Kraków, 11th Commercial Department of the National Court Register, under the KRS number: 0000686992, NIP: 6762533324.
  3. Website – website run by Nexocode, at the URL: nexocode.com whose content is available to authorized persons.
  4. Cookies – small files saved by the server on the User's computer, which the server can read when when the website is accessed from the computer.
  5. SSL protocol – a special standard for transmitting data on the Internet which unlike ordinary methods of data transmission encrypts data transmission.
  6. System log – the information that the User's computer transmits to the server which may contain various data (e.g. the user’s IP number), allowing to determine the approximate location where the connection came from.
  7. IP address – individual number which is usually assigned to every computer connected to the Internet. The IP number can be permanently associated with the computer (static) or assigned to a given connection (dynamic).
  8. GDPR – Regulation 2016/679 of the European Parliament and of the Council of 27 April 2016 on the protection of individuals regarding the processing of personal data and onthe free transmission of such data, repealing Directive 95/46 / EC (General Data Protection Regulation).
  9. Personal data – information about an identified or identifiable natural person ("data subject"). An identifiable natural person is a person who can be directly or indirectly identified, in particular on the basis of identifiers such as name, identification number, location data, online identifiers or one or more specific factors determining the physical, physiological, genetic, mental, economic, cultural or social identity of a natural person.
  10. Processing – any operations performed on personal data, such as collecting, recording, storing, developing, modifying, sharing, and deleting, especially when performed in IT systems.

2. Cookies

The Website is secured by the SSL protocol, which provides secure data transmission on the Internet. The Website, in accordance with art. 173 of the Telecommunications Act of 16 July 2004 of the Republic of Poland, uses Cookies, i.e. data, in particular text files, stored on the User's end device.
Cookies are used to:

  1. improve user experience and facilitate navigation on the site;
  2. help to identify returning Users who access the website using the device on which Cookies were saved;
  3. creating statistics which help to understand how the Users use websites, which allows to improve their structure and content;
  4. adjusting the content of the Website pages to specific User’s preferences and optimizing the websites website experience to the each User's individual needs.

Cookies usually contain the name of the website from which they originate, their storage time on the end device and a unique number. On our Website, we use the following types of Cookies:

  • "Session" – cookie files stored on the User's end device until the Uses logs out, leaves the website or turns off the web browser;
  • "Persistent" – cookie files stored on the User's end device for the time specified in the Cookie file parameters or until they are deleted by the User;
  • "Performance" – cookies used specifically for gathering data on how visitors use a website to measure the performance of a website;
  • "Strictly necessary" – essential for browsing the website and using its features, such as accessing secure areas of the site;
  • "Functional" – cookies enabling remembering the settings selected by the User and personalizing the User interface;
  • "First-party" – cookies stored by the Website;
  • "Third-party" – cookies derived from a website other than the Website;
  • "Facebook cookies" – You should read Facebook cookies policy: https://www.facebook.com/policy/cookies
  • "Other Google cookies" – Refer to Google cookie policy: www.google.com/policies/technologies/types/

3. How System Logs work on the Website

User's activity on the Website, including the User’s Personal Data, is recorded in System Logs. The information collected in the Logs is processed primarily for purposes related to the provision of services, i.e. for the purposes of:

  • analytics – to improve the quality of services provided by us as part of the Website and adapt its functionalities to the needs of the Users. The legal basis for processing in this case is the legitimate interest of Nexocode consisting in analyzing Users' activities and their preferences;
  • fraud detection, identification and countering threats to stability and correct operation of the Website.

4. Cookie mechanism on the Website

Our site uses basic cookies that facilitate the use of its resources. Cookies contain useful information and are stored on the User's computer – our server can read them when connecting to this computer again. Most web browsers allow cookies to be stored on the User's end device by default. Each User can change their Cookie settings in the web browser settings menu: Google ChromeOpen the menu (click the three-dot icon in the upper right corner), Settings > Advanced. In the "Privacy and security" section, click the Content Settings button. In the "Cookies and site date" section you can change the following Cookie settings:

  • Deleting cookies,
  • Blocking cookies by default,
  • Default permission for cookies,
  • Saving Cookies and website data by default and clearing them when the browser is closed,
  • Specifying exceptions for Cookies for specific websites or domains

Internet Explorer 6.0 and 7.0
From the browser menu (upper right corner): Tools > Internet Options > Privacy, click the Sites button. Use the slider to set the desired level, confirm the change with the OK button.

Mozilla Firefox
browser menu: Tools > Options > Privacy and security. Activate the “Custom” field. From there, you can check a relevant field to decide whether or not to accept cookies.

Opera
Open the browser’s settings menu: Go to the Advanced section > Site Settings > Cookies and site data. From there, adjust the setting: Allow sites to save and read cookie data

Safari
In the Safari drop-down menu, select Preferences and click the Security icon.From there, select the desired security level in the "Accept cookies" area.

Disabling Cookies in your browser does not deprive you of access to the resources of the Website. Web browsers, by default, allow storing Cookies on the User's end device. Website Users can freely adjust cookie settings. The web browser allows you to delete cookies. It is also possible to automatically block cookies. Detailed information on this subject is provided in the help or documentation of the specific web browser used by the User. The User can decide not to receive Cookies by changing browser settings. However, disabling Cookies necessary for authentication, security or remembering User preferences may impact user experience, or even make the Website unusable.

5. Additional information

External links may be placed on the Website enabling Users to directly reach other website. Also, while using the Website, cookies may also be placed on the User’s device from other entities, in particular from third parties such as Google, in order to enable the use the functionalities of the Website integrated with these third parties. Each of such providers sets out the rules for the use of cookies in their privacy policy, so for security reasons we recommend that you read the privacy policy document before using these pages. We reserve the right to change this privacy policy at any time by publishing an updated version on our Website. After making the change, the privacy policy will be published on the page with a new date. For more information on the conditions of providing services, in particular the rules of using the Website, contracting, as well as the conditions of accessing content and using the Website, please refer to the the Website’s Terms and Conditions.

Nexocode Team