The introduction

I believe that many technical students will use virtual machines when developing, configure a development environment, later use only need to start the virtual machine. But virtual machines can be several gigabytes or even hundreds of gigabytes, and if any of the virtual machine files are corrupted, the entire virtual machine cannot start. You might say you should always back up, which is a good idea. But is there a better way? Today, I’m going to take you through Docker and see if it’s better for us than traditional virtual machines.

What is a container?

As the name suggests, containers are for holding things. The cup we drink from is a container, but the cup is a container for water, and our container is an app.

What are the characteristics of containers?

  • Self-inclusion: It packages all the dependencies of the application and can be run directly;
  • Portability: Containers can be run the same way almost everywhere, ensuring that you can have exactly the same running environment in development, test, and production environments;
  • Isolation: Multiple containers are isolated from each other by default, even if they are running on the same host.
  • Lightweight features: second startup, less resources;

What is the difference between containers and virtual machines?

A lot of students will think, well, a container can do everything a virtual machine can do, so what’s the difference?

Disadvantages of virtual machines

  • Occupy more resources;
  • Redundant steps;
  • Start slow;

Advantages of containers

  • Occupy less resources;
  • Referring to small;
  • Start the fast;

Below is a screenshot of Docker’s official website (I’ll explain what Docker is later)

From this figure, we can see that traditional virtual machines are very heavy, and each VIRTUAL machine is an independent operating system. Docker, on the other hand, reuses existing system resources of the host while perfectly isolating different containers, so it is very light to implement and easy to be standardized. Some students will say, this is not fundamentally different from the traditional virtual machine ah, the new virtual machine. It’s this “lightweight” nature that gives it the opportunity to become the new standardized way to distribute apps.

Containers came along in the 1950s and 1960s, and they didn’t seem very technical. However, it is precisely because container is a standardized logistics mode that the global sea, land and air transportation, dock loading and unloading have formed a whole efficient logistics system around container, which ultimately changed the world trade and contributed to globalization.

Google’s Kubernetes (K8) is now the standard for instant container choreography, along with Docker Swarm and Marathon/Mesos.

What is a Docker?

So, finally, what is a Docker? Docker is a Linux container package developed using Go language, providing easy-to-use interface, is the most popular Linux container solution.

Docker usage scenarios

  • Create a consistent development, test, and production environment;
  • Create a resource-isolated runtime environment;
  • Create multi-user platform as a Service (PaaS) infrastructure;
  • Create software as a Service (SaaS) applications;
  • High-performance, super-large scale host deployment;

Docker is an open source commercial product available in two versions: Community Edition (CE) and Enterprise Edition (EE). The Enterprise edition includes some paid services that individual developers generally don’t use. The following introductions are for the community edition.

Common Docker commands

View the Docker version

docker version

Pull the Docker image

We can go to the Docker Hub site to pull the public Docker image. For example: search nginx and pull up the official nginx image.

docker pull nginx

View the Docker image

docker images

Run the Docker image

docker run -it -v /Users/kwang/docker:/usr/share/nginx/html/hello -p 80:80 -d nginx:latest

-I runs the container in interactive mode, usually with -t; -t reassigns a pseudo-input terminal to the container, usually used in conjunction with -i. -p Local port: container port mapping. -d The background runs and returns the container ID. -v maps native directories into containers. As here I put my native/home/kwang/docker mapped to/usr/share/nginx/HTML/hello/directory;

I created a static page index. HTML in /home/kwang/docker-/, which simply prints Hello World! . The /usr/share/nginx/html/ directory is the root directory of the nginx web page in the container. This is set up to demonstrate directory mapping.

On success, the command line returns a Docker container ID (this ID is randomly generated, so you’ll probably see something different than mine).

View the Docker container in action

docker ps -a

We can see that the image has been successfully started and that port 0.0.0.0:80 has been successfully mapped to port 80 in the container, the first bits of which are 7fcAC910AD6A

Open your local browser and type: http://localhost:80 to see that Nginx is already started

If you change your browser address to http://localhost/hello/, you can see the index.html THAT I prepared.

Pause the running Docker container

docker stop 7fcac910ad6a

7fcAC910AD6a is the container ID to pause. You can see that the container state has changed to Exited exit.

Start the Docker container that has been paused

docker start 7fcac910ad6a

Delete the running Docker container

docker rm -f 7fcac910ad6a

The -f parameter is forcibly deleted.

Enter the container in command line mode

You can use command line mode to access the container as if you were logging into a new Linux.

docker exec -it 9ca4f91d4027 bash

Exec is running a command in a running container that takes two arguments. The first argument is the container ID (9CA4f91D4027 in this case), and the second argument is the command to execute (bash in this case). After executing, we went inside the container in bash command line mode.

Of course, you can always exit the container with the exit command.

Generate a Docker image from a running Docker container

Docker commit -m “Kenny nginx” -a” Kenny “9CA4f91d4027 Kenny /nginx:1.0

-m indicates description information. -A indicates user information. Kenny /nginx:1.0 indicates the user name, repository name, and tag information of the mirror

You can see that the Docker image has been successfully generated.

Generate Docker image based on Dockerfile

We can create a file called Dockerfile and edit it as follows:

# which Docker image to base a new image on

FROM nginx:latest

Basic builder information

MAINTAINER kenny.wang

# What to do when you build this image

RUN apt-get update

Copy the local file to the image

COPY ./index.html /usr/share/nginx/html/

Run the build command to generate a Docker image.

Docker build – t = “Kenny/nginx: 2.0”.

-t is used to specify user information, tag, etc. Is the current directory and is used to find dockerfiles

Using Docker Images again, the new image has been successfully generated.

Delete a Docker image

docker rmi -f 83a85d2939a2

-f forcibly deletes the docker image ID 83A85D2939A2

Save the Docker image as a tar file

Docker save -o kenny_nginx.tar Kenny /nginx:2.0

Load the Docker image

docker load -i kenny_nginx.tar

conclusion

Docker is a good thing, and these are just a few of the basic operations that are commonly used, but it’s like a gateway to a new world that opens up the possibility of large-scale clustered deployments