How to create, start, and stop a Docker container

How to create, start, and stop a Docker container

1. A container is an independently running application or a group of applications and their operating environment. Container is an important concept in Docker.

2. There are three ways to start a docker container

a. Interactive mode: create a new container based on the image and start it

For example, we can start a container and print out the current calendar table

[root@rocketmq-nameserver4 ~]# docker run my/python:v1 cal ##my/python:v1 is the image name and tag 

We can also start a bash interactive terminal by specifying parameters.

[root@rocketmq-nameserver4 ~]# docker run -it my/python:v1 /bin/bash 


The -t parameter tells Docker to allocate a pseudo terminal and bind it to the container's standard input, and the -i parameter keeps the container's standard input open.

Use the docker run command to start the container. The standard operations that docker runs in the background include:

1. Check whether the specified image exists locally. If not, download it from the public warehouse
2. Create and start a container using an image
3. Allocate a file system and mount a readable and writable layer outside the read-only image layer
4. Bridge a virtual interface from the bridge interface configured on the host to the container
5. Assign an IP address to the container from the address pool
6. Execute the application specified by the user
7. The container is terminated after execution


my/sinatra:v2 is a modified image based on the training/sinatra image. training/sinatra is an image on a public repository.

b. Short-term method : directly start a terminated container

You can use the docker start command to directly start a terminated container.

[root@rocketmq-nameserver4 ~]# docker run my/python:v1 /bin/echo hello test 
hello test

After the command is executed, the console will print "hello test" and the container will terminate, but it will not disappear. You can use "docker ps -n 5" to view the latest 5 containers. The first one is the container just executed. You can execute it again: docker start container_id

However, this time the console cannot see "hello test", only the ID can be seen, which can be seen using the logs command: docker logs container_id. You can see two "hello test" because the container was run twice.

c. Daemon mode, running in guard mode

That is, let the software run as a long-term service, this is SAAS!

For example, we start the centos background container and print the calendar of the day every second.

$ docker run -d centos /bin/sh -c "while true;do echo hello docker;sleep 1;done"

After starting, we use docker ps -n 5 to view the container information

To view the output in the started centos container, you can use the following method:

$ docker logs $CONTAINER_ID ##View the output outside the container $ docker attach $CONTAINER_ID ##Connect to the container and view it in real time:

3. Terminate the container

Use docker stop $CONTAINER_ID to terminate a running container. And you can use docker ps -a to view the terminated containers.

A terminated container can be restarted using docker start.

Use the docker restart command to restart a container.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of how to enter and exit the Docker container
  • Restart the Docker service to apply the automatic start and stop command (recommended)
  • How to keep running after exiting Docker container

<<:  Sample code for implementing multiple selection based on nested Table in ElementUI

>>:  View MySQL installation information under Linux server

Recommend

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

CSS3 achieves cool sliced ​​image carousel effect

Today we will learn how to use CSS to create a co...

Solution to occasional crash of positioning background service on Linux

Problem Description In the recent background serv...

MySQL automatically inserts millions of simulated data operation code

I use Navicat as my database tool. Others are sim...

Webservice remote debugging and timeout operation principle analysis

WebService Remote Debugging In .NET, the remote d...

Some suggestions on Vue code readability

Table of contents 1. Make good use of components ...

Detailed explanation of MySQL Workbench usage tutorial

Table of contents (I) Using Workbench to operate ...

Introduction to the use of the indeterminate property of the checkbox

When we use the folder properties dialog box in Wi...

A good way to improve your design skills

So-called talent (left brain and right brain) Tha...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

Example analysis of interval calculation of mysql date and time

This article uses an example to describe the inte...

How to block and prohibit web crawlers in Nginx server

Every website usually encounters many non-search ...