April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Detailed Docker container common operations

First, start the container

There are two ways to start a container. One is to create a new container based on the image and start, and the other is to restart the container in the terminated state. 
Because Docker’s containers are too lightweight, users often delete and recreate containers at any time.

New and start

For example, the following command outputs a “Hello World” and then terminates the container.

$ docker run Ubuntu :14.04 /bin/echo ‘Hello world’ 
Hello world

This is almost indistinguishable from directly executing /bin/echo ‘hello world’ locally.

The following command launches a bash terminal that allows the user to interact.

$ docker run -t -i ubuntu:14.04 /bin/bash 
root@af8bae53bdd3:/#

The -t option causes Docker to assign a pseudo-tty and bind to the container’s standard input, and -i keeps the container’s standard input open.

When using docker run to create containers, the standard operations that Docker runs in the background include:

  • Check if the specified image exists locally. If it does not exist, download it from the public repository.
  • Create and launch a container with an image
  • Allocate a file system and mount a readable and writable layer outside the read-only mirror layer
  • Bridge a virtual interface into the container from the bridge interface configured by the host host
  • Configure an ip address from the address pool to the container
  • Execute a user-specified application
  • The container is terminated after execution

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

Second, the guardian state runs

More often, you need to have Docker run in the background instead of directly outputting the results of the execution command under the current host. This can be done by adding the -d parameter.

$ docker run -d ubuntu /bin/sh -c “while true; do echo hello world; sleep 1; done” 
cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a

A unique id is returned when started with the -d parameter. The output can be viewed with docker logs [container ID or NAMES]. If you do not use the -d parameter. The output result (STDOUT) will be printed on the host

View container information with the docker container ls command.

$ docker container ls 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
cb30b87566d0 ubuntu “/bin/sh -c ‘while t…” 2 minutes ago Up 2 minutes goofy_mcclintock

To get the output of the container, you can use the docker container logs command.

$ docker container logs goofy_mcclintock 
hello world 
hello world 
hello world 
……

Note: Whether the container will run for a long time is related to the command specified by docker run, regardless of the -d parameter.

Third, terminate the container

You can use the docker container stop to terminate a running container. The format is: 
docker container stop [options] CONTAINER [CONTAINER…]

In addition, when the application specified in the Docker container is terminated, the container is also automatically terminated. 
For example, only the container of one terminal is started. When the user exits the terminal through the exit command or Ctrl+d 
, the created container is terminated immediately.

The container for the terminated state can be seen with the docker container ls -a command. E.g

$ docker container stop goofy_mcclintock 
goofy_mcclintock

$ docker container ls -a 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
cb30b87566d0 ubuntu “/bin/sh -c ‘while t…” 20 minutes ago Exited (137) 23 seconds ago goofy_mcclintock

The container in the terminated state is started by the docker container start command; the 
container of a running state is terminated and restarted by the docker container restart command.

Fourth, enter the container

When the -d parameter is used, the container will enter the background after it starts. 
Use the docker attach command or the docker exec command to enter the container. It is recommended to use the docker exec command for reasons explained below.

The attach command 
docker attach is a command that comes with Docker. The following example shows how to use this command.

$ docker run -dit ubuntu 
e1ffd4f792fe0ce7f7e700147051e1f792e352f5b70929eb9376393ac20114b4

$ docker container ls 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
e1ffd4f792fe ubuntu “/bin/bash” About a minute ago Up About a minute awesome_payne

$ docker attach e1ff 
root@e1ffd4f792fe:/#

Note: If exit from this stdin, it will cause the container to stop.

The exec command 
-i -t parameter 
docker exec can be followed by multiple parameters, here mainly the -i -t parameter. 
When only the -i parameter, since there is no allocation of pseudo-terminals, the interface is not familiar Linux command prompt, the command execution 
line results can still be returned. 
When the -i -t parameter is used together, you can see the Linux command prompt we are familiar with.

$ docker run -dit ubuntu 
16168d4b66b115b5afac5836db3ff93304774e98489f628ac625fff2bcd640ba

$ docker container ls 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 
16168d4b66b1 ubuntu “/bin/bash” 58 seconds ago Up 57 seconds happy_bardeen

$ docker exec -it 16168 bash 
root@16168d4b66b1:/#

Exit from this stdin will not cause the container to stop. That’s why the docker exec is recommended. 
For more parameter descriptions, please use docker exec –help to view.

Fifth, delete the container

Delete a container that is in a terminated state in the format: 
docker container rm [options] CONTAINER [CONTAINER…]

$ docker container rm awesome_payne 
awesome_payne

If you want to delete a running container, you can add the -f parameter. Docker will send a SIGKILL signal to the container.

Clean up all containers in the terminated state. Use the docker container ls -a command to view all the containers that have been created, including the termination status. If the number is too large, it may be cumbersome to delete them one by one. You can use the following command to clear all the termination status. Container.

Prune Container Docker $ 
! This by Will the Remove All the WARNING stopped Containers. 
Are you the Sure you want to the Continue [the y-/ N] the y-? 
Deleted Containers: 
545f8f6d19286efae28307d06ed1acc034d07f109e907c01892471a6f89e772d 
cb30b87566d0550ec5f1232d148c5ffed6546c347889e58a6405579f2af73f2a 
……

Export and import containers

Exporting a container 
If you want to export a local container, you can use the docker export command.

Container Docker LS -a $ 
CONTAINER ID PORTS the STATUS the IMAGE CREATED the COMMAND NAMES 
16168d4b66b1 Ubuntu “/ bin / the bash” 18 is happy_bardeen minutes minutes ago Member 18 is Up

$ docker export 16168d4b66b1 > ubuntu.tar

This will export the container snapshot to a local file.

Import container snapshots 
can be imported as mirrors from the container snapshot file using docker import, for example

$ cat ubuntu.tar | docker import – test/ubuntu:v1.0 
sha256:91b174fec9ed55d7ebc3d2556499713705f40713458e8594efa114f261d7369a

$ docker image ls 
REPOSITORY TAG IMAGE ID CREATED SIZE 
test/ubuntu v1.0 91b174fec9ed 10 seconds ago 69.8MB 
ubuntu latest 735f80812f90 3 weeks ago 83.5MB

Alternatively, you can import it by specifying a URL or a directory, such as 
$ docker import http://example.com/exampleimage.tgz example/imagerepo

Note: Users can either use the docker load to import the image storage file to the local image library, or use docker import to import a container snapshot to the local image library. The difference between the two is that the container snapshot file will discard all history and metadata information (that is, only the snapshot state of the container at the time), and the image storage file will save the full record and be large. In addition, metadata information such as tags can be reassigned when importing from a container snapshot file.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>