|
| 1 | +# Visual Studio Code Remote Development |
| 2 | + |
| 3 | +[VSCode Remote Development](https://code.visualstudio.com/docs/remote/containers) allows you to run your code and environment in a container. This is especially useful for Swift when developing on macOS and deploying to Linux. You can ensure there are no compatibility issues in Foundation when running your code. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +As well as installing the Swift extension, you must install Docker on your machine to run the remote container in. See the [Visual Studio Code documentation](https://code.visualstudio.com/docs/remote/containers) for more details. |
| 8 | + |
| 9 | +Next, install the [Remote Development extension pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) that contains extensions for working in remote environments in VSCode. If you only want to work with remote containers (and not use the SSH or WSL containers), you may want to only install the [Remote Development Container extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) instead. |
| 10 | + |
| 11 | +## Running in a container |
| 12 | + |
| 13 | +### Manual Setup |
| 14 | + |
| 15 | +VSCode requires a `.devcontainer` directory which defines the settings in `devcontainer.json` and optionally a `Dockerfile` defining the container to run in. |
| 16 | + |
| 17 | +First create the directory. Next, create `devcontainer.json` and insert the following: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "name": "Swift 5.5", |
| 22 | + "image": "swift:5.5", |
| 23 | + "extensions": [ |
| 24 | + "sswg.swift-lang", |
| 25 | + ], |
| 26 | + "settings": { |
| 27 | + "lldb.library": "/usr/lib/liblldb.so" |
| 28 | + }, |
| 29 | + "forwardPorts": [8080] |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +This defines the minimum settings required to run a Swift package in a remote container. Here's what each thing does: |
| 34 | + |
| 35 | +* `name`: Used to specify the name of the remote container. |
| 36 | +* `image`: The Docker container image to run. You can choose whichever version of Swift you like, including [nightlies](https://hub.docker.com/r/swiftlang/swift). |
| 37 | +* `extensions`: Extensions to install in your remote environment. You do not need to specify extensions' dependencies, such as LLDB. |
| 38 | +* `settings`: Override any settings for extensions. The above example sets the LLDB path to stop the Swift extension from attempting to set it up. |
| 39 | +* `forwardPorts`: Ports to enable forwarding for. You may want to include this if building a Swift server application for example. |
| 40 | + |
| 41 | +That's all you need to get a dev container working! |
| 42 | + |
| 43 | +#### Using a custom Dockerfile |
| 44 | + |
| 45 | +You may want to use a custom Docker container that's version controlled to make it easy to set up a development environment for you team. In `devcontainer.json` replace `image` with the following: |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | + "build": { "dockerfile": "Dockerfile" }, |
| 50 | + // ... |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +This will use the `Dockerfile` provided in `.devcontainer`. Create that file and insert your custom Dockerfile. For example: |
| 55 | + |
| 56 | +```docker |
| 57 | +FROM swift:5.5 |
| 58 | +``` |
| 59 | + |
| 60 | +<!-- ### Automatic Setup |
| 61 | +
|
| 62 | +VSCode allows you to automatically configure your project with a dev container. In the command palette (`F1`) choose **Remote-Containers: Add Development Container Configuration Files...** and choose Swift. --> |
| 63 | + |
| 64 | +### Running in a container |
| 65 | + |
| 66 | +Once you've set up your `.devcontainer`, in the command palette run **Remote-Containers: Reopen in Container**. VSCode will relaunch running in your remote container! |
| 67 | + |
| 68 | +For more details about running your project in a remote container, and the available configuration options, see the [Visual Studio Code documentation](https://code.visualstudio.com/docs/remote/remote-overview). |
0 commit comments