Docker containers for dummies

Source: Deep Learning on Medium

Docker containers for dummies

Docker allows you to package an application with all of its dependencies into a standardized unit for software development.

Source: Link

Why should you use Docker?

  1. Try any other version of OS in few seconds without uninstalling existing OS
  2. Save installation time on new machines: Install libraries, frameworks inside docker and use that image on any other machine
  3. Version compatibility: Since all libraries will be inside docker which is like a separate machine hence no version mismatching

How to use Docker?

Switch to “root” user or run all below command with “sudo” prefix as docker daemon needs “sudo” permission to run

Install docker

apt-get install
Install Docker

Pull docker image from Docker Hub.

Sample image in which I installed python, I built it upon the official Ubuntu image.

docker pull <image name>

List all available docker images in your system

docker images
Pull Docker Image from DockerHub

Build a container out of docker image

Note: “Image Id” can be used instead of “image name:tag”

docker run -it <image name:tag> or <image id> bash
Build a Docker container from Docker Image

List Containers

List all running containers:

docker ps

List latest created running container

docker ps -l

List all containers which are created(Need not be running)

docker ps -a
List containers

Close a running container

docker stop <container id>
Stop a running container

Restart a closed container

docker start <container id>

Go to the terminal of a running container

docker exec -it <container id> bash
Start a closed container

Modify your container: Install libraries

Here I installed python

apt-get update
apt-get install python
Install Python

Save container to an image

docker commit <container id> <give image name:give new tag>
Save container to an image

Push the changes to docker hub

Create a docker hub account if you don’t have and login to that using terminal

docker login -u <username><Enter password>
Login to docker hub

Create tag: Can be anything

Tag will be a identifier for what this image is used for

docker tag <image name:tag> <docker hub username>/<image id:new_tag>

Push docker image to DockerHub

docker push <docker hub username>/<image id>
Create tag & push the docker image to Docker Hub

Finally, pull this image on any other machine and enjoy coding python without reinstalling python into it. Likewise, you can build Docker images on your machine and can reuse those images anywhere and get rid of the pain of reinstalling modules/ libraries every time.

Python docker image on Docker Hub

Thanks! for reading. Please reach out to any queries.