You may create a temporary image and then use it’s filesystem during the build of final image. It installs all the dependencies and compile all the sources in intermediate image and copy.
Use jlink to create an JRE with only the modules you need. Here is a jlink tutorial
1 2 3 4 5 6 7 8 9 10 11 12 13
FROM adoptopenjdk/openjdk11:x86_64-ubuntu-jdk-11.28 as compiler # create minimal jre RUN jlink --module-path /opt/java/openjdk/jmods --verbose \ --add-modules java.base,java.logging,java.xml,java.scripting,jdk.jdwp.agent \ --compress 2 \ --no-heaer-files \ --output /opt/jre-11-minimal # reduces the previous one to almost half of its size FROM baseline RUNmkdir -p /usr/lib/jvm COPY --from=compiler /opt/jre-11minimal /opt/jre RUNln -s /opt/jre/bin/java /usr/bin/java
Reduce Java dependencies
Many images have the same dependencies, so they may be moved to another layer.
if [ $? -ne 0]; then #image does not exist cp -r ./lib ./dependencies/ # this directory contains dependencies.dockerfile cd ./dependencies/ docker build --build-arg -t ${dep-tag}$ -f dependencies.dockerfile . docker push ${dep_tag} fi
file_name = 'demofile.txt' withopen(file_name) as file_object: lines = file_object.readlines() for line in lines: print(line)
Write
1 2 3 4 5 6 7 8
# overwrite file_name = 'journal.txt' withopen(file_name, 'w') as file_object: file_object.write("I am coding Python.") # append filename = 'journal.txt' withopen(filename, 'a') as file_object: file_object.write("\nThis is some appended text.")
Functions
Simple
1 2 3 4 5 6 7
# defintion defgreet_user(): # Display a simple greeting print("Hello!")
# call greet_user()
With default value parameters
1 2 3 4 5 6
defmake_pizza(topping='bacon'): """Make a single-topping pizza.""" print("Have a " + topping + " pizza!")
make_pizza() make_pizza('pepperoni')
Returning a value
1 2 3 4
defadd_numbers(x, y): """Add two numbers and return the sum."""return x + y sum = add_numbers(3, 5) print(sum)
Classes
Simple class
1 2 3 4 5 6 7 8 9 10 11 12 13
classDog(): """Represent a dog.""" def__init__(self, name): """Initialize dog object.""" self.name = name defsit(self): """Simulate sitting.""" print(self.name + " is sitting.")
my_dog = Dog('Buddy') print(my_dog.name + " is a great dog!") my_dog.sit()
Inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSARDog(Dog): """Represent a search dog.""" def__init__(self, name): """Initialize the sardog.""" super().__init__(name) defsearch(self): """Simulate searching.""" print(self.name + " is searching.") my_dog = SARDog('WToby') print(my_dog.name + " is a search dog.") my_dog.sit() my_dog.search()
Exceptions
Try/catch/else:
1 2 3 4 5 6 7 8 9
prompt = "How many tickets do you need? " num_tickets = input(prompt)
# run python -m coverage run -m --source="directory_to_inspect" unittest discover # get coverage report on CLI python -m coverage report # get html coverage python -m coverage html
# you may store this a script on package.json "scripts": { "test": "node jasmine.js", "coverage": "nyc --reporter=lcov --reporter=text-summary npm run test" },