Getting Started with Docker with VS Code

Target

Docker installed Use Docker for the first time.

Premise

Command line available VS Code installed

About this article

In the general Docker learning flow, while learning the docker command, we will understand the concept of images and containers, and then proceed to docker-compose.

In this article, I will omit the explanation of concepts such as images and containers, and substitute commands with vscode extensions. By doing so, you will learn the docker-compose command at a minimum and move it quickly to understand Docker practically.

Goal of this article

You can now use some docker-compose commands. You will be able to create an execution environment for nodejs.

VS Code environment construction

スクリーンショット 2020-09-14 13.18.11.jpg

スクリーンショット 2020-09-13 19.36.04.png

Install this in advance.

Create a nodejs environment

First, create a folder called nodejssample in a suitable location. This name is appropriate.

Then, create a file called docker-compose.yml directly under that folder as shown below.

docker-compose.yml


version: "3"
services:
    node: 
        image: node:14.10
        volumes: 
            - .:/project
        tty: true
        working_dir: /project
        command: bash

This content will be explained later.

And create app.js in the same hierarchy

app.js


const main = () => {
    console.log("hello node!");
}

main();

スクリーンショット 2020-09-13 19.42.35.png

OK if this happens

Then check the current state of Docker once.

If you select the Docker mark from the left side of VS Code, you will see the screen below.

スクリーンショット 2020-09-13 19.43.38.png

This screen may not appear if Docker is not running or if you have already run Docker for some reason.

After confirming, transfer the command as follows at the location of docker-compose.yml on the command line.

Command to start virtual environment

docker-compose up -d

Then the download will start and the screen will look like the one below.

nodejssample $ docker-compose up -d
Creating network "nodejssample_default" with the default driver
4f250268ed6a: Pull complete1b49aa113642: Pull complete
c159512f4cc2: Pull complete8439168fd8dc: Pull complete55abbc6cc158: Pull completee5c5821cd889: Pull complete5a7679f70bad: Pull completed827e86d1182: Pull complete2484b06a6da1: Pull complete
Digest: sha256:21658666c0eabc9006b279e826e540d20e2c835507347d9c2f3f7dd5820ec9e3
Status: Downloaded newer image for node:14.10
Creating nodejssample_node_1 ... done

And the screen of the Docker tab is as follows. スクリーンショット 2020-09-13 19.52.05.jpg A virtual environment created with Docker is called a container. The image is for making a container.

スクリーンショット 2020-09-14 14.03.23.png

Now that the virtual environment has started, let's run nodejs in that virtual environment.

スクリーンショット 2020-09-13 19.57.17.png

Right-click on the target container> press Attack Shell. Then, the following screen will appear.

スクリーンショット 2020-09-13 20.03.04.png This is the state inside the container.

root@df80b994d93d:/project# ls
app.js  docker-compose.yml
root@df80b994d93d:/project#   

The same file as the VS Code project is displayed inside the ls command. And since this is a container of nodejs environment, check if nodejs can be used.

root@df80b994d93d:/project# node -v
v14.10.1

OK if this comes out

Execute the prepared app.js.

root@df80b994d93d:/project# node app.js
hello node!

I was able to execute it.

Erase the virtual environment.

The virtual environment is heavy if left unattended, so delete it when you are done.

スクリーンショット 2020-09-13 20.49.21.png

Erase the image.

For images, this makes docker-compose up -d faster, so if you use it often, you can leave it.

If you don't use it for a while, turn it off.

スクリーンショット 2020-09-13 20.50.28.png

Now you can build the virtual environment, execute the program in the virtual environment, and delete the virtual environment.

Commentary

About docker-compose.yml

The basic syntax of docker-compose is as follows

docker-compose syntax


version:Version number.
services:
Service name:
Service settings...

The latest version is 3 You can name the service yourself. Anything is OK.

docker-compose.yml


version: "3" #The latest version is 3
services: #This is fixed
    node: #Anything is fine, but since I am using the image of node, I just used the service name node.
        image: node:14.10
        volumes: 
            - .:/project
        tty: true
        working_dir: /project
        command: bash

There are many service settings, but it's best to read the official documentation for more information.

Reference

I will explain the setting items used this time one by one.

image Described the image for making a container.

.yml


image:Image name:Tag name

Tag name can be regarded as a version

There are various images used by Docker on a site called Docker Hub. node image

スクリーンショット 2020-09-14 0.06.37.jpg

This is the tag used in this article

volumes Link the local folder with the container folder.

.yml


        volumes: 
            -Local folder path:Container folder path

If you do not set this, you will not be able to see locally placed files such as app.js from the container.

tty

tty: true

Prevents the container from stopping immediately after performing the process specified by command. Required when entering a container with Attach Shell.

working_dir That's still the place to run the command. When you perform Attach Shell, it will also be in the folder specified here.

command Process to be executed when the container is started. This time I specified bash, so it feels like opening the terminal and waiting.

There are various other settings, so if you see another Docker article and find something you don't know, you should read the reference.

About docker-compose up -d

Reference

A command to read docker-compose.yml, download an image if necessary and create a container.

-d is an option to start in the background.

Recommended Posts

Getting Started with Docker with VS Code
Getting Started with Docker
Docker management with VS Code
[Google Cloud] Getting Started with Docker
Getting Started with Docker for Mac (Installation)
Lombok with VS Code
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Doma-Transactions
Getting Started with Java Collection
Run VS Code on Docker
Format Ruby with VS Code
Hello World with VS Code!
Getting Started with JSP & Servlet
Getting Started with Ruby Modules
Build Java development environment with WSL2 Docker VS Code
Proceed with Rust official documentation on Docker container (1. Getting started)
Getting Started with Java_Chapter 5_Practice Exercises 5_4
Spring Boot programming with VS Code
Java build with mac vs code
Getting started with Java lambda expressions
Getting Started with GitHub Container Registry instead of Docker Hub
Summary of steps for developing in Docker container with VS Code
Building a haskell environment with Docker + VS Code on Windows 10 Home
Getting Started with Doma-Criteria API Cheat Sheet
Prepare Java development environment with VS Code
Getting Started with Ruby for Java Engineers
Getting Started with Parameterization Testing in JUnit
Try using Spring Boot with VS Code
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Creating Resource Bundles with ListResoueceBundle
Experience .NET 5 with Docker and Visual Studio Code
Getting Started with Java_Chapter 8_About Instances and Classes
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Using Gradle with VS Code, build Java → run
Getting started with Kotlin to send to Java developers
Maybe it works! Let's get started with Docker!
Getting Started with Doma-Using Joins with the Criteira API
Create Spring Boot environment with Windows + VS Code
Getting Started with Doma-Introduction to the Criteria API
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
I tried Getting Started with Gradle on Heroku
Connect with VS Code from a Windows client to Docker on another server
Build Java development environment with VS Code on Mac
Link Docker log to AWS CloudWatch and monitor in real time with VS Code
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Build ruby debug environment with VS Code of Windows 10