{"id":9082,"date":"2024-02-08T10:07:21","date_gmt":"2024-02-08T10:07:21","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-manage-docker-using-vs-code\/"},"modified":"2025-01-20T10:15:15","modified_gmt":"2025-01-20T10:15:15","slug":"how-to-manage-docker-using-vs-code","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/how-to-manage-docker-using-vs-code\/","title":{"rendered":"How to Manage Docker Using VS Code"},"content":{"rendered":"<p><div>You can manage Docker images and containers directly within Visual Studio Code (VS Code) with Microsoft&#8217;s Docker extension. This article walks you through adding Docker files to a Workspace, creating a containerized application environment, and explains how to build and run Docker containers directly from the VS Code interface.<\/div>\n<\/p>\n<p><h2>Prerequisites<\/h2>\n<\/p>\n<p><p>To follow this guide, you only need VS Code and Docker installed on your PC or Mac. If you work with Linux, you also must install the <a href=\"https:\/\/docs.docker.com\/compose\/install\/linux\/\" rel=\"nofollow noopener\" target=\"_blank\">Docker Compose plugin<\/a>.<\/p>\n<\/p>\n<p><h2>Install Docker extension<\/h2>\n<\/p>\n<p><p>Begin by adding the Docker extension to your VS Code which is provided by Microsoft. You can do this by moving to the Extensions view. The shortcuts are Ctrl+Shift+X for Windows users and Cmd+Shift+X for those using macOS. Once you&#8217;re there, search for the Docker extension and install it.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Install-the-Docker-extension-in-VS-Code.png\" rel=\"nofollow noopener\" target=\"_blank\">Install the Docker extension in VS Code<\/a><\/p>\n<p><h2>Create a Project<\/h2>\n<\/p>\n<p><p>Next, create a directory for your demo project to follow along with this blog. Proceed to add an <em>index.php<\/em> file and copy the following code:<\/p>\n<\/p>\n<p>&lt;p&gt;Welcome, John Doe&lt;\/p&gt;<\/p>\n<p>&lt;h2&gt;Add Docker files to the Workspace&lt;\/h2&gt;<\/p>\n<p>&lt;p&gt;Add essential Docker files (e.g., Dockerfile, docker-compose.yml, and .dockerignore) to your project. For those who don&#8217;t know, Dockerfile defines the instructions for building a container image for your app. The docker-compose.yml file defines and runs multi-container Docker applications. The .dockerignore file specifies which files and directories should be excluded when creating the container image.&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;To add the Docker files to your project, follow these steps:&lt;\/p&gt;<\/p>\n<ol>\n<li>Invoke the command palette of VS Code by pressing the Ctrl+Shift+P keys (CMD+Shift+P on Mac), then type the <em>Docker: Add Docker Files to the Workspace<\/em>, and hit Enter.<\/li>\n<li>When prompted, select your application platform from the list. I am using PHP, so I chose <em>Other<\/em>.<\/li>\n<li>When prompted to include the optional Docker compose files, select <em>Yes<\/em> or <em>No<\/em>, depending on your requirement. I will choose <em>Yes<\/em>.<\/li>\n<li>You will see the new Docker files added to your project. Now, open the <em>Dockerfile<\/em> in VS Code and paste this code:<\/p>\n<p><pre><\/p><p>FROM php:8.2-apache<\/p><p>\t\tCOPY . \/var\/www\/html\/<\/p><p>\t\tWORKDIR \/var\/www\/html\/<\/p><p>\t\tCMD [\"apache2-foreground\"]<\/p><p>    <\/pre>\n<\/p>\n<p><p>Here is an explanation of the <em>Dockerfile<\/em>:<\/p>\n<\/p>\n<ul>\n<li>The FROM php:8.2-apache command specifies the base image for the container, which is the official PHP 8.2 Apache image.<\/li>\n<li>The COPY . \/var\/www\/html\/ command copies the contents of the current directory to the \/var\/www\/html\/ directory in the container.<\/li>\n<li>The WORKDIR \/var\/www\/html\/ command sets the working directory to \/var\/www\/html\/.<\/li>\n<li>The CMD [&#8220;apache2-foreground&#8221;] command sets the command to run the Apache web server in the foreground.<\/li>\n<\/ul>\n<p><p>You can also invoke IntelliSense by pressing the <em>Ctrl+Space<\/em> keys while editing these Docker files, which is handy if you are new to Docker development.<\/p>\n<\/p>\n<\/li>\n<li>Next, from the VS Code Explorer open the <em>docker-compose.yml<\/em> file and paste this code:<\/p>\n<p><pre><\/p><p>version: '3.1'<\/p><p>\t\tservices:<\/p><p>\t\t  php:<\/p><p>\t\t    container_name: php-app<\/p><p>\t\t    build: <\/p><p>\t\t      context: .<\/p><p>\t\t      dockerfile: Dockerfile<\/p><p>\t\t    ports:<\/p><p>\t\t      - 80:80<\/p><p>    <\/pre>\n<\/p>\n<p>    This Docker compose file will help you build and run your app in a container directly from VS Code GUI, so you don&#8217;t need to type any commands manually. Let&#8217;s quickly understand the <em>docker-compose.yml<\/em> file.<\/p>\n<ul>\n<li>In the <em>services<\/em> section, you can define multiple containers for your application. In this case, we declared a single service named <em>php<\/em>.<\/li>\n<li>The <em>build<\/em> section defines how to build the container image. The <em>context<\/em> specifies the path to the build context, which is the current directory represented by a period (.) in VS Code, and the <em>dockerfile<\/em> specifies the name of the Dockerfile to use.<\/li>\n<li>The <em>ports<\/em> field maps port 80 of the container to port 80 of the host, which makes the containerized application accessible at <a href=\"http:\/\/localhost\/\" rel=\"nofollow noopener\" target=\"_blank\">http:\/\/localhost\/<\/a> URL on the Docker host itself.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h2>Run the container<\/h2>\n<\/p>\n<p><p>Make sure the Docker service is running on your computer. To start your container, right-click the <em>docker-compose.yml<\/em> file and choose the <em>Compose Up<\/em> command from the context menu. This command will build, create, start, and attach to containers for the service defined in your docker-compose.yml.<\/p>\n<\/p>\n<p><p>The context menu also provides additional options such as <em>Compose Down<\/em> (which halts the container), <em>Compose Restart<\/em> (which restarts the\tcontainer), and <em>Compose Up &#8211; Select Services<\/em> (which launches a specific service when there are multiple services in the Docker compose file). The output of tasks run when any context menu option is chosen is shown in the integrated terminal.<\/p>\n<\/p>\n<p><p>When the container started message appears in the terminal, your web application is accessible via the http:\/\/localhost URL.<\/p>\n<p>Your running container can then be viewed in Docker Desktop.<\/p>\n<\/p>\n<p><div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Viewing-containers-in-Docker-Desktop.png\" rel=\"nofollow noopener\" target=\"_blank\">Viewing containers in Docker Desktop<\/a><\/p>\n<p>Viewing containers in Docker Desktop<\/p>\n<\/div>\n<p><h2>Run Docker commands<\/h2>\n<\/p>\n<p><p>You can manually run the Docker commands in the integrated terminal if you don&#8217;t want to use Docker Compose. For example, to build a container image for your app and run it, use these commands in the integrated terminal:<\/p>\n<\/p>\n<p><pre><\/p><p>docker build --tag app .<\/p><p>docker run --detach --publish 80:80 app<\/p><p><\/pre>\n<\/p>\n<p><p>Furthermore, the command palette in VS Code allows you to run most of the Docker commands. Suppose you want to inspect the running container. To do that, invoke the command palette, then start typing the <em>Docker inspect<\/em> keyword in the command box. You will see the list of available commands. Select the <em>Docker Containers: Inspect<\/em> command, as shown in the screenshot.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Running-Docker-commands-with-Visual-Studio&#039;s-command-palette.png\" rel=\"nofollow noopener\" target=\"_blank\">Running Docker commands with Visual Studio&#8217;s command palette<\/a><\/p>\n<p><p>Select the app and then app-php when prompted, and you will see detailed information about the container.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Inspecting-a-Docker-container-in-VS-Code.png\" rel=\"nofollow noopener\" target=\"_blank\">Inspecting a Docker container in VS Code<\/a><\/p>\n<p><h2>Docker Explorer view<\/h2>\n<\/p>\n<p><p>The Docker extension adds a Docker Explorer view with separate panes in VS Code. In this view, you can manage various Docker assets such as containers, images, networks, and volumes.<\/p>\n<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/02\/Docker-Explorer-panes-in-VS-Code.png\" rel=\"nofollow noopener\" target=\"_blank\">Docker Explorer panes in VS Code<\/a><\/p>\n<p><p>The Docker VS Code extension features three central panes:<\/p>\n<\/p>\n<ul>\n<li><strong>Containers<\/strong> pane shows all the running containers on the Docker host. Click <em>CONTAINERS <\/em>&gt; app<em> and the right-click <\/em>app-php<em>. The context menu allows you to perform various operations on the container, such as View Logs, Attach Shell, Inspect, Open in Browser, Stop, Restart, and Remove.<\/li>\n<li><strong>Images<\/strong> pane displays all container images on your Docker host. The context menu gives various commands to run, inspect, pull, push, tag, and remove the container image.<\/li>\n<li><strong>Registries<\/strong> pane &#8211; In the Registries pane, you can connect to container registries, such as Docker Hub, GitHub, Azure, etc. Once connected, you can pull or push container images seamlessly straight from VS Code.<\/li>\n<li><strong>Networks<\/strong> pane allows you to manage and inspect <a href=\"https:\/\/4sysops.com\/archives\/docker-networking-connect-a-docker-container\/\" rel=\"nofollow noopener\" target=\"_blank\">Docker networks<\/a>.<\/li>\n<li><strong>Volumes <\/strong>pane &#8211; It shows all the <a href=\"https:\/\/4sysops.com\/archives\/introduction-to-docker-bind-mounts-and-volumes\/\" rel=\"nofollow noopener\" target=\"_blank\">Docker volumes<\/a>, and you can prune or remove unnecessary volumes directly from the VS Code.<\/li>\n<\/ul>\n<p><h2>Conclusion<\/h2><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can manage Docker images and containers directly within Visual Studio Code (VS Code) with Microsoft&#8217;s Docker extension. This article walks you through adding Docker files to a Workspace, creating a containerized application environment, and explains how to build and run Docker containers directly from the VS Code interface. Prerequisites To follow this guide, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9083,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,95,110,106,158],"tags":[],"class_list":["post-9082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-devops","category-docker","category-virtualization","category-vscode"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=9082"}],"version-history":[{"count":2,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9082\/revisions"}],"predecessor-version":[{"id":10345,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/9082\/revisions\/10345"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/9083"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=9082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=9082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=9082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}