Docker in itself is a powerful tool. The Docker ecosystem has considerably matured since Docker launched six years ago. We now have tools for CI/CD, Security, Monitoring, Logging, and so much more. However, one area which was still not as mature as the rest of the Docker ecosystem was the integrated development environments (IDE).
For the longest time, I was using plain VIM and Bash to handle all my Docker tasks. From writing Docker & Compose files, build, run, and test images. VIM and Bash did a great job, but I didn't realize what I was missing out on and that a more natural workflow was available.
Enter Microsoft VS Code
Microsoft Visual Studio Code (VS Code) hit the development world by storm. The General availability release launched on April 14, 2016. Less than two short years later the 2018 Stack Overflow survey listed VS Code as the most popular Developer environment tool at 34.9% usage. That is incredible! Availability across OS platforms Windows, Mac, and Linux has helped the adoption and the ecosystem.
I first switched to Microsoft VS Code about one year ago. A random stranger on my train journey home told me I should give it a try as he was watching me code. I installed the VS Code base configuration and added extensions for Docker, Kubernetes, Terraform, and quite some others to get started. Like myself, I found that most users I speak with are using VS Code only for code editing and Git management. VS Code does this well, but we missed the power of VS Code.
"With Great power comes great responsibility." Ben Parker, Spiderman
Like typical techies, we install applications and never bother to read the documentation for each extension because:
A) It works
B) It solves my immediate problem
But VS Code is so much more. I discovered the real potential of VS Code with Docker when I ran into a weird bug. The research of GitHub issues led me down the VS Code rabbit hole of features which shocked and embarrassed me at the same time that I haven't been using these features all along. After asking several colleagues if they use the Docker Extension the way it is intended resulted in ZERO people using it this way. On the one hand, I am happy I was not alone but why are so many people not using the full features of the extensions as intended?
VS Code and Docker
To use the Docker extension, you should install Docker for Desktop on Linux, Mac, or Windows . Next, install the Docker VS Code extension.
- On the left side of VS Code click the Extension icon.
- Next, search for official Docker extension containing the Green star and click install
We can adjust the Docker Extension default settings by navigating to Settings (The gear icon in the lower left corner) - > Extensions - > Docker configuration. In this menu, we can set the defaults for the default registry, what interface to use when connecting to containers, defaults for the build and run commands, and Docker Host if you want to connect to a docker daemon other than what is running locally.
New Docker Projects
One of the most brilliant features is the creation of new Docker projects. VS Code automatically creates Dockerfiles and copies in files from your project directory. Depending on the language Dockerfile you select it will even create Multi-stage builds as well. It saves a considerable amount of time bootstrapping your new projects with Docker.
- Open Command Palette Mac (⇧⌘P) or Windows (CTRL + Shift + P)
- Select the language your application requires (GO, .Net Core, Java, etc.)
- Select which port your application will listen on (defaults to 3000)
Writing Dockerfiles with Code Completion
The Dockerfile command snippet also makes writing Dockerfiles easier. It allows a quick search of Dockerfile commands and includes the example syntax and a link to the exact section in the Docker documentation for further details. The Dockerfile code completion is really decreases the amount of time to write a Dockerfile and the documentation is built right in.
- Create a file name Dockerfile
- Open Dockerfile Command snippets Mac (^ + Space) or Windows (Windows key + Space)
- Search for the Dockerfile command to insert
- Click the
ibutton next to the command for documentation and link to the Docker documentation
Enterto insert the command and
Tabto toggle between the fields of the command
Once we have docker-compose or Dockerfiles in place, we can start interacting with them. This is where the fun begins.
- Open the command palette Mac (⇧⌘P) or Windows (CTRL + Shift + P)
Docker Build image
- Provide the Docker image tag.
- Enter will then start the Docker image build
Run Docker & docker-compose
Now that we have images built we can also run them. This works for standalone Dockerfiles or a docker-compose stack. What is quite nifty and something I learned also is when running a docker-compose stack, it actually combines the run statement with a build to ensure it gets the latest versions. Slick.
docker-compose -f "docker-compose.yml" up -d --build
Docker Compose Up
- Provide the Docker image tag.
iis:latestor compose file
Enterwill then run your Dockerfile or docker-compose.
Interact with Docker images & containers
Once we have containers that are running, we can interact with them directly with VS Code. Click on the Docker Plugin logo on the left taskbar. Additional options now; Images, Containers, and Registries. Expanding Images shows all images available locally, containers show all running and stopped containers, and registries indicate which are available to use.
VS Code summary
VS Code in itself is a fantastic tool. When we expand and utilize some of the plugins like Docker, we can take our projects and Docker skills to the next level. What brings me the most value is less context switching between different interfaces. I can now stay on one screen and write Dockerfiles, build, ship, and run them all from VS Code.