Docker I use it for business, but I don't think I understand the mechanism so deeply, so Let's start with the basics.
Contrast with virtualization
Operation level | kernel | Remarks | |
---|---|---|---|
Virtualization | OS | Uses a unique kernel for each VM | It emulates HW using a hypervisor and runs multiple OSs, so processing is heavy. |
container | process | Share host OS kernel | The process is light because it is a process rather than running the OS |
Specifically, processes are isolated using kernel functions such as Namespace and cgroup. (Details in the future)
In other words, because it is light, you can easily deploy apps.
The platform that runs the container. You can build and run applications in containers, or distribute containers (Docker images, Dockerfiles).
https://docs.docker.com/engine/install/centos/
--CentOS7 on Vagrant
#Remove if you have an older version of Docker installed
[vagrant@localhost ~]$ sudo yum remove docker*
#Add docker repository
[vagrant@localhost ~]$ sudo yum install -y yum-utils
[vagrant@localhost ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# docker-ce-Make sure the stable repository is enabled
[vagrant@localhost ~]$ cat /etc/yum.repos.d/docker-ce.repo
#Package update
[vagrant@localhost ~]$ sudo yum -y update
#Latest installation of Docker Engine and containerd
[vagrant@localhost ~]$ sudo yum -y install docker-ce docker-ce-cli containerd.io
#Version confirmation
[vagrant@localhost ~]$ docker -v
Docker version 20.10.1, build 831ebea
#Start Docker
[vagrant@localhost ~]$ sudo systemctl start docker
# hello-Download and run world Image
[vagrant@localhost ~]$ sudo docker run hello-world
~ Omitted ~
#Success if this is displayed
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
#Make sure the docker group is created when you install docker
[vagrant@localhost ~]$ cat /etc/group | grep docker
docker:x:993:
#Add current user to docker group
[vagrant@localhost ~]$ echo $USER
vagrant
[vagrant@localhost ~]$ sudo usermod -aG docker $USER
#Reflection of changes
[vagrant@localhost ~]$ newgrp docker
[vagrant@localhost ~]$ id
uid=1000(vagrant) gid=993(docker) groups=993(docker),1000(vagrant) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
#Confirm that the container can be started without sudo
[vagrant@localhost ~]$ docker run hello-world
https://docs.docker.jp/get-started/part2.html Dockerfile A file that predefines what to do inside the container. If you write the definition for connecting to system resources such as network in this file, The same environment can be shared by multiple users.
#Clone sample application from github
[vagrant@localhost ~]$ sudo yum -y install git
[vagrant@localhost ~]$ git clone https://github.com/dockersamples/node-bulletin-board
[vagrant@localhost ~]$ cd node-bulletin-board/bulletin-board-app/
#There is a Dockerfile!
[vagrant@localhost bulletin-board-app]$ ls
app.js backend Dockerfile fonts index.html LICENSE package.json readme.md server.js site.css
[vagrant@localhost bulletin-board-app]$
#Check the contents
[vagrant@localhost bulletin-board-app]$ cat Dockerfile
FROM node:current-slim
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
COPY . .
#Create an image!--tags is an option to name the image
[vagrant@localhost bulletin-board-app]$ docker build --tag bulletinboard:1.0 .
\Sending build context to Docker daemon 45.57kB
Step 1/7 : FROM node:current-slim
current-slim: Pulling from library/node
e50c3c9ef5a2: Pull complete
7d035f3b6068: Pull complete
1758a95126e3: Pull complete
d39676814e5a: Pull complete
e7aa22215d06: Pull complete
Digest: sha256:7bf36131ed121f8113794e83a665bdf5d81cdf77c07672a6c1620fd71675bc8c
Status: Downloaded newer image for node:current-slim
---> 9ac9e9f30b2c
Step 2/7 : WORKDIR /usr/src/app
---> Running in ab0034727068
Removing intermediate container ab0034727068
---> 2c6dc642d528
Step 3/7 : COPY package.json .
---> 8ee0076c51cb
~ Omitted ~
Step 7/7 : COPY . .
---> e614dbf84475
Successfully built e614dbf84475
Successfully tagged bulletinboard:1.0
[vagrant@localhost bulletin-board-app]$
#Run the created image as a container
[vagrant@localhost bulletin-board-app]$ docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0
82832002c212c6a6849eb079ce3bfbfe962c38245ef5407183e7b8eb64ce6a3d
docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0
command | meaning |
---|---|
run | Container execution |
--publish | Specifying port forwarding Unless specified, communication is rejected by default. |
--detach | Run the container in the background and display the container ID If this is not specified, the prompt in the container will be displayed. |
--name | Container name |
Since the container has started, try connecting to http: // localhost: 8000 with a browser
When you are satisfied, delete the container. Since the running container cannot be deleted suddenly, stop it and then delete it.
[vagrant@localhost bulletin-board-app]$ docker stop bb
bb
[vagrant@localhost bulletin-board-app]$ docker rm bb
bb
That's all for now. If you write out the contents of the Dockerfile and so on, one more article is not enough. It seems that the first post will not be possible forever without separating it somewhere, so I will separate it here.
Recommended Posts