Skip to content

Docker 101

shamik edited this page Sep 15, 2024 · 3 revisions

Docker Playground on Ubuntu

Docker isn't natively installed on Ubuntu systems, but it can be easily installed by following the steps in the below link.

Installation on Ubuntu

https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

Adding a Docker group to not prefix sudo for every Docker command

Before playing with Docker, there is one more important step to be taken. If you don't want to type sudo with every docker command then perform the steps in the following link: https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user

Create a Dockerfile

touch Dockerfile

or

vim Dockerfile

This will create a Dockerfile in the current, but you can choose the directory of your choice to create the same.

Open the Dockerfile and paste the following commands:

FROM ubuntu 
RUN apt update && apt install -y cowsay 
CMD ["/usr/games/cowsay", "Dockerfiles are cool!"] 

Line 1 bases the image on an existing image called ubuntu. You can do this independently of which system you’re running Docker on. Line 2 installs a program named cowsay. Line 3 prepares a command that runs cowsay when the image is executed.

Building the container from the Dockerfile in current directory

docker build -t cowsay .

-t cowsay will tag the image with the name cowsay.

Running the image cowsay and removing the container after having run

docker run --rm cowsay

Running the cowsay container will output the following:
cowsay

Some docker commands

Pulling a docker image

# docker pull <image name>
docker pull python

Pulling a specific version docker image

# docker pull <image name>:<version>
docker pull python:3.8

Executing a command inside the running docker container, detached mode. This example creates a folder.

# docker exec -d <container name> <command>
docker exec -d ubuntu_bash touch /tmp/execWorks

Running the shell interactively inside a docker container.

docker exec -it <container_id/name> /bin/bash
docker exec -it ubuntu_bash /bin/bash

Naming a container

# docker rum -d --name <new name> <image name>
docker run -d --name redis_new redis

Running a docker image on a specific port.

# docker run -p <host_port_number>:<container_port> <image_name>
docker run -p 6000:6379 redis

Listing docker containers

docker ps -a

docker ps -a

Listing docker images

docker images

docker images

Deleting/Removing containers

docker rm <container name/first four characters of the container id>

docker rm

Deleting/Removing images

docker rmi <container name/first four characters of the image id>

docker rmi

For a comprehensive list of docker cli commands, visit here

Running images from the Docker Hub

Instead of creating a new image through dockerfile, one can also run official docker images and build the containers from the docker hub. One such example is the one below, wherein, the latest development version of the python is run.

Running Docker image of the latest development build natively in the terminal

docker run -it --rm python:rc

The -it option are necessary for running the container interactively. This runs the container natively in the terminal. The rc tag is shorthand for release candidate and points to the latest development version of python. The rm option is to remove the container once you have finished running it.

Running Docker image of the latest development build in the background

docker run -d --rm python:rc

Stopping a docker container

docker stop python:rc

Python versions available in Docker Hub

All python versions available in docker hub. More description for each version of python can be gleaned from the same source.

Running a specific python version from Docker Hub

Running a specific version of python in docker container and removing the container once finished running; e.g. below for final version of python 3.8.

docker run –it --rm python:3.8

Setting up a Python Environment by creating a Dockerfile

If there are multiple dockerfiles in the same directory then one can use the -f flag to be able to use a particular dockerfile to build the image.

Creation of a dockerfile

vim Dockerfile_python

Docker commands for the dockerfile

Paste the following content in the Dockerfile_python. The following instructions downloads a python version and installs mentioned packages.

FROM python:3.8-slim
RUN python -m pip install \
        parse \
        pandas

The slim tag tells docker to build with a minimal debian installation to run the specified version of python successfully. This will create a *"slim"*mer docker image but all debian will not be available.

Building the dockerfile

Building the same dockerfile, which is in the same directory containing another dockerfile with the image name as python_slim.

docker build -f Dockerfile_python . -t py3.8_slim

Running the python_slim container

docker run -it --rm python_slim 

The above commands don't install a separate virtual environment(venv) to install the python packages. As each docker container is a separate environment many a times there is no need to create a venv for installing separate python packages.

Creating a venv container in docker

As the venv cannot be activated by normal way the way to activate is to create a environment variable for the venv.

Set up and activate virtual environment

ENV VIRTUAL_ENV "/venv"  
RUN python -m venv $VIRTUAL_ENV 
ENV PATH "$VIRTUAL_ENV/bin:$PATH" 

Cleaning up docker files

Remove everything

docker system prune -a --volumes -f

Stop and remove all containers

docker rm -vf $(docker ps -aq)

Remove all images

docker rmi -f $(docker images -aq)

Remove all volumes

docker volume prune -f

Remove all networks

docker network prune -f

Clear all build cache

docker builder prune -f
Clone this wiki locally