Docker tutorial for Data Science

Docker Architecture

What is Docker?

Docker is a tool for creating and deploying isolated environments for running applications with their dependencies.

A Docker container can be seen as a virtual computer inside our computer. We can it to our friends or anyone and when they start this computer and run your code they will get exactly the same results as you did.

Why Docker?

Creating a Docker container given an image id.

Get the image id from the below command

docker images

Select any one image id and run the below command to map the docker

docker run -itd -v /pwd/:/workspace/ -p xyzw:xyzw --name=any_name image_id

The above command returns a container id, Run below command to run the container.

docker exec -it container_id bash

Example:

1. Running as a root user(base) swatimeena@Swati’s-MacBook-Pro ~ % sudo su2. Get the docker imagessh-3.2# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEfedora              latest              a368cbcfa678        3 months ago        183MBproject_1           latest              cbb0d901ffb3        3 months ago        5.57GBhello-world         latest              bf756fb1ae65        9 months ago        13.3kB3. Select one image_id (Choosing bf756fb1ae65)4. Mapping the containersh-3.2# docker run -itd -v /home/:/home/ -p 1220:1220 --name=mlprojects bf756fb1ae65(Replace name=any custom name and xyzw can be any port)5. Get the container_id sh-3.2# docker ps -a or sh-3.2# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES09a50fd07b8b bf756fb1ae65 “/hello” 12 seconds ago Exited (0) 12 seconds ago mlprojects(Check the name or if you are not using name argument then check the status, it must be a few seconds ago ust after making the container, Copy the continer id)5. Run the containersh-3.2# docker exec -it 09a50fd07b8b bash6. Install all dependencies inside the container and enjoy :)root@d8a8d3523dc0:/#

Running jupyter inside the docker container:

Run the container using

docker exec -it container_id bash 
or
nvidia-docker exec -it container_id bash

Install jupyter in the container

pip install jupyter

Run the notebook on the same port, docker container was exposed to i.e. xyzw

jupyter notebook — ip=0.0.0.0 — port=xyzw — no-browser — allow-root &

Run the jupyter notebook in your local browser

Open browser and run remote_ip:xyzw and enter token

To stop the notebook

jupyter notebook stop xyzw

Stop the jupyter notebook running on xyzw port

Running python scripts inside the docker container:

Start/Run the container:

docker exec -it container_id bash 
or
nvidia-docker exec -it container_id bash

Once you are inside the container, navigate to the folder where you have your python script using cd command and run:

python script_name.py

(You need to install all the necessary packages and dependencies inside the container first, using something like pip)

Other Docker commands

To see all the running docker containers

docker ps

All docker containers

docker ps -a

Start the container, if the below command gives an error or failed to start the container

docker exec -it container_id bash

Run

docker start container_id

(No need to make a new container with all the dependencies, restart the container using the above command)

If the container was killed, then make a new container.

Kill a Docker Container

docker kill container_name

Stop a Docker Container

docker container stop container_idordocker container stop container_name

--

--

Data Scientist @Sprinklr | IIT Bombay | IIT (ISM) Dhanbad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store