Docker tutorial

About Docker

Docker is an open source application container engine. Docker allows developers to package their applications and dependency packages into a lightweight and portable container, and then publish them to any popular Linux machine. It can also realize virtualization.

Containers completely use the sandbox mechanism, and there will be no interfaces between them (similar to iPhone app s). More importantly, the performance overhead of containers is very low.

Docker container usage

  • View all command options of Docker client
  ~# docker
  • Get image
    If we do not have a ubuntu image locally, we can use the docker pull command to load the ubuntu image:
$ docker pull <Mirror name>
  • Start container
$ docker run -it ubuntu /bin/bash

Parameter Description:

-i: Interactive operation.
-t: Terminal.
ubuntu: ubuntu Mirror.
/bin/bash: After the image name is the command. Here we want to have an interactive Shell´╝îSo the /bin/bash. 
To exit the terminal, enter exit:
  • Exit terminal
exit
  • View all container commands as follows:
$ docker ps -a
  • Start a container that has stopped running
$ docker start <Container name/container ID> 
  • Stop a container
$ docker stop <Container name/container ID>
  • Restart stopped container
$ docker restart <Container name/container ID>
  • Enter container
docker attach : Exiting from this container will cause the container to stop.
docker exec: Recommended use docker exec Command, because this exits the container terminal, it will not cause the container to stop.
  • Export container
    If you want to export a local container, you can use the docker export command.
$ docker export <container ID> > ubuntu.tar
  • Import container
    You can use docker import to import from the container snapshot file as an image. The following example imports the snapshot file ubuntu.tar into the image test/ubuntu:v1:
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
 Alternatively, you can specify URL Or import from a directory, for example:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
  • Delete container
$ docker rm -f <Container name/container ID> 
  • Network port
    You can view the port mapping to the container through the docker ps command. Docker also provides another shortcut docker port. Using docker port, you can view the port number of a specified (ID or name) container mapped to the host host.
$ docker port <Container name/container ID> 

Docker image usage

When running the container, if the image used does not exist locally, docker will automatically download it from the docker image warehouse. By default, it is downloaded from the Docker Hub public image source.

1. Manage and use local Docker host images

  • List mirrors
~$ docker images 

Description of each option:
REPOSITORY: Represents the warehouse source of the mirror
TAG: Mirrored label
IMAGE ID: image ID
CREATED: Image creation time
SIZE: Mirror size

When using the container to run the container, the command is as follows:

$ docker run -t -i <image REPOSITORY> /bin/bash
  • Get a new image
docker pull <Mirror name>
  • delete mirror
$ docker rmi <image REPOSITORY>

2. Create mirror

When the image we downloaded from the docker image warehouse cannot meet our needs, we can change the image in the following two ways.

1,Update the image from the container that has been created and commit the image
2,use Dockerfile Directive to create a new image
  • Update mirror
    Before updating the image, we need to use the image to create a container.
$ docker run -t -i <Mirror name>  /bin/bash

Use in a running container apt-get update Command to update
 After completing the operation, enter exit Command to exit the container

By command docker commit To submit a copy of the container
runoob@runoob:~$ docker commit -m="Description information submitted" -a="Specify mirror author" <container ID> <Specifies the name of the target image to create>

Use our new image runoob/ubuntu To start a container
$ docker run -t -i <Specifies the name of the target image to create> /bin/bash
  • Build mirror
    We use the command docker build to create a new image from scratch. To do this, we need to create a Dockerfile file that contains a set of instructions to tell Docker how to build our image.
$ docker build -t <Specifies the name of the target image to create>

Docker command

Docker command

Tags: Linux Docker Ubuntu

Posted on Tue, 07 Sep 2021 22:19:00 -0400 by oeb