4

Jupyter Notebook in a AWS EC2 Instance using Docker Image | Towards Data Science

 3 years ago
source link: https://towardsdatascience.com/jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Jupyter Notebook in an AWS EC2 Instance using Docker Image

Best of all possible worlds for Data Science and Machine Learning professionals

Photo by Zeshalyn Capindo on Unsplash

Most of the data science and machine learning professionals are in love with the Jupyter notebook. Barring few anomalies, most of the data science and machine learning works are happening in the cloud. So there is no option but to use Jupyter on the cloud.

In my previous article, I explained how you can download the Jupyter notebook on AWS Linux instance and access it on your client machine using Xming and X11 forwarding. Below is the link to that article

In this article, I am explaining how to access the Jupyter notebook in AWS EC2 instance directly through a Docker image. No need to install Anaconda or Jupyter in the AWS EC2 instance.

I am assuming that you already have an AWS EC2 instance and know how to access it through Putty

Prerequisite Tools

Make sure you have the latest version of the default OS packages. Especially check if firewall related packages are installed and configured. I am using a Redhat Linux instance on AWS EC2.

The first thing to confirm is iptables-services are installed. If not, install it through yum

sudo yum install iptables-services
1*AUtVtXxECacMzx3gkuxnsA.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

Docker Engine Installation

Now you are ready to install the Docker engine. I have followed instructions from the below link for installing the Docker engine on my Red Hat Linux instance. If you have an instance with a different operating system, please check the steps for that operating system in the below link.

a. First thing first; run the below command to install all the yum utilities

sudo yum install -y yum-utils
1*s_J4a4VZ0GMsR11x0zoUww.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

b. Get the latest docker repository

sudo yum-config-manager --add-repo  https://download.docker.com/linux/centos/docker-ce.repo
1*CIfbBmvMToImZ4ozDs3vBg.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011

c. And now install the latest version of docker engine and container

sudo yum install docker-ce docker-ce-cli containerd.io
1*pKSYVxeGrn5t1NSTz2f3OA.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

d. Start the docker service.

sudo systemctl start docker.service

If you do not get any error message, your docker service has started successfully.

1*2SdTXaF7RpgAjRQKHbNehQ.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

e. You can also check the status of the docker service.

sudo systemctl status docker.service
1*3PUkFvb8lY2_ZWtuaAwb2w.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

Congratulations!! You have successfully installed a docker engine. Now you are ready to pull the Jupyter Notebook image.

Jupyter Notebook

It’s time to pull the Jupyter notebook image.

a. Run the below command to pull the docker image and start Jupyter notebook.

sudo docker run -p 8888:8888 jupyter/scipy-notebook

Wait for the image download and pull to complete.

1*lAP5q2CbCgDSlZsK9lyI-w.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

You will get the message indicating Jupyter notebook is running and it will contain a link to Jupyter notebook.

1*BRhlndC0aT6_lmq9pFq2BA.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

b. Copy the link and remove the 127.0.0.1 with the public IP address of your instance. You can see the public IP address for your instance in EC2 Instance Connect tab.

1*9EsvPslCEe3TaeGPrVPsDg.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

c. If you get time out a message or a site can’t be reached message, your instance might not be allowing connection at port 8888.

1*uKGBCZ281lNCUxlu8Ol5ug.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

d. Go to the security tab of your AWS instance and click on the security groups name.

1*S2OTj8Vpdsmb4TSwDbsC4Q.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

e. Click on Edi inbound rules button.

1*tevkNaklCKmcwD_CjOspiQ.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

e. Add a rule for port 8888 as shown below and save the rule.

1*dNIIRc_Xgxe9sy5KQmW-wA.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

f. Now refresh your browser. Jupyter notebook will come up.

1*XCvUoJLQHNU92k8mb5-ZHg.png?q=20
jupyter-notebook-in-a-docker-container-on-aws-ec2-instance-7efc51459d6f?readmore=1&source=---------7----------------------------&gi=cc8157dc0011
Image by Author

Congratulations!! Your Jupyter notebook is waiting for your next data science or machine learning adventure!

Conclusion

Cloud and Docker images are becoming an inevitable part of data science and machine learning professionals' life. Docker images are like work already done for you. Use it as much as possible to reduce your turnaround time in projects.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK