Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Start the Docker container


May 22, 2021 Docker From entry to practice



There are two ways to start a container, one is to create a new container based on the mirror and start it, and the other is to restart the container in the stop state.

Because Docker's containers are so lightweight, many times users delete and create containers at any time.

New and started

The command required is docker run

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

$ sudo docker run ubuntu:14.04 /bin/echo 'Hello world'
Hello world

This makes almost no difference to /bin/echo 'hello world' directly locally.

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

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

Where the -t allows Docker to assign a pseudo-tty and bind to the container's standard input, -i the container's standard input open.

In interactive mode, users can enter commands through the terminals they create, for example

root@af8bae53bdd3:/# pwd
/
root@af8bae53bdd3:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

When docker run the standard operations that Docker runs in the background include:

  • Check the local presence of the specified mirror, which is downloaded from the public repository if it does not exist
  • Create and start a container with a mirror
  • Assign a file system and mount a readable and write layer outside the read-only mirror layer
  • Bridge a virtual interface from the bridge interface configured by the host host to the container
  • Configure an ip address to the container from the address pool
  • Perform user-specified applications
  • The container is terminated after execution has been completed

Start the terminated container

You can docker start command to run a container that has been terminated directly.

The core of the container is the application that is executed, and the resources required are all necessary for the application to run. B eyond that, there are no other resources. You can view process information in ps top or top.

root@ba267838cc1b:/# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
   11 ?        00:00:00 ps

As you can see, only the specified bash app is running in the container. This feature makes Docker extremely efficient with resources and is a true and lightweight virtualization.