Docker Learning Chapter (ii)----basics

Source: Internet
Author: User
Tags terminates phpmyadmin ssh access docker ps docker hub docker run
Introduction

In my previous studies, I learned that the three main components of Docker are----mirrors, containers, and warehouses. Knowing about these three components also gave you a preliminary understanding of Docker. So I learned these three components and recorded them.

Mirror

Docker requires a local mirror to exist when running a container, and the execution process is roughly the same:

    1. Preparing the Container
    2. See if the mirrors required by the container exist on-premises
    3. If present, run the container
    4. If it does not exist, retrieve the remote mirror repository, locate and download
    5. Running the container
Get Mirror

To get mirroring from a mirrored warehouse via Docker Pull:

ubuntu@vm---ubuntu:~$ docker pull ubuntu:latestlatest:Pulling from library/   for Ubuntu:latest
During the download process, each layer of information in the mirror appears.

If the official warehouse registration server download speed is too slow, we can download from other warehouses, this time because it is not from the default registration warehouse download, so we need to explicitly declare the download warehouse address:

sudo docker pull dl.dockerpool.com:ubuntu:latestpulling dl.dockerpool.com:5000 /ubuntuab8e2728644c:pulling dependent layers511136ea3c5a:download complete5f0ffaa9455e:download Completea300658979be:download complete904483ae0c30:download Complete
Show Mirror
Docker images

The above command allows you to view the image files that have been downloaded locally

From the above information, we can obtain:

    • From the warehouse (REPOSITORY)
    • Mirror mark (TAG)
    • ID number Unique identifier (IMAGE ID)
    • Creation Time (CREATED)
    • Mirror Size (sizes)

Tag: Used to mark different images from the same repository, such as multiple mirrors in an Ubuntu repository, to differentiate versions by tag. If you do not remember the specific version, the default tag information with latest

Modifying Mirrors

To start a container with a downloaded image

sudo docker run-t-i training/sinatra/bin/bashroot@0b2616b0e5a8:/#

Add JSON and GEM applications to containers

Install JSON

After the end exits the container through exit, and now the container has the application we just added, the container has been changed by us. To submit an updated copy via Docker commit

sudo " Added JSON gem " " Docker Newbee " 0b2616b0e5a8 ouruser/Sinatra: V24f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

-M to specify the descriptive information for the submission, just like the version control tools we use;

-A can specify updated user information, followed by the ID of the container used to create the mirror;

Finally, specify the warehouse name and tag information for the target image.

The ID information for this image is returned when the creation is successful.

We can use Docker images to view the newly created image.

Dockerfile

When sharing a newly created image in a team, we can use Dockerfile to solve the team sharing problem.

Dockerfile contains instructions on how to create a mirror.

Create a new directory and a dockerfile:

mkdirTouch Dockerfile

Each instruction in Dockerfile creates a layer of mirroring:

# This is a commentfrom ubuntu: 14.04  <newbee@docker.com>RUN apt-get-qq updaterun aptinstall Ruby ruby-   Install Sinatra

Basic syntax:

Annotate with #

The from directive tells Docker which mirror to use as the underlying image

And then the maintainer's message.

Commands that begin with run run in the creation. Like installing a package

In addition, the ADD command is to copy the local file to the mirror, the EXPOSE command to open the port externally, the CMD command to describe the program that is running after the container starts, and so on.

Create a build image by using Docker build after you have finished writing dockerfile.

sudo docker build-t="ouruser/sinatra:v2" .

The-t flag to add the tag, specifying the user information for the new mirror. Note: "." cannot be deleted, it refers to the path where the Dockerfile is located (the current directory), and if Dockerfile is in a different location, you can "." Replace with a specific dockerfile path.

The build process is performing an operation. The first thing it needs to do is upload this Dockerfile content, because all the operations are based on Dockerfile. The docker instruction is executed as a bar, each step creates a new container, executes the instruction in the container, commits the modification, and returns the final ID when all instructions have been executed.

We can modify the label of the image through the docker tag . Pass the docker push to upload the created image to the repository for sharing.

Exporting and Importing mirrors

To export the image locally, you can use the Docker Save command

$sudo docker save-o ubuntu_14. . tar ubuntu:14.04

You can use Docker load to import the local image library from the exported local file

sudo docker load--input ubuntu_14. . Tar

Or

sudo docker load < ubuntu_14. . Tar
Delete a local mirror

To remove a local mirror using the Docker RMI command

Attention:

    • The Docker RM command is used to delete a container, and the operand of the two delete command is different.
    • Deleting a mirror first deletes all containers that depend on the image, or an error may occur.

Container

A container is a set of independent runs or a set of applications and their operating environment. A virtual machine can be understood as a complete set of operating systems (providing a run-state environment and other system environments) and running applications in the simulation run. Therefore, the container is very lightweight compared to the virtual machine, and the start-up speed is also calculated in seconds.

Start

There are two forms of container initiation:

    • One is to create a new container based on the image and start
    • The other is to restart the container in the terminating state

The start container command is implemented with Docker run , as in the following example:

sudo Docker run Ubuntu:16.04 /bin/echo'helloworld'Hello World

Launch a bash terminal that allows user interaction:

sudo Docker run-t-i ubuntu:16.04 /bin/bashroot@af8bae53bdd3:/#

The-t option allows Docker to assign a pseudo-terminal (pseudo-tty) and bind it to the container's standard input, and-I keeps the container's standard input open.

