Docker Introduction: Understand the three core features of Docker

Docker's past and present

The core concept is Build, Ship, and Run Any App, Anywhere, that is, package once and run everywhere.

In the past, when we configured an application's operating environment on the server, we had to install various components. For example, in the JavaWeb environment, we had to install Tomcat, Etc. Not to mention how troublesome it is to install and configure these things, it is not cross-platform. If we install these environments on Windows, Even if the operating system is not cross-operating system, it is very troublesome to transplant the application to another server with the same operating system.

The emergence of Linux container technology solved this problem, and Docker was developed on its basis. The application is run on the Docker container, and the Docker container is consistent on any operating system, which achieves cross-platform and cross-server. You only need to configure the environment once, and you can deploy it on another machine with one click, which greatly simplifies the operation.

Docker installation and configuration

Here I will mainly introduce the installation of Docker in Ubuntu and Windows. You can search for installation tutorials for other operating systems.

Since the access speed of Docker official website in China is not good, it is recommended to download the image from DaoCloud, which is very fast:

https://get.daocloud.io/

Windows

First go to the Docker download page:

https://get.daocloud.io/

in the case of If the system is Windows 10 or earlier, download Docker Toolbox. After the download is complete, click Next to complete the installation.

Notice:To run Docker on Windows, you need to enable Intel Virtual Technology in BIOS. If you cannot open Docker and receive an error, please search for relevant tutorials based on your computer model.

Ubuntu

  • You can install Docker using the installation source that comes with Ubuntu. Just execute the following two commands:

$ sudo apt-get update
$ sudo apt-get install docker.io

  • However, the Docker installed with its own installation source is not the latest version. It is recommended to install the latest version in the following way:

$ curl -sSL https://get.daocloud.io/docker | sh

  • After the installation is complete, you can use the docker version command to view the docker version information.

Notice:Normally, the information of Client and Server should be displayed. If there is no Server information, it means that the service is not started. In this case, you need to start it with the service docker start command.

Uninstall Docker on Ubuntu

If you installed the wrong version, you can uninstall and reinstall
$ sudo apt-get remove --purge docker.io
$ rm -rf /var/lib/docker

Docker Core Concepts

Docker has three core concepts:

  • Repository
  • Image
  • Container

Docker's design of using repositories to manage images is very similar to Git. The application runtime environment we want to configure is the image, such as When we don't have the image locally, we need to pull it from the remote repository. When we start the image, a copy of the image will be copied and run in the container. If we change the content in the container, we can also commit it and save it as a new image.

First Docker instance

Here we take Nginx as an example to demonstrate the process of starting an application in Docker.

Starting Nginx is divided into three steps:

  • Pull nginx image
  • Run the nginx container
  • Map nginx port

Pull image

First, search for nginx in the Docker official repository, find the one with the word official, click it and copy the pull command.

https://hub.docker.com

Execute this command to pull the image to the local computer. Execute "docker images" to view the local image.

In fact, the docker pull nginx command is a shorthand form. The full command should be:
$ docker pull registry.hub.docker.com/library/nginx
The complete command adds the address of the registration server in front of nginx. Since the default address of docker pull is this, the address can be omitted.

Why do I say this? Because downloading images from the official repository is very slow, and sometimes it can't be downloaded. If we change the address of the registration server and replace it with a domestic one, we can speed it up.

Mirror Acceleration

There are many image repositories in China. This article introduces the use of Alibaba Cloud. Later, we will also introduce how to upload your own images to Alibaba Cloud, and this service is free.

Visit this address:

https://cr.console.aliyun.com

If you don't have an Alibaba Cloud account, register one yourself. On the page above, you can see your own exclusive acceleration address, and there are installation methods for different systems. Just install it according to the tutorial given.

Run the nginx container

After pulling the image, execute the following command to start it.
$ docker run -d nginx
This command starts nginx in the background. Without -d, it starts in the foreground. After starting, we still cannot access it for the time being. We also need to map the nginx port in the container to the outside of the container before we can access it.

Map nginx port

$ docker run -d -p 8080:80 nginx
Compared with the previous command, only one is added -p Parameter, followed by the port. Here I map port 8080 of the local machine to port 80 in the container. When you visit localhost:8080 in the browser, you can see the welcome page of nginx.

Basic operations on images and containers

Mirror Operation

  • View local image

$ docker images

  • Add tags to the image

$ docker tag old-tag new-tag

The new tag image ID is exactly the same as the original tag image ID. They actually point to the same image file, but with different aliases.

  • Deleting an image

$ docker rmi image-id/image-tag

Notice:To delete an image, you need to first delete the container that uses the image. Although you can force deletion, it is not recommended. In addition, you can delete an image by tag or ID. If the ID is the same, delete it by tag.

Container Operations

  • View running containers

$ docker ps

  • Stop a running container

$ docker stop container-id

The container ID does not need to be entered in full, as long as it can distinguish between the two containers.

  • View All Containers

$ docker ps -a

  • Deleting a container

$ docker rm container-id

Before deleting a container, you need to stop it first.

  • Save the running container as an image

$ docker commit container-id image-tag

Special attention:If you change the container but do not save it as a new image, all changes will not be saved after the container is deleted. The next article will introduce another Docker data storage that can solve this problem. In addition, the tag after this command can be omitted and only the name can be written. The default is latest.

  • Entering the container

$ docker exec -it container bash

The Docker container also has a Linux-like file system and can use some Linux commands. To exit the container, enter

Notice:Some images do not have bash. For example, if you run docker pull daocloud.io/library/nginx:1.13.8-alpine-perl, this command will report an error. In this case, you can use sh to establish a connection:
$ docker exec -it container /bin/sh

  • Copy files into the container

$ docker cp file container-id:newfile

  • Copy files in the container to outside the container

$ docker cp container-id:file newfile

The command to copy files from the container to the outside of the container is executed outside the container

Import and export images and containers

Importing and exporting images

  • Exporting an Image

$ docker save -o name.tar image-tag

  • Importing an Image

$ docker load --input name.tar

Importing and exporting containers

  • Export Container

$ docker export -o name.tar container-id

  • Import the container (docker import name.tar image-tag)

Importing a container actually imports it into an image.

Push your own image to the remote repository

Let's first understand how Docker manages repositories and images.

The warehouse is placed on the registration server. One warehouse only manages one image, and one image can have multiple versions.

Push the image to Docker Hub

  • Access Docker Hub (registration server):

https://hub.docker.com/

  • If you don’t have an account, register one yourself, then click Create Repository to create a repository on the following page.
  • For example, my name is: web-server, and my warehouse name is stormxing/web-server
  • Then log in to Docker Hub locally using the following command
  • If we want to upload the image to Docker Hub, our image name must be consistent with the warehouse name. To do this, we can create a new tag for the image. For example, recreate a tag stormxing-webserver for the nginx image. If the version is not filled in, it defaults to latest.
  • Finally, just push it. After completion, you can see your image in the tags option on the repository page.

Push the image to the Alibaba Cloud warehouse

Although Docker Hub can push images, the speed is too slow. It is better to use domestic ones with faster speed. Here we introduce Alibaba Cloud.

  • First log in to this address, create a warehouse, and then set the login password:

https://cr.console.aliyun.com

  • Create a warehouse and select local warehouse
  • Finally, Alibaba Cloud’s operation guide is very detailed and basically the same as the above steps, so I will not go into details here.

Summarize

This article mainly introduces the role of Docker,Docker's three core componentsIt also introduces the basic operations of (warehouse, image, container), and how to push your own image to the remote warehouse.

1/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *