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

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

How MLSQL Stack makes stream debugging easier

Preface A classmate is investigating MLSQL Stack&...

Detailed explanation of the process of using docker to build minio and java sdk

Table of contents 1minio is simple 2 Docker build...

js to realize a simple puzzle game

This article shares the specific code of js to im...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...

Sample code for implementing Alipay sandbox payment with Vue+SpringBoot

First, download a series of things from the Alipa...

JavaScript data flattening detailed explanation

Table of contents What is Flattening recursion to...

Detailed explanation of Docker container data volumes

What is Let’s first look at the concept of Docker...

How to solve mysql error 10061

This article shares with you the solution to the ...

Detailed steps for installing, configuring and uninstalling QT5 in Ubuntu 14.04

1. I downloaded QT5.13 version before, but after ...

Solution to inserting a form with a blank line above and below

I don't know if you have noticed when making a...

The whole process of configuring reverse proxy locally through nginx

Preface Nginx is a lightweight HTTP server that u...

MySQL enables slow query (introduction to using EXPLAIN SQL statement)

Today, database operations are increasingly becom...