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!
・ We carry out in mac environment
The basic flow of environment construction with Docker is as follows
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) |
- |
Schematic diagram (https://docs.docker.jp/engine/understanding-docker.html)
Register with docker hub and download the desktop version from "Get start with Docker Desktop" on the top page. https://hub.docker.com/
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)
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
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
jupyter-lab screen
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) 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
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
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!
・ 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