After understanding Docker, I actually introduced it.

1 About this article

It seems that I will use Docker in my new workplace from tomorrow, but I don't know (or almost anything) well, so I looked it up.

2 What is Docker?

I don't know what Docker is in the first place, so I looked it up.

Docker is, in a nutshell, a "simple, easy-to-use lightweight container environment that runs on Linux." You can prepare a box called a container on the OS called Linux, install the necessary software and libraries (tools) in the container, and package it as a single unit.

Source: Story of introducing Docker into the development environment to streamline development-Development Headquarters / TechLunch-


Docker is one of the technologies that are attracting attention in the infrastructure and DevOps area, and is a platform developed by Docker for creating, distributing, and executing container-type virtual environments. Docker uses Linux container technology and is often compared to virtual machines. In a virtual machine such as VirtualBox, the guest OS is run on the host machine using the hypervisor, and middleware etc. is run on it. On the other hand, a container uses the kernel of the host machine and isolates processes, users, etc., so that it can be operated as if another machine is running. Therefore, it is lightweight and can be started and stopped at high speed.

Source: Introduction to Docker (1st) -What is Docker and what is good-

** It seems that if you develop in a container environment called Docker, you can package that environment and it will be convenient **.

3 Install Docker

Follow the Official Documents to install Docker on ubuntu.

3.1 Uninstall the old version of Docker just in case

There is a possibility that you have an older version of Docker installed at the fine particle level, so run the remove command. It seems that you don't have to do it.

$ sudo apt-get remove docker docker-engine docker.io containerd runc

It's OK if you see something like Package'XXXX' is not installed, so not removed.

3.2 Update package index-Install packages so that apt can use the repository

Install the packages needed to install Docker. It seems that the main purpose is to make up for the fact that apt does not support HTTPS.

$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

3.3 Added official GPG key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

I'm not sure what I'm doing, so I'll take a closer look.

3.3.1 What is curl?

Since I installed curl in 3.2, I can use the curl command. What is the curl command?

If you specify a URL as an argument, the file with that URL will be downloaded. The default output destination is "standard output", so save it with a redirect or specify the "-O" or "-o" option.

Source: [curl] command-download (transfer) files using various protocols

The curl command is a ** command to download **. (-fsSL is that option. I'll omit it this time.)

3.3.2 What is GPG?

The URL of the argument of the curl command is the URL of Docker's GPG. Since the curl command is a command to download, you will download Docker's GPG. GPG is an abbreviation for ** GnuPG **, and what is this?

A tool that verifies the signature of a file with a public key, encrypts / decrypts an email with a public / private key pair, and attaches a signature.

Quote source:GnuPG |IT Glossary|Otsuka Shokai

In other words, you should think of it as ** the key required for authentication **.

3.3.3 What is apt-key?

I'm passing Docker's GPG to apt-key. What is apt-key?

"Apt-key" is a command that manages the list of keys that "apt" uses to authenticate packages. In order to get the package from other than Ubuntu official, it is necessary to capture the key information to authenticate with this command.

Source: How to use apt / apt-key | npaka | note

In other words, it seems that you need to import Docker's GPG into your local app-key in order to install Docker.

3.4 Key confirmation

Make sure the GPG key has been added. The GPG key fingerprint of Docker is "9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88", so search for the last 8 digits as follows, and if the result is displayed, it is OK.

$ sudo apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]

3.5 Set up stable repository

$sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

[Ubuntu] How to add / remove PPA | Hbk project

If you look at this article, it's easy to understand what the work is for. By default, the only packages that can be installed using apt are those in /etc/apt/source.list. In other words, in order to install Docker, you need to add a PPA (Personal Package Archive).

3.6 Performing the installation

Update the package index of Apt and perform the installation.

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

This completes the Docker installation.

4 Try using Docker

From here is the production. Let's actually use Docker according to Official Document.

4.1 Orientation

First, check if Docker works.

4.1.1 Checking the version

Make sure you really have Docker installed.

$ docker --version
Docker version 19.03.12, build 48a66213fe

Was being done.

4.1.2 Run hello-world image

Get the hello-world image.

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

It is easy to understand what you are doing by referring to the following site.

Introduction to Docker (2nd) -Docker setup, container startup- | Sakura Knowledge

** The official image "hello-world" in the container sharing service called Docker Hub is acquired, and the container is started based on it **.

Reference: hello-world image page

4.1.3 Confirm that the image has been downloaded

$ docker image ls

You can see that there is an image of hello-world.

4.1.4 Confirm that the container is started

Since docker run is not a command to get an image but a command to start a container based on an image, the container should be started.

$ docker ps --all

CONTAINER ID     IMAGE           COMMAND      CREATED            STATUS
54f4984ed6a8     hello-world     "/hello"     20 seconds ago     Exited (0) 19 seconds ago

Certainly the container was confirmed.

4.2 Build Image & Run Container

Now that we have Docker running, it's time to build and run the image.

4.2.1 Create a git clone for the bulletin board app

Clone the bulletin board app for testing.

$ git clone https://github.com/dockersamples/node-bulletin-board

4.2.2 Image build

Build the image.

$ cd node-bulletin-board/bulletin-board-app
$ docker build --tag bulletinboard:1.0 .

As mentioned in 4.1.2, you need an image to start the container. I brought the official image in 4.1.2, but since there are as many container environments as there are development environments, you need your own image (if you look for it, you may be able to use the one published on Docker Hub. But). Building is ** creating a new image based on an existing image (base image) **. The build is executed in the place where ** Dockerfile ** is placed, and it is executed based on the Dockerfile.

Let's take a look at the Dockerfile.

Dockerfile


FROM node:current-slim

WORKDIR /usr/src/app
COPY package.json .
RUN npm install

EXPOSE 8080
CMD [ "npm", "start" ]

COPY . .

Notice the "FROM node: current-slim" on the first line. It is a description to build a new image based on the base image "node: current-slim". An image named "bulletinboard: 1.0.", Which is the argument of the command executed earlier, has been created. Let's check if the image is actually created.

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
bulletinboard       1.0                 789f62d53520        13 minutes ago      177MB
node                current-slim        3aaf4acbaad7        2 days ago          159MB
hello-world         latest              bf756fb1ae65        10 months ago       13.3kB

You can see the base image and the built image.

4.2.3 Start the container

Start the container based on the created image.

$ docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0

You can check that the bulletin board application is running by accessing localhost: 8000 with a browser. Let's take a closer look at this.

4.2.3.1 --publish 8000:8080

8000 is the port number on the host side and 8080 is the port number on the container side.

4.2.3.2 --detach

This option is used when starting the container in the background.

4.2.3.3 --name bb bulletinboard:1.0 Start a container named "bb" based on the image "bullet inboard: 1.0".

4.2.4 Stop container

Stop / delete the container.

docker rm --force bb

Now you have a good understanding of image building and containerization.

5 Finally

I'm glad that I got a rough idea of how to build and distribute a container environment using Docker.

Please also follow Twitter!

Recommended Posts

After understanding Docker, I actually introduced it.
I introduced Docker to Rails 6, so I summarized it (beginner)
Let's use it after understanding sudo!
I installed Docker on EC2 and started it
I can't find the docker image after updating to docker desktop 2.4.0.0