# How to Set Up a Docker for Deep Learning Using PyTorch Framework On Ubuntu

*Docker For Deep Learning with Pytorch*

By [Perception](https://paragraph.com/@perception) · 2024-06-21

tutorial

---

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It uses containerization technology to bundle applications and their dependencies into a standardized unit for software development. This ensures that the application will run the same, regardless of the environment it's in, thus solving the issue of "it works on my machine" problem. Docker is widely used in deep learning to create consistent, reproducible environments which can be very important when working with complex libraries such as PyTorch.

Install Docker on Ubuntu
------------------------

To begin the installation of Docker on your Ubuntu system, first execute the following two commands: update your Ubuntu repositories using \`sudo apt update\`, then install Docker with \`sudo apt install docker.io -y\`.

    sudo apt update
    sudo apt install docker.io -y

Run the following command to show the interfaces on your computer.

    ip address show

To list the networks on the current docker, run the command:

    sudo docker network ls

You can launch containers on Docker's default network using the following command line.

    sudo docker run -itd --rm --name [container name] [image]

*   \`-itd\` allows the Docker container to run interactively in detached mode, meaning it continues running in the background,
    
*   '-d' instead of '-itd' if you don't want the interactive mode but only to run it in the background.
    
*   \`--rm\` instructs Docker to automatically remove the container once it's no longer in use.
    
*   \`\[image\]\` represents the specific image that you want your Docker container to utilize.
    

You can use the following commands to see the virtual ethernet interfaces created and names together with the links, respectivelly.

    ip address show
    bridge link

The \`sudo docker inspect\` command provides detailed information about a Docker object. This could be a container, image, volume, network, or node. The command returns information in JSON format, and it can be used for troubleshooting or scripting purposes. In the context of the PyTorch deep learning setup mentioned in the post content, you could use this command to examine the state and configurations of your Docker containers or images to ensure they are set up correctly for your specific needs.

For example, to inspect the bridge (the network type that docker uses)

To open an interactive shell and execute a process inside a running Docker container, use:

    sudo docker exec -it [container name] sh

To create a custom or user defined bridge other than the default bridge, Use the following command

    sudo docker network create [network name]

Now use the custom network to create a new container as follows:

    sudo docker run -itd --rm --network [network name] --name [container name] [image]

Note that the user defined network type (bridge) and the default can't talk each other or simply are isolated. One advantage of user defined network type is that you can ping to the other container in the same network by name. i.e., if you have loki and odin containers with the same user defined network, you can use:

    sudo docker exec -it loki sh
    # ping odin

where odin is a network. where as you have to specify the ip address not the DNS when using the default bridge.

Run the following command to view the running processes or containers (first command) and all including the stopped or exitted processes (second command). 'ps' refers process status. i.e., -a (--all), -l(--latest), -f(--filter \[e.g., name, status, image\]), --format: customize the output format (e.g., JSON, table with specific columns).

    sudo docker ps
    sudo docker ps -a

Stop the running container or process with the container name or id

    sudo docker stop [container name/container id]

Building a Docker Image
-----------------------

Step 1: Create a Dockerfile. You can take a base image and add your own instructions into the Dockerfile.

First, start with creating a directory, let's create a directory and name it dockertmp.

    mkdir dockertmp
    cd dockertmp

Create a Dockerfile.

    touch Dockerfile

Now open the 'Dockerfile' file in a text editor to add some instructions used to build an image. The COPY command copies a file to the appropriate directory in the image.

In a Dockerfile, the `FROM base_image` instruction is the foundation for building your image. It specifies the base image that your new image will be built upon. Here's a breakdown of its usage:

**Functionality:**

*   The `FROM` instruction tells Docker which existing image to use as the starting point for your new image.
    

    FROM [base image]
    COPY [file on dockertmp directory] /tmp/
    RUN apt update -y && \
        apt autoremove && apt autoclean 

The commands used to create a docker image are

    FROM [base image]
    COPY [file or directory you want to copy] /project
    # create a '/project' folder in the base image which contains all your codes/files
    WORKDIR /project  # Create a working directory called 'project' on the container, as it is isolated from the local machine
    RUN pip install -r requirements.txt # intall the libraries and dependencies required
    EXPOSE $PORT
    CMD

Step 2: Start docker and use the Dockerfile to build docker image.

Check if the docker is running first using the command

    docker info

If the docker is not running, run it using

    sudo service docker start

Build the docker image and name it \[image name\], e.g. "docker build -t semeregt\_dl ." -t stands for tag or labeling the image name

    docker build -t [image name] .

Step 3: Create a container from the docker image and specify the port to map port from your local machine, e.g. 8080, to port inside the container, e.g., 80.

    docker run -p 8080:80 -itd --name [container name] [image]

Now you can search 'https://\[host ip address\]:8080' on your browser to see what is going on.

Container
=========

A container has its own operating system, packages, and all the scripts. It is sort of disconnected from the local system or local computer. The isolated nature of a container is both its strength and challenge. It's like having a fully equipped computer within your existing one, with its own set of dependencies and libraries, all running in sync to perform the tasks you desire. This isolation ensures that whatever you do inside the container doesn't interfere with your main system, providing an extra layer of safety and consistency in your development process. Especially in the realm of deep learning, where complex libraries like PyTorch are used, Docker lets you experiment freely without worrying about environment inconsistencies or conflicts. However, it also means you need to manage data transfer carefully between your main system and the container, as they don’t automatically share resources unless explicitly told to do so.

Docker File
-----------

Consists of a set of instructions on how to build the docker image. The OS, the Python version, the extra packages are stated in this 'Dockerfile' file.

Docker Image
------------

Docker image is built from the **Dockerfile**. The .ISO image file is created in this step from the Dockerfile.

Docker Container
----------------

The docker container is when the docker image is run. In other words, it is an instance of a docker image.

Advantages of Using a Docker
----------------------------

*   Portability
    
*   Ease of deployment
    

Build docker image

    docker build -t aladdin-image .

Run the container

    docker run -d \
      --gpus all \
      -v "${PWD}:/code" \ # to mount volume from local to container directory, should be absolute path (start from /home/ or /root/
      -p 8080:8080 \
      --name "aladdin-container" \
      --env AUTHENTICATE_VIA_JUPYTER="mytoken" \  # this is for jupyter you can leave it
      aladdin-image \
      tail -f /dev/null # this is to keep the container running

Interact with the docker container

    docker exec -it aladdin-container /bin/bash

Attach the volume mount of the container to the local machine. This helps to sync the container to the local machine. Run this code from the local machine command.

    tmux attach -t docker

[![](https://paragraph.xyz/editor/youtube/play.png)](https://www.youtube.com/watch?v=kuBQ8ylVXBU)

[

![](https://opengraph.githubassets.com/3190614cbbe61fb68a793c2fde4e449c2c699a7c6cf96cc213e28f4ff6bc033f/aladdinpersson/Machine-Learning-Collection)

https://github.com

GitHub - aladdinpersson/Machine-Learning-Collection at recsys
-------------------------------------------------------------

A resource for learning about Machine learning & Deep Learning - GitHub - aladdinpersson/Machine-Learning-Collection at recsys





](https://github.com/aladdinpersson/Machine-Learning-Collection/tree/recsys?tab=readme-ov-file#Docker-Setup)

### With Docker Compose

    docker compose up # run container using docker compose
    docker compose down # stop 
    docker container prune # remove all the stopped containers

Rename the name of an image

    docker image tag [image name]:latest [new name]:version or # run docker images to see the name and if the tag is latest
    docker image -t [image name]:latest [new name]:version # version is a tag, can be 1.0, 2.0, 1.1 or something else

Upload the image into docker hub by

    docker push [image name]:version # check about the tag/version from the above code

To remove the local instance of an image, run

    docker container prune
    docker rmi [image name]:version # version is the tag, rmi is remove image

    sudo systemctl daemon-reload
    sudo systemctl restart docker

Use the following website to make the local gpu accessible by the container. i.e., installing nvidia-docker runtime. [https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)

---

*Originally published on [Perception](https://paragraph.com/@perception/how-to-set-up-a-docker-for-deep-learning-using-pytorch-framework-on-ubuntu)*