When you execute Docker run to create a container, the process for Docker is:

    1. Checks if there is a specified image locally, if it is not downloaded from the shared warehouse
    2. Creates a container with the specified base image
    3. Allocates a filesystem and mounts a layer of read-write layers outside the read-only mirroring layer
    4. Bridging a virtual interface to a container from the bridge interface configured by the host host
    5. Configure an IP address from the address pool to the container
    6. Executing user-specified applications
    7. Close container

So how do you start a container that has been terminated before? We can use the Docker start command to restart a terminated container.

The core of the container is the application that is executed, and the resources required are required for the application to run. In the pseudo terminal that interacts with the container, we can view the process information in the container through the PS or the top command.

Daemon operation

We want to have a container run in the background as a daemon. You can increase the-D parameter when you execute the Docker Run command.

sudo docker run-d ubuntu:16.04 /bin/sh"while true; do echo Hello world; Sleep 1; done"1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

The container's ID is returned when the container is run in the daemon, and we can view the container information through Docker PS .

sudo PS CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS names1e5535038e28  ubuntu:14.04  /bin/ SH ' While tr  2 minutes ago up  1 minute        insane_babbage

Note: To see all containers that are not removed, you need to add the-a parameter.

You want to see the information that the daemon container outputs in the background, but use docker logs to view it:

$ docker logs Insane_babbagehello Worldhello world ...
inside the container

When Plus-D runs the container, the container executes in the background as a daemon, but sometimes we want to get into the container to work, how should it be implemented??

1. We can use Docker's own instruction Docker attach

sudo PS CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               names243c32535da7        ubuntu:latest       "  /bin/bash"          seconds ago        seconds                           nostalgic_hypatia$sudo  docker attach nostalgic_hypatiaroot@243c32535da7:/#

But how many windows go into a container at the same time? All Windows will be displayed synchronously at this point, and if a window is blocked, other windows will not work, so attach is very inconvenient.

2. Nsenter command

We can enter the container through the Nsenter command, but instead use the Nsenter command to install the Nsenter tool, the installation steps are as follows:

$ CD/tmp; $ curl https://$ cd util-linux-2.24; $. /configure--without-makesudocp Nsenter/usr/local/bin

Note: The Nsenter tool is included in the Util-linux pack 2.23 version, if you have a Util-linux package in your computer, first check if the version is too low. Ubuntu14.04 is using version 2.20 of the Util-linux package.

In order to connect to a container, we need to know the PID of the first process of the container, which is obtained by the following method:

" {{ . State.pid}}" <container>)

To connect the container through this PID:

Nsenter--target $PID--Mount --uts--ipc--net--pid
Terminate Container

Use the docker stop instruction when we need to stop a container that is executing.

If the application specified in the container terminates, the container also terminates automatically. For example, we let the container run a pseudo terminal to interact with the user, and when exiting the pseudo terminal via exit, the container also terminates automatically.

If you want a running container to terminate and reboot, we can use the docker restart instructions.

Export and Import

Export

It's also easy to back up the containers in Docker, and you can export them through Docker

sudo docker export 7691a814370e > Ubuntu. Tar

We need to know the ID of the container at the time of export.

Import

You can use the Docker import directive to import a container snapshot as a mirror.

Cat Ubuntu. Tar | Docker Import-test/ubuntu:v1. 0

We can also get the directory path by specifying the URL to import:

$sudo Docker import http://example.com/exampleimage.tgz Example/imagerepo
the difference between load and import

Docker load is used to import a mirrored storage file into a local mirror library; Docker import is to import a container snapshot to the local mirror library. The difference is that the container snapshot file discards all history and metadata information (that is, the snapshot state of the container at that time), and the image file saves the full record and is larger in size.

Delete Container

When we want to delete a container, we can use the Docker RM directive to delete a container with a terminating state. If we want to remove a running state, we can add the-f parameter.

Warehouse

Warehouses are places where mirrors are stored.

Docker currently maintains a public warehouse Docker Hub (hub.docker.com/), which already includes more than 15000 mirrors. Most of the requirements can be achieved by directly downloading the image in the Docker Hub.

Login

Register and log in by entering your username, password and mailbox via the Docker login command. The user's authentication information is stored locally in the. dockercfg.

Retrieval

We want to find out what images we need in our official warehouses, which can be implemented through Docker search instructions and downloaded to the local mirror repository via Docker pull.

ubuntu@vm- the- One-ubuntu:~$ docker Search Ubuntuname DESCRIPTION STARS official Automatedubuntu Ubunt U is a Debian-based Linux Operating sys ...8174[OK] Dorowu/ubuntu-desktop-lxde-vnc Ubuntu with Openssh-server and NoVNC208[Ok]rastasheep/ubuntu-sshd dockerized SSH Service, built on top of offi ...167[Ok]consol/ubuntu-xfce-vnc Ubuntu container with"Headless"VNC session ... the[Ok]ansible/ubuntu14.Geneva-ansible Ubuntu14.04LTS with Ansible94[Ok]ubuntu-upstart Upstart is an event-based replacement forTh ... the[OK] Neurodebian Neurodebian provides n Euroscience ... the[OK] 1and1internet/ubuntu- --nginx-php-phpmyadmin-mysql-5ubuntu- --nginx-php-phpmyadmin-mysql-5           +[Ok]ubuntu-debootstrap debootstrap--variant=minbase--components=m ... the[OK] Nuagebec/ubuntu simple always updated Ubuntu Docker imagesW... at[Ok]tutum/ubuntu simple Ubuntu docker images with SSH access -i386/ubuntu Ubuntu is a debian-based Linux operating sys ... -      

Above ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.