What is a Docker?

Docker is an open source container project based on go language. It was born in the beginning of 2013, and the initial initiator is dotCloud.

Docker project has joined the Linux foundation, and complies with the Apache2.0 protocol, all the code is maintained on github.

The idea of Docker is to achieve Build Ship and Run Any app anywhere, that is, through the encapsulation, distribution, deployment and operation life cycle management of applications, so as to achieve the purpose of one-time encapsulation and running anywhere of application components.

Application components are not limited to Web applications. They can be a compilation environment, a database platform service, or even an operating system and cluster.

The development of Docker is built on Linux container technology.

Why Docker?

In the cloud era, applications can be accessed anytime, anywhere, without the constraints of the underlying physical hardware. This allows for rapid distribution and deployment. Docker is used to package apps and decouple apps from running platforms. Reduce development costs and deployment risks.

Docker advantages

  • Faster delivery and deployment
  • More efficient use of resources
  • Easier migration and scaling
  • Easier update management

Docker core concepts

Image: A read-only template that contains a basic operating system container: a lightweight sandbox that creates a running instance of the application from the image. You can start to stop deletion and isolate containers from each other. Repository: Similar to a code repository, it is a centralized place to store image files.

Docker Image – Docker Image

How do I use Docker images

Before Docker can run the container, the corresponding image needs to exist locally. If the image is not saved locally, Docker will try to download it from the default image repository first. Using the Docker pull command, you can download the image directly from the Docker Hub image source.

Docker pull NAME[:TAG] NAME: NAME of the image TAG: version of the imageCopy the code

Eg: In summary, the information required for a mirror is the name + label

Docker pull Ubuntu :14.04 14.04: Pulling from Library/Ubuntu bae382666908: Pull complete 29ede3c02FF2: Pull complete da4e69f33106: Pull complete 8d43e5f5d27f: Pull complete b0de1abb17d6: Pull complete Digest: sha256:6e3e3f3c5c36a91ba17ea002f63e5607ed6a8c8e5fbbddb31ad3e15638b51ebc Status: Downloaded newer imageforUbuntu: 14.04Copy the code

If the specified TAG is not displayed, the latest TAG is selected by default. The latest version of the image is downloaded.

Boot image

Once downloaded locally, you can use the image to create a container

Docker run it Ubuntu :14.04 bash /* -i -t */ root@4a9c106836e1:/#
Copy the code

The bash screen is displayed, where you can perform some basic operations

View information about a mirror

docker images REPOSITORY TAG IMAGE ID CREATED SIZE calculatorapp latest 1dec489fae9a 12 hours ago 955MB <none> <none> 2dab0d648733 6 days ago 755MB node 6.11.1 1FFBFD4a58EC 2 months ago 656MB Parameter Docker iamges-aList all mirrorsCopy the code

Run the Tag command to add a mirror Tag

docker tag ubuntu:lastest myubuntu:lastest REPOSITORY TAG IMAGE ID CREATED SIZE myubuntu lastest dea1945146b9 2 weeks Ubuntu 14.04 DEA1945146b9 2 weeks ago 188MBCopy the code

In fact, you see that the IMAGE ID is the same, so it actually points to the same IMAGE file.

Remove the mirror

1. Delete by label

docker rmi IMAGE[IMAGE...]
Copy the code

eg:

REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
myubuntu                               lastest             dea1945146b9        2 weeks ago         188MB

docker rmi myubuntu:latest
Copy the code

2. Delete the vm by ID

docker rmi ID
Copy the code
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
myubuntu                               lastest             dea1945146b9        2 weeks ago         188MB

docker rmi dea1945146b9
Copy the code

Note that when deleting, if no container uses the image, it will delete the image and all the layers it contains. However, if a container uses the image, it will be prompted

docker rmi myubuntu:lastest
Error response from daemon: conflict: unable to remove repository reference "myubuntu:lastest" (must force) - container 4a9c106836e1 is using its referenced image dea1945146b9
Copy the code

There are two ways to solve this problem:

  • use-fForcibly Delete parameters (not recommended)
  • Delete the container first, then delete the image

Create a mirror image

There are three ways to create an image:

  • Create a container based on an existing image
  • Import based on local templates
  • Create from Dockerfile

Docker container – Docker container

A container is a running instance of an image.

Container to create

The docker create container is stopped and can be started by using the docker start command

docker create
-dWhether to run in the backgroundCopy the code

eg:

docker create -it ubuntu:latest
bb821ea158758312d0832d2c971aab1665cce73541f04f22ca292f979c871e2e

docker ps -a
CONTAINER ID        IMAGE               COMMAND          CREATED             STATUS                     PORTS               NAMES
bb821ea15875        ubuntu:latest       "/bin/bash"      3seconds ago        Created                                        adoring_archimedes
Copy the code

Start the container

Use the docker start command to start a container that has been created

docker start [container_name/container_id]
Copy the code
CONTAINER ID        IMAGE               COMMAND          CREATED             STATUS                     PORTS               NAMES
bb821ea15875        ubuntu:latest       "/bin/bash"      3seconds ago        Created                                        adoring_archimedes

docker start bb821ea15875
or 
docker start adoring_archimedes
Copy the code

View running containers

Use the docker ps command to view running containers

docker ps 
-aView all containersCopy the code

Create and start the container

docker run = docker create + docker start

Docker run -i keeps the container's standard input open. -t allows docker to assign a pseudo-tty to the container's standard input and bind it to itCopy the code

eg:

Docker run -it Ubuntu :14.04 /bin/bash Start a bash terminal on Ubuntu 14.04 and allow user interaction to use itexitOr CTRL + D to exit the containerCopy the code

Daemon running (background running)

docker run -dUbuntu 14.04 / bin/bash-dThe container ID is displayed in the backgroundCopy the code

Note that if it is running in the background, you can use docker logs [container_name/container_id] to obtain the output of the container

Termination of the container

docker stop [container_name/container_id]
Copy the code

Into the container

With the -d argument, the container startup goes into the background. The user cannot see or operate on the information in the container

Three ways to solve:

  • attachCommand (not recommended)
  • execCommand (recommended)
  • Nsenter tools (not recommended)
docker execIt [container_name/container_id] /bin/bash -i Enables the standard input to accept the user's input -t asks docker to assign a pseudo-tty and bind it to the container's standard input -u Specifies the user who executes the commandCopy the code

Remove the container

docker rm [container_name/container_id]
-fForce termination and removal of a running containerCopy the code

Docker Repository – Docker Repository

Warehouse:

  • Public warehouse
  • Private warehouses

Docker Hub: Docker’s official public image repository

Docker data management

Containers manage data in two ways:

  • Data Volumes: Data in containers is mapped to the local host environment
  • Data Volume Containers: Use specific Containers to maintain Data volumes

Data volume

What is a data volume? A special directory available to containers that maps the host operating system directory directly into the container

Advantage:

How do I create a data volume?

Three ways:

  • Create a data volume inside the container
  • Mount a host directory as a data volume
  • Mount a local host file as a data volume (not recommended)

Create a data volume inside the container

Creating a data volume within the container uses the -v argument, essentially creating a folder within the container

Docker run -v /webapp Ubuntu // Create a folder inside the container -v Bind mount a volumeCopy the code

Eg:

docker run -it ubuntu
root@f22d574bb63f:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

docker run -it -v /webapp  ubuntu
root@c3acf065e9c2:/# lsBin boot dev etc home lib lib64 media MNT opt proc root run sbin SRV sys TMP usr var webapp // A webapp folder is addedCopy the code

Mount a host directory as a data volume (recommended)

Also using the -v parameter,

Docker run -v/SRC /webapp:/opt/webapp ubuntu // Mount the local/SRC /webapp directory to /opt/webapp inside the containerCopy the code

The local directory path must be an absolute path. If the destination directory does not exist, Docker will create it automatically. But if the target exists,??

Docker mounted data volumes have read and write (RW) permissions by default and can be changed

Data volume container

Creating a data volume container can share continuously updated data between multiple containers.

How do I create it?

Docker run it --name dbdata -v /dbdata Ubuntu // Create a dbdata folder as the shared data volume --name give the container a nameCopy the code

How can other containers use this shared data volume?

Docker runit --volumes-from dbdata --name db1 Ubuntu // Run volumes from Mount to run a new volume named dbdata Volumes from the Specified Container (s) To mount volumes from the specified ContainerCopy the code

Use container volumes to back up data. 1

