Docker from introductory to hands-on notes (i)

Source: Internet
Author: User
Tags docker ps

1. Modify an existing image

Start the container with the downloaded image first.

$ sudo docker run-t-I training/sinatra/bin/bash[email protected]:/#

Add JSON and gem two apps to a container.

[Email protected]:/# gem Install JSON

When this is over, we use Exit to exit, and now our container has been changed by us, using the Docker commit command to submit the updated copy.

$ sudo docker commit-m "Added json gem"-a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v24f177bd27a9ff0f6dc2a830403925b5 360bfe0b93d476f7fc3231110e7f71b1c

Where-M to specify the description of the submission, as we use the version Control tool;-A can specify updated user information, followed by the ID of the container used to create the mirror, and 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.

Use Docker images to view the newly created image.

$ sudo docker imagesrepository          TAG     IMAGE ID       CREATED       VIRTUAL sizetraining/sinatra    latest  5bc342fa0b91   hours ago  446.7 Mbouruser/sinatra     v2      3c59e02ddd1a   ten hours ago  446.7 Mbouruser/sinatra     Latest  5db5f8471261   hours ago  446.7 MB

After that, you can use the new mirror to start the container

$ sudo docker run-t-I ouruser/sinatra:v2/bin/bash[email protected]:/#
2. Use Dockerfile to create a mirror

Using Docker commit to extend an image is simple, but not easy to share in one team. We can use the Docker build to create a new image. To do this, you first need to create a Dockerfile that contains instructions on how to create the mirror.

Create a new directory and a Dockerfile

$ mkdir sinatra$ CD sinatra$ Touch Dockerfile

Each instruction in the Dockerfile creates a layer of mirroring, for example:

# This is a commentfrom ubuntu:14.04maintainer Docker newbee <[email protected]>run apt-get-qq UpdateRUN apt-get-q QY Install ruby Ruby-devrun gem install Sinatra

Dockerfile the basic syntax is

    • Use # to Annotate
    • The from directive tells Docker which mirror to use as the basis
    • And then the maintainer's message.
    • The commands at the start of run will run in the creation, such as installing a package, where you can use Apt-get to install some software

You can use the Docker build to generate the image after writing the Dockerfile.

$ sudo docker build-t= "Ouruser/sinatra:v2".
Where the-t flag is added to the tag to specify the user information for the new mirror. "." is the path (current directory) where the Dockerfile is located, or it can be replaced by a specific Dockerfile path.

* Note that an image cannot exceed 127 levels

In addition, you can use the Add command to copy the local file to the mirror, open the port externally with the expose command, and use the cmd command to describe the program that is running after the container starts. For example

# put my local Web site in myApp folder To/var/wwwadd myapp/var/www# expose httpd portexpose 80# the command to Runcmd [ "/usr/sbin/apachectl", "-D", "FOREGROUND"]

You can now start a container with the newly created image.

$ sudo docker run-t-I ouruser/sinatra:v2/bin/bash[email protected]:/#

You can also use the Docker tag command to modify the label of the Mirror.

$ sudo docker tag 5db5f8471261 ouruser/sinatra:devel$ sudo docker images ouruser/sinatrarepository          tag     IMAGE id< c2/>created        VIRTUAL sizeouruser/sinatra     latest  5db5f8471261 hours  ago 446.7   mbouruser/ Sinatra     devel   5db5f8471261  hours ago   446.7 Mbouruser/sinatra     v2  5db5f8471261 Hours ago   446.7 MB

* Note: For more usage, please refer to the Dockerfile section.

3. Save the image

If you want to export the image to a local file, you can use the Docker Save command.

$ sudo docker imagesrepository          TAG                 IMAGE ID            CREATED             VIRTUAL sizeubuntu              14.04               c4ff7513909d        5 weeks ago         225.4 MB ... $sudo docker save-o Ubuntu_14.04.tar ubuntu:14.04
4. Carrying photographed image

You can use Docker load to import from an exported local file to a local mirror library, for example

$ sudo docker load--input Ubuntu_14.04.tar

Or

$ sudo docker load < Ubuntu_14.04.tar

This imports the image and its associated metadata information (including tags, and so on).

5. Remove the local mirror

If you want to remove a local mirror, you can use the Docker RMI command. Note the Docker RM command removes the container.

$ sudo docker rmi Training/sinatra

* Note: Use docker rm to delete all containers that depend on this image before deleting the image.

6. New and started

The required commands are primarily Docker run.

$ sudo docker run-t-I ubuntu:14.04/bin/bash[email protected]:/#

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

When you use Docker run to create a container, the standard operations that Docker runs in the background include:

    • Checks if there is a specified image locally and does not exist to download from the public repository
    • Create and start a container with mirroring
    • Assign a file system and mount a layer of read-write layers outside the mirrored layer
    • Bridging a virtual interface into a container from the bridge interface configured by the host host
    • Configure an IP address from the address pool to the container
    • Executing user-specified applications
    • Container is terminated when execution is complete
7. Start the terminated container

You can use the Docker Start command to start running a container that has been terminated directly.

The core of the container is the application that is executed, and the resources required are required for the application to run. In addition, there are no other resources. You can use PS or top in a pseudo terminal to view process information.

[Email protected]:/# PS  PID TTY time          CMD    1?        00:00:00 bash   ?        00:00:00 PS

Visible, only the specified bash app is running in the container. This feature makes Docker highly utilized for resources and is a genuine lightweight virtualization.

8. Daemon operation

More often, you need to let the Docker container run in the background in a daemon (daemonized) mode. At this point, you can do so by adding the-D parameter.

For example, the following command runs the container in the background.

$ sudo docker run-d ubuntu:14.04/bin/sh-c "while true; do echo Hello world; Sleep 1; Done "1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

When the container starts, it returns a unique ID, or you can view the container information through the Docker PS command.

$ sudo docker pscontainer ID  IMAGE         COMMAND               CREATED        STATUS       PORTS names1e5535038e28  Ubuntu : 14.04  /bin/sh-c ' while TR  2 minutes ago up  1 minute        insane_babbage

To get the output information for a container, you can use the Docker logs command.

$ sudo docker logs Insane_babbagehello Worldhello Worldhello world ...
9. Terminating the container

You can use Docker stop to terminate a running container.

In addition, the container terminates automatically when the application specified in the Docker container is terminated. For example, in the previous section, only a container for a terminal was launched, and when the user exits the terminal through the Exit command or CTRL+D, the container that is created terminates immediately.

The container of the terminating state can be seen with the Docker ps-a command. For example

sudo docker ps-acontainer ID        IMAGE                    COMMAND                CREATED             STATUS                          PORTS               namesba267838cc1b        ubuntu:14.04             "/bin/bash"            minutes ago      Exited (0) about a minute ago                       trusting_newton98e5efa7d997        training/webapp:latest   "python app.py" about an        hour ago   Exited (0) minutes ago                           Backstabbing_pike

The container in the terminating state can be restarted by the Docker Start command.

In addition, the Docker Restart command terminates a running container and then restarts it.

The attach command enters the container.

Docker attach is a command that comes with Docker. The following example how to use this command.

$ sudo docker run-idt ubuntu243c32535da7d142fb0e6df616a3c3ada0b8ab417937c853a9e1c251f499f550$ sudo docker PsCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               names243c32535da7        ubuntu:latest       "/bin/ Bash "         seconds ago up"      seconds                           nostalgic_hypatia$sudo Docker attach nostalgic_hypatia[email protected ]:/#

But using the attach command is sometimes inconvenient. When multiple windows are attach to the same container at the same time, all Windows will be displayed synchronously. When a window is blocked by a command, other Windows cannot perform the action.

11. Exporting and Importing containers

Export container

If you want to export a local container, you can use the Docker Export command.

$ sudo docker ps-acontainer ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               names7691a814370e        ubuntu:14.04        "/bin/bash"         hours ago        Exited (0) hours ago                       test$ sudo docker export 7691a814370e > Ubuntu.tar

This will export the container snapshot to a local file.

Import Container Snapshot

You can use Docker import to import from a container snapshot file into a mirror, such as

$ Cat Ubuntu.tar | sudo docker import-test/buntu:v1.0$ sudo docker imagesrepository          TAG                 IMAGE ID            CREATED              VIRTUAL Sizetest/ubuntu         v1.0                9d37a6082e97 about        a minute ago   171.3 MB

Alternatively, you can import by specifying a URL or a directory, such as

$sudo Docker Import http://example.com/exampleimage.tgz Example/imagerepo

* Note: Users can either use Docker load to import a mirrored storage file to a local mirror library, or you can use Docker import to import a container snapshot to a local mirror library. The difference between the two is that the container snapshot file discards all history and metadata information (that is, only the snapshot state of the container at that time), while the mirrored storage file will hold the full record and be larger in size. In addition, metadata information such as labels can be re-specified when importing from a container snapshot file.

12. Delete a container

You can use Docker RM to delete a container that is in a signaled state. For example

$sudo Docker RM  Trusting_newtontrusting_newton

If you want to delete a running container, you can add the-f parameter. Docker sends a sigkill signal to the container.

Docker from introductory to hands-on notes (i)

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.