Docker, as an emerging virtualization method, has many advantages compared with the traditional virtual machine method. Docker can make more efficient use of system resources, faster startup time, consistent operating environment, continuous delivery and deployment, easier migration, easier maintenance and expansion.

The first time the blogger used Docker, he fell in love with this way, one configuration, running everywhere, no longer repeated configuration environment can be said to be very convenient. This blog will talk about the installation and basic use of Docker, and the Docker series blog will be updated from time to time.

Compare with traditional VMS

What is a Docker? Docker is a technical packaging of containers, providing a more simple and easy to use interface, so that shipping and maintenance personnel can use containers more conveniently and quickly.

features The container The virtual machine
Start the Second level Minutes of class
The hard disk to use Generally for MB Generally for GB
performance Close to the native Weaker than
System support Supports thousands of containers on a single machine Usually dozens

The performance and features of containers are better than those of virtual machines.

The installation of a Docker

Docker is an open source product, divided into Community Edition (CE) and Enterprise Edition (EE). The community edition is free, while the Enterprise edition includes some paid services, which are generally sufficient for individual developers. This post is for the community edition only.

Installation environment and Version:

  • System: Ubuntu 18.04 LTS
  • Docker version: 18.9.05

If you are good at English, you can also read the official documentation directly. This article only introduces the installation of Docker in Ubuntu system in detail. Please refer to the official documentation for installation of other systems.

  • Mac
  • Windows
  • CentOS
  • Debian
  • Fedora
  • Ubuntu
  • Other Linux Versions

Uninstall the old version

Docker will not be installed in Ubuntu by default, but if you have an older version you can uninstall it using the following command.

$ sudo apt-get remove docker docker-engine docker.io containerd runc
Copy the code

Install the Docker CE

There are several different ways to install Docker CE:

  1. Set up the Docker repository and install it. This method is easy to install and update, and is the most recommended method.

  2. Download the DEB package, install it manually and manage the upgrade completely manually.

  3. In test and development environments, some users choose to install Docker using automatic convenient scripts.

This blog will cover the first installation method.

Set up the Docker repository

  1. updateaptIndex of package:
$ sudo apt-get update
Copy the code
  1. allowaptInstall software using the repository via HTTPS:
$ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Copy the code
  1. addDockerOfficial GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Copy the code

Once added, use the following command to verify the secret key by searching for the last eight characters of the fingerprint to verify that you now have the key for the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

$ sudo apt-key fingerprint 0EBFCD88

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]
Copy the code
  1. Use the following command to set up the stable repository.
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Copy the code

Install the Docker CE

  1. updateaptIndex of package:
$ sudo apt-get update
Copy the code
  1. Install the latest versionDocker CEcontainerd:
$ sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Copy the code
  1. validationDocker

Use the following command to view the version of the Docker

$ docker -v
Copy the code

Then use the following command to run the hello-world program. Since there is no image of the hello-world program in Docker, it will be pulled down and run.

$ sudo docker run hello-world
Copy the code

If you see the print Hello for Docker! This indicates that the program is running successfully.

Uninstall the Docker

  1. Uninstall the Docker CE
$ sudo apt-get purge docker-ce
Copy the code
  1. Images, containers, volumes, and custom configuration files on the host are not automatically deleted. You need to run the following command to manually delete these files:
$ sudo rm -rf /var/lib/docker
Copy the code

The use of the Docker

Docker startup, shutdown, etc

You can use the following commands to start, shut down, and restart the Docker.

# open Docker
$ sudo service docker start

Close # Docker
$ sudo service docker stop

# restart Docker
$ sudo service docker restart
Copy the code

You can also run the systemctl command to perform the operation

# open Docker
$ sudo systemctl start docker

Close # Docker
$ sudo systemctl stop docker

# restart Docker
$ sudo systemctl restart docker
Copy the code

Iamge image file

Docker needs to package the application program and its related dependencies into an image image file, which can be stored in the cloud repository. Users can download the image file from the cloud to the local, and then generate the instance of Docker container through this file.

So once you’ve packaged your app and its dependencies into an image file, you can upload it to the cloud repository and run it on any Docker machine. It’s a single configuration, running it anywhere, and it’s very convenient.

Docker’s official repository is Docker Hub, which is the most important and most commonly used image repository.

Run a program

Next, we use the hello-world used above to illustrate the use of image.

  1. Look at the localimageThe list of

Use the following command to view the image files that already exist locally, including the image name, label, image ID, creation time, and image size.

# view the local image
$ sudo docker image ls
Copy the code

  1. downloadimageTo the local

Use the following command to download the image file from the cloud to your local computer.

# download image
$ sudo docker pull hello-world
Copy the code

  1. runimage

Image can be run using the following command.

$ sudo docker run hello-world
Copy the code

If you see the print Hello for Docker! The hello-world program is running successfully.

  1. deleteimage

Delete the local image file using the following command.

# remove image
$ sudo docker image rm -f hello-world
Copy the code

Use Chinese official image acceleration

Through Docker official image acceleration, Chinese users can quickly access the most popular Docker images. Hosted in mainland China, local users will now enjoy faster download speeds and greater stability, enabling more agile development and delivery of Docker-based applications.

Docker China official image acceleration can be accessed at registry.docker-cn.com. This mirror library contains only popular public images. Private images still need to be pulled from the US mirror library.

We can pull directly from the mirror acceleration address using the following command:

$ docker pull registry.docker-cn.com/myname/myrepo:mytag

# e.g.$docker pull registry.docker-cn.com/library/ubuntu:16.04Copy the code

To make the changes permanent, modify the /etc/docker-daemon. json file and add a registry-mirrors key value.

{
  "registry-mirrors": ["https://registry.docker-cn.com"]}Copy the code

Save the Settings and restart the Docker for the Settings to take effect. Run sudo service Docker restart to restart the Docker.

Install the Docker Compose

Docker Compose can be downloaded by going to the Compose Repository release page on GitHub, or by using the curl command.

  1. Run the following command to download the current stable version of Docker Compose:
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code
  1. Add permission:
$ sudo chmod +x /usr/local/bin/docker-compose
Copy the code
  1. Verify installation:
$docker-compose --version docker-compose version 1.24.1, build 4667896bCopy the code
  1. Uninstall the Docker Compose:

To delete the docker-conpose file, run the following command:

$ sudo rm /usr/local/bin/docker-compose
Copy the code

If you installed using PIP, run the following command to remove the docker-conpose file:

$ sudo pip uninstall docker-compose
Copy the code

other

There is still a lot of knowledge about Docker, and I will continue to introduce it in the following articles. Welcome to keep following this blog.

For more technical articles, please visit my blog at JemGeek.com

Click to read the original article