Docker learning process

A few days ago, I saw that pacman on Arch is very easy to use, but I encountered many problems installing Manjaro. So try Arch on the server. Docker allows you to run other Linux distributions on any Linux kernel. Using docker, the deployment, testing and distribution of applications can become more efficient and easy than ever before!

Preparation before start

My server is Ubuntu, so first install Docker.

bash

1apt install docker

COPY

Basic command

  • Pull the Arch image.

bash

1docker pull archlinux/base

COPY

In this way, the of Arch is stored locally. You can view all local images through docker image list.

bash

1root@lnmp:~/docker# docker image list
2REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
3arch                v1                  9a4034dda227        3 hours ago         594MB
4<none>              <none>              20b81ab423c2        3 hours ago         458MB
5nginx               latest              f68d6e55e065        4 days ago          109MB
6archlinux/base      latest              98e93880f9b1        4 days ago          458MB
7bash                latest              417531d957ab        8 weeks ago         15.2MB

COPY

  • Start a Docker container

Start a Docker container with docker run.

Before that, understand the relationship between mirrors and containers.

The relationship between image and container is like a class and instance in object-oriented programming. Image is a static definition, and container is an entity when the image runs. Container can be created, started, stopped, deleted, suspended, etc.

bash

1docker run -it --name arch archlinux/base

COPY

-i is the interactive mode and - t is the starting tty-- Name names the container. Followed by the image name.

Then it will enter the tty character terminal of Arch.

In addition, the commonly used parameters are -d background operation.

The tty terminal entering the container from the background can use docker exec -it arch bash

arch is the previous name, and bash is the command in the container. Open a shell here.

  • View container running status

docker ps -a means to view the status of all containers.

bash

1root@lnmp:~/hugo/quickstart# docker ps -a
2CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
372e8ede0f2c8        archlinux/base      "/usr/bin/bash"          13 seconds ago      Up 12 seconds                              arch
402243150a4a8        archlinux/base      "--name arch"            7 minutes ago       Created                                    reverent_wing
5d44d03201e87        nginx               "nginx -g 'daemon of..."   15 minutes ago      Up 15 minutes       0.0.0.0:8080->80/tcp   web

COPY

  • Delete a container

docker rm 3dd93022c1b2, followed by the ID or name of the container.

  • Stop a container

docker stop arch is also the ID or name of the. Start naturally, needless to say.

  • Save a container to the mirror

Why save as a mirror?

Now that we have customized the changes, we hope to save them to form an image. You know, When we run a container (if the volume is not used), any file changes we make will be recorded in the container storage layer. Docker provides a docker commit command to save the container storage layer as an image. In other words, on the basis of the original image, the container storage layer will be superimposed to form a new image. We will run this new image later When, you will have the last file change of the original container.

Use docker commit to join the version library like git commit.

bash

1root@lnmp:~# docker commit --author "Innei" --message "init" a2 arch:v1
2sha256:9a4034dda227606da0e621fd448d741d1e4b28137a65069575bccf823581bd65

COPY

We can see this newly customized image in docker image ls:

bash

1root@lnmp:~/docker# docker image list
2REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
3arch                v1                  9a4034dda227        3 hours ago         594MB
4<none>              <none>              20b81ab423c2        4 hours ago         458MB
5nginx               latest              f68d6e55e065        4 days ago          109MB
6archlinux/base      latest              98e93880f9b1        4 days ago          458MB
7bash                latest              417531d957ab        8 weeks ago         15.2MB

COPY

  • In container port mapping

The internal container is an isolated environment. If you need to open the port for external access, you need to start port mapping.

Take Nginx as an example.

bash

1# Pull nginx
2docker pull nginx
3docker run --name webserver -d -p 8080:80 nginx

COPY

-p open port mapping, external port: internal port. 8080:80 indicates internal 80 - > external 8080.

Now access port 8080 through external IP and you will see:

Added by nay4 on Tue, 28 Dec 2021 23:56:34 +0200