Follow

Follow
Run Docker container with desktop environment.

Run Docker container with desktop environment.

Abhishek Singh's photo
Abhishek Singh
·Dec 12, 2022·

3 min read

Table of contents

  • Introduction
  • Requirement
  • Configuration
  • Building Image
  • Running Container

Introduction

If you need to run light-weight development environment for quick code testing, you can use below mentioned methods to launch a docker based GUI container with custom configurations.

In this tutorial we are using kasm-workspace image for base.

Requirement

  • Running docker environment.
  • Browser Firefox/Chrome

Configuration

First of all we will build our docker image using kasm-workspace image. So you can copy example Dockerfile from here

Building Image

Sample Dockerfile copied from Kasm-

FROM kasmweb/core-ubuntu-focal:1.12.0
USER root

ENV HOME /home/kasm-default-profile
ENV STARTUPDIR /dockerstartup
ENV INST_SCRIPTS $STARTUPDIR/install
WORKDIR $HOME

######### Customize Container Here ###########

# add custom configs here

######### End Customizations ###########

RUN chown 1000:0 $HOME
RUN $STARTUPDIR/set_user_permission.sh $HOME

ENV HOME /home/kasm-user
WORKDIR $HOME
RUN mkdir -p $HOME && chown -R 1000:0 $HOME

USER 1000

In the above docker file don't change top and bottom lines just add your commands which you want to run in the mentioned section.

Here I am going to install python3, sublime text, and Hyperjs terminal.

After adding all the commands dockerfile will look like this -

FROM kasmweb/core-ubuntu-focal:1.12.0-rolling
LABEL maintainer="https://github.com/asabhi6776"
USER root

ENV HOME /home/kasm-default-profile
ENV STARTUPDIR /dockerstartup
ENV INST_SCRIPTS $STARTUPDIR/install
WORKDIR $HOME

######### Customize Container Here ###########


COPY asset/wallpaper-custom.jpg /usr/share/extra/backgrounds/bg_default.png

RUN apt update ; apt upgrade -y
RUN apt install wget curl git -y
RUN apt install python3 python3-dev build-essential -y
RUN wget -O hyper_3.2.3_amd64.deb https://releases.hyper.is/download/deb
RUN apt install ./hyper_3.2.3_amd64.deb -y

RUN apt-get update \
    && apt-get install -y sudo \
    && echo 'kasm-user ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers \
    && rm -rf /var/lib/apt/list/*

RUN  wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | apt-key add - \
    && apt-get update \
    && apt-get install -y apt-transport-https \
    && echo "deb https://download.sublimetext.com/ apt/stable/" |  tee /etc/apt/sources.list.d/sublime-text.list \
    && apt-get update \
    && apt-get install sublime-text \
    && cp /usr/share/applications/sublime_text.desktop $HOME/Desktop/ \
    && chmod +x $HOME/Desktop/sublime_text.desktop \
    && chown 1000:1000 $HOME/Desktop/sublime_text.desktop

RUN mkdir -p $HOME/project
RUN chmod -R 777 $HOME/project


######### End Customizations ###########

RUN chown 1000:0 $HOME
RUN $STARTUPDIR/set_user_permission.sh $HOME

ENV HOME /home/kasm-user
WORKDIR $HOME
RUN mkdir -p $HOME && chown -R 1000:0 $HOME

USER 1000

After adding all the customization build the docker image using following command-

docker build -t username/imagename:tagname .

Now you can push docker image to your docker hub account using following command-

docker push username/imagename:tagname

Running Container

Now by using this image we can run our command with following command-

docker run -it -d -p 6901:6901 -e VNC_PW 'Password' username/imagename:tagname

Or We can create docker compose file and run them using docker-compose.

version: '3'
services:
    dev_container:
        container_name: dev_container
        # you can find other tags in docker hub
        image: username/imagename:tagname
        restart: always
        command: "--shm-size=512m"
        environment:
        #username will be kasm_user
            VNC_PW: password
        # change port as per your need
        ports:
            - "6901:6901"

And now run following command to start the container-

docker-compose up -d

After starting the container open browser and access the container UI using localhost:6901

Default username - kasm_user

Password - Mentioned in ENV

If you want to use my code you can clone repo by using following command -

git clone https://github.com/asabhi6776/dev-container.git

Now enjoy.

If you have any doubt please comment below.

Did you find this article valuable?

Support Abhishek Singh by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this