The starting point of a Dockerfile is to set the base image.Let’s go over the instructions you just added: bin/debug/Run serve -env local -hostname 0.0.0.0 RUN mv `swift build -show-bin-path` /app/binĮNTRYPOINT. RUN apt-get update & apt-get install libsqlite3-dev ![]() Open the file and add the following content: # 1 Use either your preferred text editor or run touch development.Dockerfile in Terminal, as long as you’re in the correct location. To get started, create a file named development.Dockerfile in the project’s root directory, at the same level of Package.swift. It’s just like a recipe, telling Docker what to do at each step. You can automate this process with a few steps as you would in a Continuous Integration environment. Even better: You can build the image once, upload it to a container registry and use it to spin up new servers and more. This way, when running the build command, Docker can deterministically generate the same image in different host machines. ![]() This file is like a recipe that Docker reads: it tells Docker how to assemble the image, what actions and commands it should execute and how to start your app or service. It’s time to create the app’s Dockerfile. Now, the app is ready to run on Linux using Docker. Notice that these change at compile time. The code above uses compiler directives to specify which code should compile in each platform. Remove the following line: private let platform = "macOS" Next, change the returned platform name ServerInfoResponse. The code above formats a time interval manually, instead of relying on DateComponentsFormatter. Let timeInterval = Date().timeIntervalSince(date)ĭuration = String(format: "%.2f d", timeInterval/86_400)ĭuration = String(format: "%.2f h", timeInterval/3_600)ĭuration = String(format: "%.2f m", timeInterval/60) Scroll to uptime(since date: Date) and replace the existing implementation with the following code: Open the ServerInfoController.swift file. One way to prevent this is to avoid using DateComponentsFormatter. Therefore, trying to compile the app on Linux as is will fail. The function in ServerInfoController that calculates the server uptime uses DateComponentsFormatter, which is currently in Swift 5.5 but is unavailable on Linux. ![]() To make sure your code runs smoothly on Linux, you need to remove the APIs that aren’t available on that platform. Removing APIs That are Unavailable on Linux Now it’s time to tackle the first objective: run code that’s conditional to a specific platform. The infrastructure of the project is working. Notice how the platform in this response is macOS. To check what it returns, run the following command in the Terminal: curl This endpoint returns a JSON object containing three values: the server start date, the uptime and the platform the server is running on. It contains one single endpoint: /server-info. In the controller’s directory, there is a file named ServerInfoController.swift, which declares a controller with the same name. ![]() Once the app is running, visit in your browser and you’ll see the TIL home page.Ĭhoose the starter project’s folder as the app’s working directory. Then, look at the files within the Controllers, Models and Migrations folders. Take a look at the configure.swift file, as well as the routes declared in routes.swift. While the app is compiling, explore the project. This will fetch all the dependencies and run the Vapor app. Unzip the file, open Terminal and navigate into the the starter folder. It appears in our Server-Side Swift book and video course. The sample project is the TIL app: a web app and API for searching and adding acronyms. This folder contains the files you’ll use to build the Vapor app. Start by clicking the Download Materials button at the top or bottom of this tutorial. Check out this great tutorial by Audrey Tam if you want to read more about the basics of Docker on macOS. Note: This tutorial requires Docker for Desktop on your Mac, which you canĭownload from Docker’s website.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |