Since I built a container that uses the Python environment using Jupyterlab, I will make a note of the procedure at that time.
[Docker installation environment] ・ Ubuntu 18.04 LTS (on GCP) ・ Docker 19.03.13
** Create a VM instance on GCP **
** Install docker ** [Systemctl cannot be used on Ubuntu in Docker container] Refer to step 1.
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.
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
Tera Term
etc. to generate the key. This time it is assumed to be accessed as the root user.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.
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