Learn about docker

Tram Ho

1. What is Docker?

Docker is a platform that provides developers with an easy way to build, deploy and run applications using containers (virtualization platform). Initially, Docker was written in Python but now it has migrated to Golang.

2. Docker Key Components

  • Docker image: it is considered as templates in  Docker Container. It includes all the things needed for your project to run, for example it can be an image file, a file of a platform, a language or an operating system or a source code. Images are shared publicly at  Docker Hub  so that everyone can use and develop together.
  • Docker Container: is a virtual machine and an executable version of the  Docker Image  as *Container * holds all the packages needed to start and run the application.
  • Docker Hub: is a cloud service similar to Github  capable of automating continuous workflows and sharing applications. It allows users to pull/push operations with images.
  • Docker Engine: as a tool that can package applications and run them.

3. Why use Docker?

  • Share, store the project environment of the team: the Container in Docker  allows users to store and share the project environment and work with multiple people in the team easily.
  • Emulate the environment on the server under the local machine: using Docker will allow users to completely simulate a new server environment under the local machine quickly and perfectly.
  • Experience and try a new operating system: with Docker you won’t need to use* Virtual Studio* when you want to experience and try out a new operating system.

4. What is Dockerfile? How to create a Docker Image from Dockerfile?

Docker will build Docker Image automatically by reading the instructions declared in a file named  Dockerfile. Dockerfile is a text file containing all the instructions that the user wants to execute to create a  Docker Image.

Some commonly used commands in Dockerfile

  • FROM <image>:<version>: used for the initialization of a new  Docker Image  and to indicate which original image will be the basis on which build Image  will execute subsequent instructions.
  • RUN <command>: use this command to run a command for installing the necessary tools for our  Image .
  • CMD <command>: In the  Dockerfile  there is only one CMD command, which is used to provide the default command to be run when the  Docker Container starts from the built Image . CMD does not execute the command at the build image process, but will execute it during the  Docker Container from Image run.
  • ENTRYPOINT<command>: Defines default commands, which will be run when the container is running.
  • SHELL [“executable”, “parameters”]: The shell directive allows other shell forms to override the default shell.
  • ENV <key> <value>: used to declare environment variable named <key> with value <value> in Container.
  • LABEL <key>=<value>: used to add metadata information to the * Docker Image* when it is built. An Image can have multiple labels with metadata information.
  • WORKDIR: used to declare the working directory for directives: RUN, CMD, ENTRYPOINT, COPY and ADD.
  • EXPOSE <port> : listens for connections on specified ports at launch.
  • VOLUME <folder_name>: used to access or link a folder in  Container.
  • ADD <src>…<dest>: used to copy files and directories from the building directory location on the local client or remote files URL (src) and add them to the filesystem of  Image (dest).
  • USER <user>[:<group>]: used to declare username information used when running Image and also the user used to run other directives such as :RUNCMD and ENTRYPOINT.
  • MAINTAINER <name>: This statement is used to declare on the creator of Image, we can declare it or not. Now we will start creating the image from the Dockerfile. We create an empty directory then create a file named Dockerfile with the following content:

Then we point to the directory containing the Dockerfile and run the command docker build ./ and see the results:

After the build is successful, we execute run Image with the command:

5. How to create Docker Image on Docker Hub:

To create a Docker Image on Docker Hub we need to perform the following steps:

  1. Create an account and login to the site hub.docker.com
  2. Find and click Create Repository
  3. Fill in the required information and select public
  4. Log in to Docker Hub using the command line:

  5. Check the ID of the image to use:

    $ sudo docker images

    If the image is not tagged, tag it with the command:

  6. To upload an Image to Docker Hub you need to use the command:

6. Summary of commands when manipulating Docker Containers:

Here are the operations commonly used with Docker Containers, in addition, there are many other commands you can refer to at This

  • docker container commit: Create a new Image from the changes of the Container.
  • docker container create: Create a new Container .
  • docker container exec: Run commands while Container is active.
  • docker container kill: Termination of one or more Containers.
  • docker container pause: Pause all processes inside one or more Containers.
  • docker container run: Run commands in a new Container .
  • docker container start: Run a Container or multiple stopped Containers.
  • docker container rename: Rename Container.
  • docker container restart: Restart one or more Containers
  • docker container stop: Stop 1 or more Containers from running
  • docker container rm: Delete 1 or more Containers

7. Summary of commonly used commands when manipulating Docker Image:

  • docker images: List of Images
  • docker image build: Build image from file Dockerfile.
  • docker image history: View the history of Image.
  • docker image import: Import the content from the tarball to create the filesystem of Image.
  • docker image inspect: Displays detailed information of one or more Images.
  • docker image load: Load images from *.tar or STDIN files.
  • docker image prune: Delete unused Images..
  • docker image pull: Pull an Image or repository from the Docker HUB registration.
  • docker image push: Push  Image, repository to Docker HUB.
  • docker image save: Save one or more images to a *.tar file.
  • docker image tag: Tag the TARGET_IMAGE corresponding to the SOURCE_IMAGE.

8. What is Docker Volume?

As we all know, the characteristic of Docker Container is that they run independently, and do not affect each other, but for some reason, we want to share data between Containers for example, you have 1, 2, 3 web servers nginx and expect when they share the same config files, or static html. Luckily docker provides volumes to do that.

Docker volume is a volume created that allows containers to mount the volume into containers or more easily docker uses that volume instead of a container’s folder. In terms of operation  Volume is similar to Bind mounts, but Volume is managed by Docker. While bind mounts, the file or directory to be mounted must exist on the docker host.Volume works on both Linux and Windows containers.

When to use Volume?

  • When sharing data between multiple running containers.
  • Save data to a remote server or cloud.
  • When you need to backup, restore or migrate data from one Docker host to another.
  • Needs easier and more convenient management than bind mounts.

9. What is Docker Compose?

Docker compose is a tool used to define and run multi-containers for Docker applications. With Compose you use the YAML file to configure the services for your application. Then use the command to create and run from those configs.

To use Docker Compose it only takes three steps:

  • Declare app’s environment in Dockerfile.
  • Declare the services needed to run the application in the docker-compose.yml file.
  • Run docker-compose up to start and run the app.

The syntax used in the Docker Compose config file:

  • version: indicates the version of docker-compose used.
  • services: Set up the services (containers) you want to install and run.
  • image: Specifies the image used during the creation of the container.
  • build: used to create containers.
  • ports: Set ports to run at the host machine and in the container.
  • restart: automatically launch when the container is shut down.
  • environment: set environment variables (usually used while configuring the parameters of the DB).
  • depends_on: indicates dependencies. That is, which services must be installed and run first, then the service is configured there to be run.
  • volumes: used to mount two directories on the host and the container together.To run Docker Compose run the command: docker-compose up -d

Summary:

In this article, I mainly introduce the theory so that I can understand Docker and the main components of Docker. In the process of writing articles, I also need to learn and learn a lot of new things. I look forward to hearing from you in the comments section below! Thank you for listening to my sharing.

Share the news now