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

Detailed explanation of Javascript event capture and bubbling methods

Table of contents 1. Event Processing Model 1. Ev...

Detailed explanation of viewing and setting file permissions on Mac

Preface To modify file permissions in the termina...

Let's talk in detail about how the NodeJS process exits

Table of contents Preface Active withdrawal Excep...

Detailed process of building mongodb and mysql with docker-compose

Let's take a look at the detailed method of b...

Style trigger effect of web page input box

<br />This example mainly studies two parame...

Vue Basics Introduction: Vuex Installation and Use

Table of contents 1. What is vuex 2. Installation...

How to implement https with nginx and openssl

If the server data is not encrypted and authentic...

Docker implements cross-host container communication based on macvlan

Find two test machines: [root@docker1 centos_zabb...

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

Methods of adaptive web design (good access experience on mobile phones)

1. Add the viewport tag to the HTML header. At th...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Detailed steps for building a React application with a Rails API

Table of contents Backend: Rails API part Front-e...

Docker runs operations with specified memory

as follows: -m, --memory Memory limit, the format...