Docker installation and several commands

Install docker

sudo apt-get update
sudo apt-get install docker-ce

Start docker

sudo systemctl enable docker
sudo systemctl start docker

Set up docker user group

By default, the docker command uses Unix socket to communicate with the docker engine. Only root and docker group users can access the Unix socket of docker engine. For security reasons, the root user is not directly used on Linux systems. Therefore, it is better to add users who need to use docker to the docker user group.

To create a docker group:

sudo groupadd docker

Add the current user to the docker group:

sudo usermod -aG docker $USER

Get mirror image

docker pull [options] [Docker Registry address [: port number] /] warehouse name [: label]

List mirroring

docker image ls

REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
redis                latest              5f515359c7f8        5 days ago          183 MB
nginx                latest              05a60462f8ba        5 days ago          181 MB
mongo                3.2                 fe9198c04d62        5 days ago          342 MB
<none>               <none>              00285df0df87        5 days ago          342 MB
ubuntu               18.04               f753707788c5        4 weeks ago         127 MB
ubuntu               latest              f753707788c5        4 weeks ago         127 MB

The list contains the warehouse name, label, image ID, creation time, and occupied space

View the space occupied by image, container and data volume

docker system df

TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              24                  0                   1.992GB             1.992GB (100%)
Containers          1                   0                   62.82MB             62.82MB (100%)
Local Volumes       9                   0                   652.2MB             652.2MB (100%)
Build Cache                                                 0B                  0B

Dangling mirror image

This kind of image originally had the image name and label, which was mongo:3.2. With the official image maintenance, after the release of the new version, when docker pull mongo:3.2 again, the image name mongo:3.2 was transferred to the newly downloaded image, and the name of the old image was cancelled, thus becoming < none >. In addition to docker pull, docker build can also cause this phenomenon. Because of the same name of the new image and the old image, the old image name is cancelled, resulting in the image with warehouse name and label < none >. This kind of unlabeled image is also called dangling image. You can use the following command to display this kind of image specifically:

docker image ls -f dangling=true

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              00285df0df87        5 days ago          342 MB

#Delete suspended image

docker image prune

Delete local image

docker image rm [options] < image 1 > [< image 2 >...]

FROM specifies the base image

The so-called customized image must be customized based on one image. Just like we ran a container of nginx image before and then modified it, the basic image must be specified. FROM is the specified basic image. Therefore, FROM in a Dockerfile must be the first instruction

RUN execute command

The RUN instruction is used to execute command line commands. Because of the power of the command line, the RUN instruction is one of the most commonly used instructions when customizing images. There are two formats:

  • shell format: RUN < command >, just like the command entered directly on the command line. The RUN instruction in the Dockerfile just written is in this format.
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
  • exec format: RUN ["executable", "parameter 1", "parameter 2"], which is more like the format in a function call.

Constructing mirrors

docker build [options] < context path / URL / - >

Read Dockerfile from standard input for construction

docker build - < Dockerfile

or

cat Dockerfile | docker build -

In fact, the file name of the Dockerfile does not need to be a Dockerfile, and it does not need to be in the context directory. For example, you can use the - F.. / Dockerfile.php parameter to specify a file as a Dockerfile

docker build -f xxx -t xxx:version .

docker cp is used for data copy between container and host

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

OPTIONS Description:

  • -L: keep link in source target
Example

Copy the host / www/runoob directory to the / www directory of container 96f7f14e99ab.

docker cp /www/runoob 96f7f14e99ab:/www/

Copy the host / www/runoob directory to container 96f7f14e99ab and rename the directory to www.

docker cp /www/runoob 96f7f14e99ab:/www

Copy the / www directory of container 96f7f14e99ab to the / tmp directory of the host.

docker cp  96f7f14e99ab:/www /tmp/

logs log view

$ docker logs [OPTIONS] CONTAINER
  Options:
        --details        Show more information
    -f, --follow         Track real-time logs
        --since string   Show from timestamp Subsequent logs, or relative time, such as 42 m(42 minutes)
        --tail string    How many lines of logs are displayed from the end of the log? The default is all
    -t, --timestamps     presentation time stamp 
        --until string   Show from timestamp Previous logs, or relative time, such as 42 m(42 minutes)
Example:

To view the log after the specified time, only the last 100 lines are displayed:

$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID

To view the logs for the last 30 minutes:

$ docker logs --since 30m CONTAINER_ID

To view logs after a certain time:

$ docker logs -t --since="2018-02-08T13:23:37" CONTAINER_ID

To view a time period log:

$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID

Tags: Docker sudo Nginx Unix

Posted on Wed, 06 Nov 2019 22:55:20 -0800 by dickey