Introduction
The phrase 'But it works on my machine!!' is exactly the problem Docker solves. Docker allows us to share applications, including the operating system they were built on, along with all of the dependencies. This solves the issue of running applications across different environments.
Definitions
- Image: An image is a template of our source code along with all dependencies
- Container: A container is a running instance of an image. If an image is equivalent to a class, then a container is equivalent to an object of that class
- Dockerfile: It is used to create the Docker Image
- Docker Registry: A place where people can push their images to be accessed publicly. The most famous is DockerHub
- Docker Daemon: Docker runs on a client-server architecture system. Docker Daemon is the server side of it
Commands
- Start a container from an already downloaded/built image:
docker run -it <IMAGE_NAME>/<IMAGE ID>
- List all running containers:
docker ps
- Build an image from a Dockerfile:
docker build -t <IMAGE_NAME> .
- List all images:
docker images docker images ls
How to make a DockerFile for ML projects
A Dockerfile is used to build a docker image which clones the contents of our project, sets up the base Operating systems and also downloads all the dependencies. Here are the steps to create one:
- Create a file named Dockerfile and .Dockerignore file in the root of the project
- Choose a base image, usually for python projects it is the Slim or the alpine Version taken from DockerHub
- Set the Working directory in your image and give it a name
- Copy the requirements.txt file to the working directory of the image
- Install the dependencies via pip install -r requirements.txt
- Copy all the project files
Docker Errors and how to solve them
If you encounter the error 'Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?', simply restart Docker Desktop. This error may occur when the connection to the docker daemon is lost after a system restart.
How to mount a docker volume
To mount the current volume to the desired location in the image, use the -v or --volume tag. For example:
Copy
docker run -it -v $(pwd):/workspace/project 63b0afa6efdc bash
This mounts the current directory to /workspace/project in the Docker container. Remember that the mount is two-way: changes in the Docker image will reflect in the local filesystem and vice versa.