Latest Swift running with Docker

Swift is a programming language that is designed to work with Appleā€™s ecosystem. However, it can run on Linux. It is not just written for Apple products. If you do not have Mac hardware or you do not want to install newest Xcode 13 version on your Mac you should read

Running Swift 5 latest on Linux

The latest Swift language version is p

rovided as a Docker image. You should install this image into a Docker container with just a few Docker commands and reuse this container.

# downloads swift latest image
docker pull swift        

# create local folder for your swift files
mkdir ~/Swift      

# Create a container from swift image and attach it
docker run -it --name swiftlatest -v $HOME/Swift:/root/Swift swift

# To start and attach your image later
docker start swiftlatest
docker attach swiftlatest
  

The container is defined together with a bind mount. Bind mounts have been around since the early days of Docker. They are really helpful for the scenario where you want to write your code on your target OS i.e. macOS with your favourite editor and run or test this code in a linux container.

Write & Compile & Run

Now you are able to create a new swift package and checkout the new swift features. This can all be done within a terminal running an attached shell and your favourite code editor on your desktop os.

# Start swiftlatest container
docker start swiftlatest

# Attach with shell to started container
docker attach swiftlatest

# Create a project directory
mkdir HelloWorld
cd HelloWorld
swift package init --type executable --name HelloWorld
 
# Edit main.swift with our favourite editor 

# Compile and run 
swift run 

# ... or compile and test
swift test

tomkausch

Leave a Reply

Your email address will not be published. Required fields are marked *