Understand About Docker, Docker For Beginners

What is Docker?

    
Imagine that Docker is like a big ship carrying containers. Well, inside the containers will contain the merchandise. Hypothesize that the merchandise are "Docker images", so structure of docker similar :

1. What is the container?

    What is the containers?
Container is like virtual environment in python. It's contain several Docker image, Docker image is instance or object of container.

  • Container is a way to package applications with all the necessary dependencies and configuration.
  • Easily shared and moved around between a development team or deployment and operations team
  • Makes development and deployment more efficient

2. Different before container & after container

    👍How did we develop applications before the containers?Usually when you have a team of developers working on some application, you would have to install most of the services on your operating system directly. Alright, for example developing some Javascript application. And every developer in the team would configure them and run them on the local development environment and depending on which operating system they are using installation process will look actually different. Also, another thing with installing services like this is that you have multiple steps of installation, but several steps going wrong and error happening is actually pretty high. And this approach of setting up a new environment can actually be pretty tedious, depending on how complex your application. For example, if you have 10 services that your application is using then you would have to do that 10 times on each operating system environment.

  • Installation process different on each OS environment
  • Many steps where something could go wrong

    👍So now let's see how containers solve some of these problems with containers. With containers, you actually do not have to install any of the services directly on your operating system. Because the container is own isolated operating system layer with Linux based image. So you have the PostgresSQL, pakage with a configuration in the start script inside of one container. And the download step is just one docker command which fetches the container and starts it at the same time. So we have 10 applications that your Javascript application uses and depends on, you would just have to run 10 docker commands for each container and that will be it. Which makes the setting up your local development enviroment actually much easier and much more efficient than the previous version.Also, you can actually have different versions of the same application running on your local environment without having any conflict.

  • own isolated environment
  • package with all needed configuration
  • one command to install the app
  • run same app with 2 different versions

Docker compare to Virtual Machine

     Docker virtualize is the application layer. So, when you dowload a docker image, it actually contains the applications layer of the operating system and some other applications installed on top of it. And it use the kernel of the host because it doesn't have its own kernel.
Virtual Machine (VM) has the applications layer and its own kernel, so virtualize is the complete operating system, which means that when you download a virtual machine image on your host, it doesn't use your kernel. It puts up its own.
So what is this difference between Docker and Virtual Machine actually mean ? So first of all, the size of Docker images are much smaller because they just have to implement one layer. A second one is the speed so you can run and start docker container much faster than VM. The third difference is compatibility.

Installation docker

    Docker can install on the multiple flatforms, such as : Mac, Linux, Window. Link tutorial Iam working with deep learning then by default you use ubuntu. Detail description install : link

  1. Set up the responsitory
$ sudo apt-get update
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
$  curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
$  echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
      $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Install Docker Engine
$  sudo apt-get update
$  sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. Inspect install
$  sudo docker run hello-world

    If result is " Hello from docker ", the installation was successful.

Base commands in docker

  • docker ps : list running containers

    

  • docker ps -a : lists running and stopped container

    

  • docker images: list images

    

  • docker rmi: remove image

    docker rmi <image_id>

  • docker build: building image

    docker build -t <image_name> .

  • docker run: start new container with a command

    docker run -it --rm -d -p <port_host>:<container_host> --name <name> <image_name>

  • docker pull: pull docker image from docker hub

    

  • docker push: push image to docker hub

    docker login
docker tag <container_name> <user_name>/<responsitory>:<tag>
docker push <user_name>/<responsitory>:<tag>

  • docker stop: stop the container

    docker stop <container_name>

  • docker start: start stopped container
  • docker logs: debug containers

    docker logs <containers_id>

  • docker exec -it: use review or debug containers

    docker exec -it <container_id> /bin/bash

    You can refer to PhamDinhKhanh's article very good.

Build docker

    Structure :

1. Build Dockerfile

    Dockerfile is used to create an image for our application. This image will run on any host or environment with Docker installed.
Let's have an overview of whats in our Dockerfile:

  • FROM: A Dockerfile must start with a From instruction with an argument that has anather image.
FROM okwrtdsh/anaconda3:cpu
  • RUN: Will execute terminal commands during build image.
RUN pip install -r requirements.txt <br>
 RUN pip install tensorflow
  • WORKDIR: It is similar to the cd command.
WORKDIR /app
  • LABEL: Provide metadata information for images such as: email, company,author,...
  • EXPOSE: Port
  • COPY: This command copies files from the local system onto the Docker image.
COPY ./app
  • ADD: similar COPY
  • CMD: This command specifies the program or file that will be excecuted when the container initializes.
    Syntax: CMD ["executable", "param1","param2"]

    CMD ["python3","app.py"]

  • ENV: parameters environment
  • ARG: similar argument parser in python

    Eg Dockerfile:

2. requirements.txt

    File requirements.txt help pack libraries to run

3. docker-compose.yaml

    next time or refer to :https://phamdinhkhanh.github.io/2020/11/17/DockerDL.html#11-khái-niệm

Documents

    Install docker: https://docs.docker.com/engine/install/ubuntu/
Build Dockerfile anaconda3: https://github.com/okwrtdsh/anaconda3
Build Dockerfile pytorch: https://github.com/anibali/docker-pytorch/tree/master/dockerfiles
Video tutorial : https://www.youtube.com/watch?v=3c-iBn73dDE
Blog: https://phamdinhkhanh.github.io/2020/11/17/DockerDL.html#11-khái-niệm

Nguồn: Viblo

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan