Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods for creating, starting, and stopping Docker containers

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

A container is an application or set of applications that run independently, along with the environment in which they operate. Containers are an important concept in Docker.

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 launch a container to print out the current calendar.

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

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

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

The-t argument causes Docker to assign a pseudo-terminal and bind it to the container's standard input, and-i causes the container's standard input to remain open.

Use the docker run command to start the container. Standard actions docker runs in the background include

1. Check whether the specified image exists locally. If not, download it from the public repository.

2. Create and start containers using mirrors

Allocate a file system and mount a readable and writable layer outside the read-only mirror layer

4. Bridge a virtual interface from the bridge interface configured by the host to the container

5. Assign an ip address from the pool to the container

6. Executes user-specified applications

7. After execution the container is terminated

my/sinatra:v2 Modified mirror based on training/sinatra mirror, training/sinatra is mirror on public repository.

b. Short-term mode, directly starting and running a terminated container

You can use the docker start command to start a container that has been terminated.

[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 look at the latest top 5 containers. The first one is the container just executed. You can execute it again: docker start container_id

However, this time the console does not see the "hello test", only the ID can be seen, and the logs command can be used to see: docker logs container_id. You can see two "hello tests" because the container runs twice.

c, daemon mode, guard mode operation

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

For example, we start the centos backend 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 startup, we use docker ps -n 5 to view container information

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

$ docker logs $CONTAINER_ID ##View its output outside the container $ docker attach $CONTAINER_ID ##Connect to the container for real-time viewing:

3. Terminate the container

Use docker stop $CONTAINER_ID to terminate a running container. And you can use docker ps -a to look at containers in the termination state.

Terminated containers can be restarted using docker start.

Use the docker restart command to restart a container.

The above is all the content of this article, I hope to help everyone's study, but also hope that everyone a lot of support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report