How to deploy a Docker Container with VS Code
VS Code is one of the most popular open-source IDEs on the planet. It can work with a large number of languages, is quite user-friendly and available for Linux, macOS and Windows. Another very nice feature of the app is that it includes a vast array of extensions that add several extra abilities, such as working with containers.
With the help of a Docker extension, you can create containers, build images, deploy apps and more.
I’m going to show you how to make that happen, using VS Code on Pop!_OS Linux. You can do this on any platform that has Docker installed. I’ll walk you through the installation of both VS Code and Docker on my Ubuntu-based OS of choice. If you use a different platform, you’ll need to modify the installation process accordingly.
SEE: Hiring kit: Back-end Developer (TechRepublic Premium)
What you’ll need to deploy Docker
To get started with Docker on VS Code, you’ll need a running instance of an operating system that supports both and a user with admin privileges.
How to install Docker
The first thing we’ll do is install Docker.
Download and install the necessary GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Now, add the official Docker repository:
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Install the necessary dependencies:
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
Finally, we can install the latest version of the Docker engine:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
When the installation completes, add your user to the docker group with:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect.
How to install VS Code
To install VS Code, head over to the official download page, open a terminal and run the following command:
sudo dpkg -i ~/Downloads/code*.deb
When the installation completes, open VS Code from your desktop menu.
How to install the Docker extension
Upon the first run of VS Code, it should detect that Docker is installed and ask if you want to install the Docker extension. If so, click install and you’re good to go. If not, click the gear icon near the bottom left of the VS Code window, and in the search field, type Docker. When the Docker extension appears, click the associated Install button (Figure A).
Figure A
After the extension installs, you should see any containers, images, and registries you’ve already created listed in the left pane.
How to deploy a container
In the VS Code window, click Terminal | New Terminal. With the terminal window open, let’s pull and run the getting-started Docker container with:
docker run -d -p 80:80 docker/getting-started
If port 80 is already taken, you can deploy it on external port 8080 with:
docker run -d -p 8080:80 docker/getting-started
Once the container has been deployed, you’ll see it listed under the CONTAINERS section. If you right-click that entry, you can take a number of actions such as View Logs, Attach Shell, Attach Visual Studio Code, Inspect, Open In Browser, Stop, Restart and Remove (Figure B).
Figure B
If you expand the entries for the Getting Started container under CONTAINERS, you can right-click any of those files and open them in VS Code for editing (Figure C).
Figure C
Let’s say you’ve pulled down an image and want to run a container from that. Right-click the image from the IMAGES section and click Run.
This will deploy a container, based on the image, with the standard options. For example, if I click Run for the etherpad image, the command VS Code will execute is:
docker run --rm -d -p 9001:9001/tcp etherpad/etherpad:latest
You should fairly quickly see the etherpad container running under CONTAINERS, where you can right-click the entry and then click Open In Browser to experience the now running container.
And that’s pretty much the gist of deploying a container within VS Code. There’s so much you can do with these two tools together and this barely scratches the surface. But for those who like a traditional IDE for use within the realm of containers, this is a great combination.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
For all the latest Technology News Click Here
For the latest news and updates, follow us on Google News.