-
Notifications
You must be signed in to change notification settings - Fork 0
Docker 101
Docker isn't natively installed on Ubuntu systems, but it can be easily installed by following the steps in the below link.
https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
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
touch Dockerfileor
vim DockerfileThis 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.
docker build -t cowsay .-t cowsay will tag the image with the name cowsay.
docker run --rm cowsayRunning the cowsay container will output the following:

docker ps -a
docker images
docker rm <container name/first four characters of the container id>
docker rmi <container name/first four characters of the image id>
For a comprehensive list of docker cli commands, visit here
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
docker run -it --rm python:rcThe -it option are necessary for running the container interactively. The rc tag is shorthand for release candidate and points to the latest development version of python.
Stopping a docker container
docker stop python:rcAll python versions available in docker hub. More description for each version of python can be gleaned from the same source.
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.8If 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.
vim Dockerfile_pythonPaste 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 \
pandasThe 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 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_slimdocker 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.
As the venv cannot be activated by normal way the way to activate is to create a environment variable for the venv.
ENV VIRTUAL_ENV "/venv"
RUN python -m venv $VIRTUAL_ENV
ENV PATH "$VIRTUAL_ENV/bin:$PATH"