Fortran in Docker

The gcc image

The official gcc image already contains gfortran, so Docker users can skip a local compiler install.

Pin a tag such as gcc:14 to keep builds repeatable across machines and across course lessons.

Run one-liners

Start with a version check that leaves no container behind after it exits.

docker run --rm gcc:14 gfortran --version

GNU Fortran 14.2.0

The remove flag deletes the stopped container automatically. The image stays cached for the next run.

Mount your source

Mount the current folder into the container, set it as the work folder, then compile and run inside.

docker run --rm -v "$PWD":/app -w /app gcc:14 gfortran -Wall -Wextra hello.f90 -o hello

docker run --rm -v "$PWD":/app -w /app gcc:14 ./hello

Hello, Fortran

The compiled binary lands beside your source file because the folder is shared. Edit locally and rerun the same two lines.

Keep warning flags

Keep -Wall -Wextra inside the container exactly as on a local install so the compiler warns about suspicious code.

Add the modern standard flag to the same line once the lesson asks for it, then compare warnings across runs.

docker run --rm -v "$PWD":/app -w /app gcc:14 gfortran -Wall -Wextra -std=f2018 hello.f90 -o hello

The code on this page was not run: it needs Docker and the gcc:14 image.

Article author: Arthur Isaev

Banner image