docker images and containers

Directory

1. Brief introduction

2. Pull the image

1. Configure the accelerator

Configure on /etc/docker/daemon.json

Whether the configuration takes effect

2. Pull the image — docker pull image name

3. View the image that has been pulled locally – docker image ls / docker images

4. Search for mirror resources in the site – docker search mirror name

3. Modify the image tag – docker tag image ID New repository: new tag

4. Start the container (pull + start) – docker run -itd repository name

1. Start the container and give the container a custom name – docker run –name “custom name” -itd image name

2. After starting the container and running the specified command, it will automatically exit and delete – docker run –rm -it image name bash -c “executed command”

5. View the container startup status – docker ps -a

6. Stop the container – docker stop container id

Seven, delete a container – docker rm container id

Eight, delete a mirror – docker rmi repository:tag

Nine, enter a container – docker exec -it container name bash

1. When bash cannot be started, sh can be used instead

Experiment example:


1. Brief description

The win system often uses Ghost technology to make a mirror image. The system and the applications installed on the system are packaged together for backup. Installing this GHO image on other computers will have the same system and applications as the source system.

The docker function is similar, and it is layered, which is visible when pulling the image.

After the docker image is pulled down, it can be run directly. After running, we call it a container, and one image can start many containers.

The container is the state after the image is started, and it is a process in the Linux system.

Containers can be entered, just like entering a virtual machine.

2. Pull mirror

1, configure accelerator

  • Configure on /etc/docker/daemon.json

The official address is hub.docker.com , but we can configure an Alibaba Cloud mirror station to accelerate by ourselves.

This mirror station can go to the backstage of Alibaba Cloud to apply.

cat > /etc/docker/daemon.json <<EOF
{
   "registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
EOF

View daemon.json

After the configuration takes effect, you need to restart systemctl restart docker

  • Whether to configure effective verification

The registry mirrors item will be displayed in docker info

If an error occurs, you can see the debug information

dockerd –debug This error is normal, because the daemon is not started because it is running.

2. Pull image – docker pull image name

This busybox is a mirror image of a single layer

Redis is a multi-layer mirror

3. Check the locally pulled image – docker image ls / docker images

TAG is usually the version number, the latest version is latest

IMAGE ID The image has its own unique ID

official is official

To search for the specific version of the image, you can go to the official website https://hub.docker.com/ to search, click tag to view

docker pull nginx:1.23.2 Pull the nginx image of the specified version

3. Modify the image tag – docker tag image ID new repository: new tag

It can also be written as: docker tag old repository: old tag new repository: new tag

The IMAGE ID is the only one that determines the identity of the container. Although the name of the new tag is different, the image id will be the same.

Modifying the image tag will generate a new image record, but the image ID used is the same.

4. Start the container (pull + start) – docker run -itd repository name

-i makes the standard input of the container open

-t means to allocate a pseudo terminal

-d means start in the background

The container id printed and displayed at startup is complete, and what is viewed in docker ps is only a part of the id, but it can still be used to perform container operations.

In fact, when starting, it will first search for this image locally, if not, it will automatically pull it first, and then start it after pulling it.

1. Start the container and give the container a custom name — docker run –name “custom name” -itd image name

With the name, you can use either CONTAINER ID or NAMES to operate the container

It seems that you can use REPOSITORY:TAG when operating mirroring

2. Automatically exit and delete after starting the container to run the specified command — docker run –rm -it image name bash -c “executed command”

Function: just to run some instructions in the container, after execution, it will automatically exit and delete the container

5. View container startup status – docker ps -a

Added -a to see unstarted containers

6. Stop the container – docker stop container id

7. Delete a container – docker rm container id

You need to stop the container first.

If it is not stopped, it will be deleted forcibly, add the -f option (force)

8. Delete a mirror – docker rmi repository:tag

A mirror file may have several tags (mirrors with multiple tags with the same image id). At this time, an error will be reported when deleting with the mirror id. You need to use docker rmi repository:tag to delete, but at this time because the mirror has other tag, so only the mirror record of the tag will be deleted, and the mirror file will not be completely deleted.

When the image has only a unique tag, you can use the docker rmi container id to delete it.

When the image is not a tag, you need to add -f to force the image of all tags related to the image ID to be deleted at one time.

9. Enter a container – docker exec -it container name bash

1. When bash cannot start, you can use sh instead

The container is the same as the host kernel, indicating that a kernel is shared

The hostname in the container is the container ID

Experiment example:

ubantu is very pure, download update first to use install

The apt of ubuntu is equivalent to the yum of nginx