[DOCKER] python & jupyterlab container construction

Introduction

Since I built a container that uses the Python environment using Jupyterlab, I will make a note of the procedure at that time.

Execution environment

[Docker installation environment] ・ Ubuntu 18.04 LTS (on GCP) ・ Docker 19.03.13

procedure

    1. Environmental preparation
    1. Build a python container
    1. Added SSH server function
  1. Simultaneous launch of Juypterlab and SSH

1. 1. Environmental preparation

** Create a VM instance on GCP **

** Install docker ** [Systemctl cannot be used on Ubuntu in Docker container] Refer to step 1.

2. 2. Container construction

Get base image

python


$ docker image pull python:3

Create a container

python


$ docker container run -it -d --name python-con1 python:3

Go inside the booting container and install some packages

python


$ docker container exec -it python-con1 /bin/bash

Commands to execute in the container


#Install package
pip install numpy pandas jupyterlab

#Get out of the container
exit

Create modified container image

python


#Stop container
$ docker container stop python-con1

#Image the current container
$ docker container commit python-con1 python-img:ver1

Confirm that the image has been created

python


$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
python-img   ver1      34ad01ba7efb   41 seconds ago   1.19GB
python       3         d1eef6fb8dbe   2 weeks ago      885MB

Create a container again from the created image.

python


$ docker container run -it -d \
-p 8888:8888 \
#-v
--name python-con2 python-img:ver1
# jupyter lab --ip=0.0.0.0 --allow-root --LabApp.token=""

Enter the container and start Juypterlab.

python


$ docker container exec -it python-con2 /bin/bash

Commands to execute in the container


jupyter lab --ip=0.0.0.0 --allow-root --LabApp.token=""

If you access port 8888 on the host side in this state and juypterlab is opened, there is no problem.

3. 3. Added SSH server function

Install an SSH server so that you can operate directly on each container, and make sure that you can connect to SSH.

python


#Container creation
$ docker container run -it -d --name python-con2 python-img:ver1

#Enter the container
$ docker container exec -it python-con2 /bin/bash 

Commands to execute in the container


apt update
apt install -y openssh-server
apt install nano

Create the folder required to start the service and register the common key

Commands to execute in the container


mkdir /var/run/sshd
mkdir /root/.ssh/
nano /root/.ssh/authorized_keys
#The contents of the key are the public key[.pub]Copy the contents of the file

Create modified container image

python


#Stop container
$ docker container stop python-con2

#Image the current container
$ docker container commit python-con2 python-img:ver2

Confirm that the image has been created

python


$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
python-img   ver1      34ad01ba7efb   41 seconds ago   1.19GB
python       3         d1eef6fb8dbe   2 weeks ago      885MB

Create a container again from the created image.

python


$ docker container run -it -d \
-p 8888:8888 \
-p 20022:22 \
--name python-con3 python-img:ver2 \
/usr/sbin/sshd -D

In this state, if you can SSH to the 20022 port of the docker host from Tera Term etc. of the local PC, it is working normally.

4. Simultaneous launch of Juypterlab and SSH

Create an sh file that starts up ** JuypterLab ** and ** SSH server **, and execute the sh file with the CMD overwrite command when creating the container to automatically service each time the container is restarted. I wanted to launch it, but it didn't work. The cause is that the container will drop when the execution of the sh file itself is completed. I also tried an sh file that loops infinitely, but it didn't work in the end, so I decided to use another method.

First, create a container with the following command so that the SSH service will start when the container starts.

python


docker container run -it -d \
-p 8888:8888 \
-p 20022:22 \
--name [arbitrary container name] python-img:ver2 \
/usr/sbin/sshd -D

Then, connect to SSH and execute the following command.

python


jupyter lab --ip=0.0.0.0 --allow-root --LabApp.token=""

Recommended Posts

python & jupyterlab container construction
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
python windows environment construction
homebrew python environment construction
Python development environment construction
python2.7 development environment construction
Mac environment construction Python
Python environment construction @ Win7
Python + Anaconda + Pycharm environment construction
My python data analysis container
Web scraping with python + JupyterLab
Python environment construction (Windows10 + Emacs)
CI environment construction ~ Python edition ~
Python environment construction For Mac
Anaconda3 python environment construction procedure
Python3 environment construction (for beginners)
Python environment construction under Windows7 environment
[MEMO] [Development environment construction] Python
Environment construction of python2 & 3 (OSX)
Environment construction of python and opencv
Python environment construction memo on Windows 10
Get started with Python! ~ ① Environment construction ~
Python
Anaconda python environment construction on Windows 10
Python + Unity Reinforcement learning environment construction
Create a DI Container in Python
I checked Mac Python environment construction
Python environment construction memo on Mac
Python environment construction (pyenv, anaconda, tensorflow)
[Python3] Development environment construction << Windows edition >>
Python development environment construction on macOS
Recommended container image for Python applications
Python environment construction (pyenv + poetry + pipx)
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Python3 TensorFlow for Mac environment construction
Emacs Python development environment construction memo
pytorch @ python3.8 environment construction with pipenv
[docker] python3.5 + numpy + matplotlib environment construction
Python3.6 environment construction (using Win environment Anaconda)
OpenCV3 & Python3 environment construction on Ubuntu
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Python (anaconda) development environment construction procedure (SpringToolsSuites) _2020.4
Python project environment construction procedure (for windows)
[Python] Road to snake charmer (1) Environment construction
Python3 + venv + VSCode + macOS development environment construction
VScode environment construction (Windows10, Python, C ++, C, Git)
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Python and machine learning environment construction (macOS)
Python execution server construction (Python + uWSGI + Django + Nginx)
Python development server construction procedure (AWS + Anaconda)
[Python] Web application from 0! Hands-on (0) -Environmental construction-
Happy GUI construction with electron and python
Let's get along with Python # 0 (Environment construction)
Windows + gVim + Poetry python development environment construction