$ docker run --volumes-from dbdatasrc -v $(pws):/backup --name worker ubuntu tar cvf /back/backup.tar /dbdata // Mount the dbdatasrc internal data volume (/ dbData) to the new Ubuntu container (named worker container), At this point, there will be a data volume of/dbData inside the worker container. // Meanwhile, all data inside/dbData will be compressed and packaged to the /back directory under the worker container. Implement data backup.Copy the code

Port mapping and container interconnection

Accessing container applications externally (port mapping)

If no parameter is specified when the container is started, network applications and services in the container cannot be accessed from outside the container.

How do I give external access to the container? 1. When using the -p flag with -p uppercase P, Docker will randomly map a port from 49,000 to 49900 to the container’s open network port:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
5ad0d233c126        training/webapp     "python app.py"46 seconds ago Up 44 seconds 0.0.0.0:32768->5000/ TCP dazzling_kirchCopy the code

2. Run -p lowercase p to specify the format supported by the port to be mapped

HostPort:Container | Ip:HostPort:Container  | Ip::ContainerPort
Copy the code

The first HostPort is Container, which maps the local port to the specified port of the Container and maps all interface addresses

docker run --name test1 -p 5000:5000 training/ webApp Python app.py // Mapping the local 5000 port to the CONTAINER 5000 port CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7f8a05e63a9 training/webapp"python app.py"3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/ TCP ecstatic_einstein docker run -p 55:5000-p 3000:80 UbuntuCopy the code

In this case, ports 5000 in all IP segments are bound to port 5000 of the container

The second IP: HostPort: ContainerPort to specify the local IP port mapping to the container to the specified port number on the specified port mapping to the specified address

docker run --name test2 -p 127.0.0.1:55:5000 training/ webApp Python app.py // Map the local IP address 127.0.0.1 5000 to 5000 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd4e352c3b48 training/webapp"python app.py"2 minutes ago Up 2 minutes 127.0.0.1:5000->5000/ TCPtest1
Copy the code

The third type of Ip::ContainerPort maps to any port at the specified address

docker run --name test127.0.0.1::5000 training/ webApp Python app.py // Bind any port 127.0.0.1 to port 5000 of the container. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES B804ACC891DF training/ WebApp"python app.py"5 seconds ago Up 3 seconds 127.0.0.1:32768->5000/ TCPtest3
Copy the code

Check the port mapping configuration

docker port [container_name/container_id] eg: Docker port b804ACC891df 5000/ TCP -> 127.0.0.1:32768 // indicates that 5000 containers are mapped to localhost 127.0.0.1:32768 or: Docker port B804ACC891df 5000 127.0.0.1:32768Copy the code

Container interconnection

Container interconnection is a way for applications in multiple containers to quickly interact with each other. It creates a connection between the source and the receiving container. The receiving container can quickly access the source container using the container name without specifying an IP address.

Create a custom container name. When creating a container, use the –name parameter, otherwise the system will randomly generate a name.

Using the –link parameter allows containers to safely interact with each other. eg:

1. Create a new database container $docker run-d--name db-container training/postgres 2. Creating a new Web container can access the database container $docker run-d -P --name web --link db-container:db training/webapp python app.py
// --link name:alias//name: the name of the container to be connected //alias: alias for the connection What's the use? docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c5797ccc4f95 training/webapp"python app.py"2 hours ago Up 2 hours 0.0.0.0:32769->5000/ TCP web cDD159304AF3 training/postgres"su postgres -c '/..."   2 hours ago         Up 2 hours          5432/tcp                  db-container
Copy the code

Docker creates a virtual channel between two interconnected containers without mapping their ports to the host host.

Docker exposes connection information for containers in two ways:

  • Updating environment variables
  • Update the /etc/hosts file

If two containers are interconnected, there must be some way to connect them. Db-container is equivalent to the parent container. Its hosts information is as follows: /etc/hosts of db-Container:

127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet FF00 ::0 ip6-mcastPrefix ff02::1 Ip6-allnodes FF02 ::2 IP6-allRouters 172.17.0.2 CDD159304AF3 <= The ID of their own container and address of the routersCopy the code

The web container connected to it is not quite the same: /etc/hosts

127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet FF00 ::0 ip6-mcastPrefix ff02::1 Ip6-allnodes FF02 ::2 IP6-allRouters 172.17.0.2 db db-container CDD159304AF3 <= -- Link parameter db-container: DB 172.17.0.3 C5797ccc4f95 <= ID of its own containerCopy the code

The resources

Book: Introduction and Practice of Docker Technology