-
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 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.
docker build -t cowsay .
-t cowsay will tag the image with the name cowsay.
docker run --rm cowsay
Running the cowsay container will output the following:
# docker pull <image name>
docker pull python
# 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
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 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
All 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.8
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.
vim Dockerfile_python
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 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
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.
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"
docker system prune -a --volumes -f
docker rm -vf $(docker ps -aq)
docker rmi -f $(docker images -aq)
docker volume prune -f
docker network prune -f
docker builder prune -f