Skip to content

Submodule for development in VSCode 'Remote - Containers'

License

Notifications You must be signed in to change notification settings

NoUnique/.devcontainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Visual Studio Code - DevContainer

English | 한국어

Descriptions

Visual Studio Code Remote - Containers extension lets us use a Docker container as a full-featured development environment.

It is very complicated to match and update the dependencies between the different components needed to develop the program.
If you store a well-organized development environment separately and call the environment when necessary, it is very easy to reproduce the development environment or share the development environment among various developers.

This document describes how to save the above development environment as an image using Docker, Docker Compose, and Visual Studio Code and call and use it as a container as needed.

In the Windows environment without Docker Desktop, CLI Script can be an alternative.


Requirements

Docker, Docker Compose, Visual Studio Code, and Remote - Containers (extension of VSCode) are should be installed on your computer. (Linux PC)
(For Windows PCs, Visual Studio Code must be installed, and Linux PCs with Docker and Docker Compose is required)


Components

The following three files are usually required to run DevContainer.

Dockerfile

Dockerfile is a file for building docker images.
An image made from the script is used to run a container.

The file is not required when using the docker image that has already been created. Originally, the name of the script is usually 'Dockerfile', but here it is used separately as follows to distinguish between the development image and the release image.

docker-compose.yml

docker-compose.yml file is a setting file for driving Docker Compose. The YAML file with .yml and .yaml extensions is a file format with a key-value structure that is often used to create settings, and is used to display multiple containers at the same time or to store and use the Docker argument required to drive containers.

devcontainer.json

devcontainer.json file is a setting file for DevContainer operation of Visual Studio Code. Here, you can decide whether to use Dockerfile only when running the container, Docker-compose, workspace path inside the container, which shell to use, and which formatter or extension to use as a default.


Run DevContainer (Linux OS)

Once all of the above settings are complete, development can begin inside the container following the following description.

https://code.visualstudio.com/docs/remote/containers


Run DevContainer (Windows OS)

There are many restrictions on configuring the development environment using Windows OS.
By combining WSL and Docker Desktop, Windows PCs were able to establish a development environment almost similar to Linux PCs, but it is still difficult to configure GPU development environment through that method.

Although all developers can use Docker Desktop and WSL in Windows PCs, but it is not recommended due to various limitations and difficulty in debugging.
This document only deals with how to run a container on a Linux PC and then connect it remotely(Remote - SSH) using Windows PC to develop codes.

CLI (compose.sh)

This is a CLI script that makes it easy to use frequently used commands without typing the entire command of Docker Compose.
This script creates and uses/removes only one container for convenience of development.

  • Basic Usage

    $ .devcontainer/compose.sh -b
      -b : build an image  
      -r : run a container  
      -l : print a log of the container(force log)
      -s : connect to shell(bash)  
      -k : kill container (attach and kill)  
      -d : down container (kill container and remove container, network and volumes)  
      -p : push image to remote repository  
      --service=? : specify service name of docker-compose to run  
      --tag=? : tag image version  
      --release : use image for release  
      --no-cache : build an image without caching
      
      This script makes ONLY 1 CONTAINER
  • Connect to the Shell of the Container
    (Build automatically when there is no image, run automatically when there is no container)

    $ .devcontainer/compose.sh -s
  • Connect to a container of another service

    $ .devcontainer/compose.sh -s --service=new_service
  • Connec to a container for release

    $ .devcontainer/compose.sh -s --release
  • Tag and push to remote repository

    $ .devcontainer/compose.sh --tag=v1.0.1-a0 --service=release -p

Remote - SSH

Use the above script to run the container on the Linux PC and then connect remotely through 'Remote-SSH' in VSCode.

Here, the port to be used for SSH(Secure Shell) is should be specified in docker-compose.yml.

docker-compose.yml

services:
  dev:
    build:
      network: host
      context: .
      dockerfile: dev.Dockerfile
    image: "${COMPOSE_IMAGE_NAME}:${USER}"
    hostname: ${COMPOSE_IMAGE_NAME}
    container_name: ${COMPOSE_IMAGE_NAME}_dev
      - '922:22'
    volumes:
      - .:/app/${COMPOSE_IMAGE_NAME}:rw
      - /var/run/docker.sock:/var/run/docker.sock  # for docker-in-docker

In addition, the user account to be used for SSH(Secure Shell) should be a default user of the image or created when the image was built.

services:
  dev:
    build:
      network: host
      context: .
      dockerfile: dev.Dockerfile
      args:
        USER: ${USER}
        PUID: ${PUID}
        PGID: ${PGID}
        DOCKER_GID: ${DOCKER_GID}
        LC: ko_KR.UTF-8
        TZ: Asia/Seoul
        COMPOSE_IMAGE_NAME: ${COMPOSE_IMAGE_NAME}
    image: "${COMPOSE_IMAGE_NAME}:${USER}"
    hostname: ${COMPOSE_IMAGE_NAME}
    container_name: ${COMPOSE_IMAGE_NAME}_dev
    user: ${USER}
    environment:
      - USER=${USER}
      - PUID=${PUID}
      - PGID=${PGID}
      - DOCKER_GID=${DOCKER_GID}
      - COMPOSE_IMAGE_NAME=${COMPOSE_IMAGE_NAME}

After running the container set as above, connect to the container through 'Remote - SSH' to proceed with development.
https://code.visualstudio.com/docs/remote/ssh

About

Submodule for development in VSCode 'Remote - Containers'

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages