Date
Sep 11, 2022
slug
docker-cheatsheet
Status
Published
Subtitle
A comprehensive guide to Docker basics and commands
Tags
Tips
Code
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 bashThis 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.
Removing Dangling Docker Images
Dangling images are images that aren't tagged and aren't referenced by any container. Here are the commands to remove them:
List dangling images first
docker images -f "dangling=true"
Remove all dangling images
docker rmi $(docker images -f "dangling=true" -q)
Using docker prune (recommended method)
# Remove only dangling images docker image prune # Remove all unused images (both dangling and unreferenced) docker image prune -a
The
docker image prune command is generally the preferred approach as it's specifically designed for this purpose and handles all the filtering for you.If you want to confirm before deletion, these commands will prompt you before removing the images. To skip the confirmation, add the
-f or --force flag:docker image prune -f
Would you like me to explain anything else about managing Docker images?
Deleting Active Docker Containers
Here are the commands to delete active Docker containers:
To stop and remove a specific container
# Stop a specific container docker stop <container_id_or_name> # Remove a specific container after stopping it docker rm <container_id_or_name>
To stop and remove all running containers
# Stop all running containers docker stop $(docker ps -q) # Remove all stopped containers docker rm $(docker ps -a -q)
To force remove running containers (use with caution)
# Force remove all containers without stopping them first docker rm -f $(docker ps -a -q)
To list containers before deleting
# List all running containers docker ps # List all containers (running and stopped) docker ps -a
Would you like me to explain any of these commands in more detail?