[Introduction to Docker] ~ The shortest explanation until starting jupyter lab ~

Introduction

This article aims to help docker beginners get to the start of jupyter lab as quickly as possible. Deep talk of docker jargon such as container and image is skipped, so please google the part you care about.

If you read the article and found it helpful, I would appreciate it if you could press LGTM!

Premise

・ We carry out in mac environment

table of contents

  1. Overview of environment construction work
  2. docker installation
  3. Create Dockerfile
  4. Image creation
  5. Container creation
  6. Delete the environment

1. Overview of environment construction work

The basic flow of environment construction with Docker is as follows

  1. Creating a Dockerfile
  2. Creating an image
  3. Create & start container

To explain each in one word

the term Commentary Reference URL
Dockerfile Design document of the environment to be built
(You can see at a glance what kind of environment it is by looking at this file)
-
image Duplicate environment file with environment information
(You can create many environments with one image)
Tutorial aimed at understanding Docker images
container Environment instance generated from image
(You can set up or crush the environment with trial and error)
-

image.png Schematic diagram (https://docs.docker.jp/engine/understanding-docker.html)

2. docker installation

Register with docker hub and download the desktop version from "Get start with Docker Desktop" on the top page. https://hub.docker.com/ スクリーンショット 2020-11-22 21.09.33.png

3. Creating a Dockerfile

Create a docker directory on Desktop and create the following Dockerfile in it

Desktop/docker/Dockerfile


#Pull the image of anaconda3 from docker hub (https://hub.docker.com/r/continuumio/anaconda3)
#This image is the environment where anaconda3 is set up on ubuntuOS.
FROM continuumio/anaconda3:2020.07

#Make changes to jupyter's config file (without this setting, jupyter-lab will not start)
RUN jupyter notebook --generate-config
WORKDIR /root/.jupyter
RUN echo 'c.NotebookApp.allow_root = True' >> jupyter_notebook_config.py && \
    echo 'c.NotebookApp.ip = "0.0.0.0"' >> jupyter_notebook_config.py && \
    echo 'c.NotebookApp.token = "xxxx"' >> jupyter_notebook_config.py && \
    echo 'c.NotebookApp.port = 8888' >> jupyter_notebook_config.py && \
    echo 'c.NotebookApp.open_browser = False' >> jupyter_notebook_config.py
#Refer to the following URL for security settings
#https://jupyter-notebook.readthedocs.io/en/latest/security.html#server-security
#https://qiita.com/SaitoTsutomu/items/aee41edf1a990cad5be6

#Create a working directory
RUN mkdir -p /home/work
WORKDIR /home/work

#Set the default command (command executed when container is started)
CMD [ "jupyter", "lab"]

** Dockerfile instruction list **

order Commentary
FROM Pull image from docker hub
(If there is a corresponding image in local, pull will not be done)
RUN Execute linux command
(Execute the command in the environment of the pulled image to customize the environment)
WORKDIR Change the current directory
(Note that the move is temporary with the linux cd command.)
CMD Set default command
(Command executed when container is started)

There are not so many commands, so if you are interested, please check other ones as well. (http://docs.docker.jp/v17.06/engine/reference/builder.html)

4. Image creation

Execute the command as follows to create and check the image

image creation

#Move to the directory containing the Dockerfile
cd desktop/docker/Dockerfile
#Specify the current directory and build the image(-Specify the name of the image with the t option)
docker build . -t anaconda-env

Confirmation of created image

docker images
REPOSITORY      TAG       IMAGE ID            CREATED             SIZE
anaconda-env    latest    8aacbb4fe3dc        10 hours ago        2.76GB

If it is displayed as above, image creation is complete

5. Container creation

Finally, by creating a container, the creation of the jupyter-lab execution environment is complete! Enter the following commands in order

#Create a directory for saving on the local host side
mkdir volume
#Create a container
docker run -v $(pwd)/volume:/home/work -p 1111:8888 --name anaconda-container  -it anaconda-env

Option explanation

option Commentary
-v -Specify the save destination of some directories inside the container locally.
-In the above command, container:/home/Save to work is saved to host: volume
-Since the data will not be deleted even if the container is deleted, the source code and learning data will be managed in this directory.
・ Because it is necessary to specify with an absolute path$(pwd)Is assigning the current directory by
-p Represents the connection between localhost and the port in the container
(In the case of the above specification, when connecting to port 1111 of localhost, it connects to port 8888 in the container)
--name Decide the name of the container to generate
-it Magic
(-i:Accepts input from the command line,-t:A combination of these that cleans the command line display)

When you execute the above command, jupyter-lab will start, so please access the following URL http://localhost:1111

This time, the password is specified in Dockerfile 'xxxx' (If you want to know the details of how to set the password, please refer to the URL below). https://jupyter-notebook.readthedocs.io/en/latest/security.html#server-security

Login screen image.png

jupyter-lab screen image.png

At this point, docker has started jupyter-lab! Thank you for your hard work!


At the end of this chapter, check the status of the created container Return to the linux screen, and from the state where jupyter-lab is running as shown below, use ** "ctrl + p + q" ** to temporarily exit the container. (The container will not be stopped) image.png You can see that the container is started with the following command

#Check the status of container
docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
22093b0f68f1        anaconda-env        "jupyter lab"       25 minutes ago      Up 25 minutes       0.0.0.0:1111->8888/tcp   anaconda-container

By the way, you can stop and execute the container with the following command.

docker stop anaconda-container

#Stop container(Exited)confirm(-You can also check the stopped container with a)
docker ps -a
CONTAINER ID        IMAGE           COMMAND          CREATED             STATUS                      PORTS         NAMES
22093b0f68f1        anaconda-env    "jupyter lab"    37 minutes ago      Exited (0) 26 seconds ago                 anaconda-container

If you want to start it again, execute the following command

docker start anaconda-container
#juypter-When starting lab
docker exec -it anaconda-container jupyter lab
#When connecting to the CUI of the environment(If you want to do conda install etc.)
docker exec -it anaconda-container bash

6. Delete the environment

Follow the steps below to remove the container and image

#The container cannot be deleted without stopping
docker stop anaconda-container
#Delete container
docker rm anaconda-container
#Delete image
docker rmi anaconda-env

When creating an image with Dockerfile, chashe is left as a save point (chash is generated for each RUN instruction in Dockerfile) chashe is saved so that chas can be reused up to the save point even if there is a mistake when generating the Dockerfile. Don't forget to delete this as chashe may overwhelm the capacity without your knowledge.

#Delete cache
docker builder prune

finally

I've gone through all the steps so far, but I think I was able to build an environment unexpectedly smoothly. ?? Once you become a docker, you can automate troublesome environment construction with Dockerfile, and it is easy to migrate to other PCs and servers. It seems to be the standard for building an environment for data analysis / AI development and web development, so there should be no loss if you hold it down. I have skipped a lot of detailed explanations, so if you want to study comprehensively, please study with reference materials! (Especially the udemy course is recommended!)

If you read the article and found it helpful, I would appreciate it if you could press LGTM!

reference

・ Introduction to Docker https://datawokagaku.com/whatisdocker/ ・ Udemy course (recommended for those who want to study comprehensively) https://www.udemy.com/course/aidocker/

Recommended Posts

[Introduction to Docker] ~ The shortest explanation until starting jupyter lab ~
[GCP] Until you push the local Docker image to the Container Registry
Introduction to Linux Container / Docker (Part 1)
Introduction to Linux Container / Docker (Part 2)
Introduction to Docker (1) Frequently used commands
Introduction to Slay the Spire Mod Development (1) Introduction
Investigate the replacement from Docker to Podman.
Introduction to java for the first time # 2
Introduction to Ratpack (3) --hello world detailed explanation
Output of the book "Introduction to Java"
Introduction to Docker / Kubernetes Practical Container Development
[Docker] Build Jupyter Lab execution environment with Docker
Explanation until the original application is completed
[Introduction to Docker] Official Tutorial (Japanese translation)
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook