Run the application using Docker

Source: Internet
Author: User
Tags sleep custom name docker ps docker hub docker run
With Docker, you can run your application in a container, and running an application in a Docker container requires only a simple docker run command.
Hello Word.
Now try to execute the following command:
$ sudo docker run Ubuntu:14.04/bin/echo ' Hello world '
Hello world!
Congratulations, you have successfully created your first Docker container.
Next we'll look at what the Docker Run command does.
First we run the Docker program and tell the Docker program to execute the command run. The Docker run combination represents running a new Docker container. Next, we specify the image on which the container is to be run: ubuntu:14.04, which is the template in which we run the container, in Docker terms we call the template that creates the container mirror. In this example we use Ubuntu 14.04 as the operating system image. Docker will first look for the mirror on the host based on the name of the specified image, and if it is not found, Docker will download it from the public mirror repository. This public image warehouse is the Docker Hub we mentioned earlier. Next we tell Docker the command to execute in the container that was created:/bin/echo ' Hello world! ' When the container is loaded, Docker creates a new Ubuntu 14.04 environment for us and executes the command in this newly created environment:/bin/echo. So we see the following output on the command line: Hello world!
So the output "Hello world!" What the container we just created will do next. The life cycle of the Docker container is consistent with the life cycle of the commands he executes. In this small example, the output "Hello world!" After the/bin/echo execution is finished, the Docker container also stops.
a container that can be interacted with
Once again, we run the Docker Run command, and this time we run a new command in the container:
$ sudo docker run-t-i Ubuntu:14.04/bin/bash
In this example we still use the Docker Run command to load the ubuntu:14.04 image to create the container. But the difference is that we specified two parameters when we executed the Docker Run command:-T and-I. The-t parameter indicates that a virtual terminal is running in our container, and the-i parameter can redirect the container's standard input output to the host, thus establishing a connection that can interact with the container. At the same time we also specified that the container needs to execute the command for/bin/bash. This command loads the Bash shell of the operating system running in the container. After the command executes successfully, we will see the following command prompt:
Next we try to run the other common commands:
root@af8bae53bdd3:/# pwd
root@af8bae53bdd3:/# ls
Bin Boot Dev etc home lib lib64 media mnt opt proc root run sbin SRV sys tmp usr var
As you can see, we run the PWD command to display the current directory as the root directory, run the LS command to enumerate all subdirectories under the root directory, and we see that the root directory is a common Linux system file. You can run some other common Linux commands in the container. If you need to exit the shell, execute the exit command. For this container we created, executing the exit command means that the command in the container ends, so the shell exits after executing exit, and the container that executes the shell also stops.
Run Hello world! in the background
The ability to run some commands and then exit with the command doesn't seem to be very useful, now we're creating a container that can run in the background, and in fact we want most of the programs that Docker runs to run in the background. We execute the Docker Run command again.
$ sudo docker run-d ubuntu:14.04/bin/sh-c "while true; do echo Hello world; Sleep 1; Done

This time, however, there is no output of "Hello", and we carefully analyze the commands we have just executed. The same Docker run was used as before, but this time we specified the-d command. The-d command tells Docker that the container is running in the background. This time the system image we specified is still ubuntu:14.04 next we specify the command that the container needs to execute:

/bin/sh-c "while true; do echo Hello world; Sleep 1; Done "This is a simple shell footstep program that will loop the output of Hello world. But why don't we see the output Hello World? Instead, it's a long string: 1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
This very long string is called the container identifier, and he identifies the uniqueness of the container. Note: The container ID is long and inconvenient to use, we will introduce a short container ID, but the most convenient and meaningful container name.
We can take a look at the container ID to see what's actually going on in our backend container. First we confirm that the container we created is running. We can do this with the Docker PS command. Docker PS queries all containers that are running in the background and information about the container.
$ sudo docker PS
1e5535038e28 ubuntu:14.04/bin/sh-c ' while tr 2 minutes ago up 1 minute insane_babbage
With the output of the Docker PS command, we can see the Docker container information running in the background. Docker PS Returns a lot of useful information about the container, such as the first column is the shortened container id:1e5535038e28, the second column is the image we used when we created the container: ubuntu:14.04, and also includes the command to run when the container was created. As well as the state of the container and the name assigned to the container automatically insane_babbage. Note: When you create a container, Docker automatically assigns a name to the container, which is later introduced to the container to specify a custom name.
The output from the Docker PS shows that the container we created is working properly, but this does not prove that we have correctly executed the instructions we specified.
To prove this, you need to look at the log information inside the container and view the log information inside the Docker using the Docker logs command.
Use the container name that Docker automatically specifies to view the running logs for the container:
$ sudo docker logs Insane_babbage
Hello World
Hello World
Hello World
. . .
The docker logs command is used to view the running logs inside the container and return to the standard input output, in this case the return of the command after execution is Hello world. So far, the container we created has run successfully in the background. Our Hello World application has been successfully executed in the Docker container. Next we look at how to stop a running container. Stop a running container by using the Docker Stop command.
$ sudo docker stop Insane_babbage
The Docker Stop command tells Docker to stop the container and, if it stops successfully, returns the name of the container. Let's perform the Docker PS command to verify:
$ sudo docker PS
As you can see from the return of the Docker PS command, there are currently no containers running.
Next Step
We've already experienced how easy it is to create a container using Docker. The next step is to explore the high-level theme of Docker on how to manipulate Docker containers.

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: 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.