【Docker入門】初心者向け!Dockerの基本を学んでコンテナ型の仮想環境を作ろう!

Pythonプログラミング VTuber サプー
18 Jul 202237:03

Summary

TLDRこのビデオは、Dockerの基本的な操作方法と利点について説明しています。Dockerの概念から始まり、コンテナ、イメージの作成方法、Docker Hubからイメージのダウンロード、Dockerfileによるカスタムイメージの構築、実例を用いてコンテナの実行方法などを解説しています。要約すると、Dockerを使うことで開発環境の再現性が高まるなどの利点があり、コンテナの作成から実行、停止、削除といった基本的な操作コマンドをマスターすることで、様々な開発環境を手軽に利用できるようになる、という内容です。

Takeaways

  • 😀 Dockerとは、コンテナ型の仮想環境を作成・配布・実行するもの
  • 📦 Dockerの利点は、同じ環境を構築できること、バージョン管理・配布がしやすいこと
  • ☁️ Dockerでクラウド上のサーバーも自動構築できる
  • 🗄 Docker Hubには必要なコンテナイメージがたくさん公開されている
  • 🐳 Dockerコマンドでコンテナを操作できる(起動、中止、削除など)
  • 🔌 ポートフォワーディングを使ってコンテナと通信できる
  • 📄 Dockerfileを使えばカスタムイメージを構築可能
  • 🗃 copyやaddコマンドでファイルをコンテナにコピーできる
  • 📝 docker buildでDockerfileからイメージ作成
  • ✅ ENTRYPOINTでコンテナ実行時にスクリプト実行可能

Q & A

  • Dockerとはどのようなものですか?

    -Dockerはコンテナ型の仮想環境を作成し、配布・実装するためのものです。ホストOSのカーネルを利用することで、高速に仮想環境を起動できるのが特徴です。

  • コンテナ型と仮想マシン型の違いは何ですか?

    -コンテナ型はホストOSのカーネルを利用していますが、仮想マシン型はゲストOS上に仮想ソフトを載せることで仮想環境を作成します。コンテナ型のほうが軽量で高速に起動できます。

  • Dockerの利点は何ですか?

    -Dockerの利点は、1完全同一の環境を構築できること、2環境設定をコード化しているためバージョン管理や配布がしやすいこと、3クラウド上でのサーバ構築を自動化できること、4公開されたコンテナイメージを利用できることです。

  • Dockerイメージとは何ですか?

    -Dockerイメージはコンテナを構築するのに必要なコマンドやメタデータが含まれたパッケージです。Dockerイメージからコンテナを作成して利用します。

  • Dockerコンテナの利点は何ですか?

    -Dockerコンテナを利用することで、簡単に様々な仮想環境をPC上に構築できます。データベースサーバやWebサーバなど、目的に合わせたコンテナを活用できます。

  • Dockerfileとは何ですか?

    -DockerfileはカスタマイズしたDockerイメージを生成するためのテキストファイルです。fromやrun、copyなどのコマンドを記述して独自のDockerイメージをビルドできます。

  • なぜDockerが便利なのですか?

    -Dockerを使うことで、開発環境の再現性が高まります。コンテナイメージを共有することで、全く同じ環境を他の人と構築できるからです。

  • Dockerの基本的な操作コマンドを教えてください。

    -代表的なコマンドとしては、docker pull、docker run、docker ps、docker exec、docker stopなどがあります。イメージの取得からコンテナの操作、停止までを行えます。

  • Docker Composeとは何ですか?

    -Docker Composeは、複数のコンテナを定義してアプリケーションを管理できるDockerの機能です。yamlファイルでコンテナ構成を記述し、一括してコンテナを起動できます。

  • Dockerを使うメリットは何ですか?

    -開発環境の共有と再現性の向上、デプロイとスケーリングの容易化、リソース利用効率の最適化など、様々なメリットがあります。コンテナ型仮想化を活用することで、開発から運用まで効率的にできます。

Outlines

00:00

😀Dockerの概要とメリットを解説

Dockerはコンテナ型の仮想環境を作成して配布・実行するもの。コンテナ型はホストOSのカーネルを流用して軽量な仮想環境を実現。メリットは、環境の完全な再現性、バージョン管理・配布の容易さ、クラウド上でのサーバ構築の自動化など。

05:03

😊Dockerの基本操作を実践

Docker Desktopをインストールし、Docker HubからMySQLのコンテナイメージをPullしてコンテナを作成。コンテナに入りMySQLに接続、テーブル作成とデータ入力を行う。外部からの接続もしてみる。これらの一連の操作を通じて、コンテナの基本的な取り扱い方を理解。

10:09

🤔Docker Hubでコンテナイメージを検索

Docker Hubにアクセスし、利用目的に合ったコンテナイメージを検索。今回はMySQLコンテナを作成したいので、MySQLのイメージをPullする。バージョンも指定できる。ほぼ必要なものは揃っている。

15:09

🚀コンテナの開始・停止・削除

`docker run`でコンテナを作成して起動。`docker ps`で実行中のコンテナを確認。`docker stop`でコンテナを停止。`docker rm`でコンテナ自体を削除。`docker rmi`でPullしたイメージを削除。操作に習熟すれば、手軽に必要なコンテナを準備できる。

20:10

🐍PythonコンテナからDBデータを取得

コンテナとホストPC間のポートフォワーディングを設定し、PythonからローカルPCの指定ポート経由でコンテナ内MySQLに接続。コンテナ側のDBデータを正しく取得できたことを確認。入出力を容易に分離。

25:14

👩‍💻Dockerfileでカスタムイメージ作成

Dockerfileに基となるイメージの指定、パッケージのインストール、ファイルのコピーなどの命令を記述。これによりカスタマイズされた新しいコンテナイメージをビルド可能。`docker build`コマンドでイメージのビルドを実行。

30:15

🎉カスタムイメージからコンテナを作成

自分でビルドしたイメージからコンテナを生成。Dockerfileの通り環境が構築されていることを確認。ENTRYPOINTを使用して、コンテナ起動時にPythonスクリプトを実行する例も示す。様々な用途に応じたコンテナが作れる。

35:18

🙇‍♀️Dockerの基本操作をまとめ

Docker DesktopによりローカルPC上にコンテナを作成可能。コンテナイメージを基にしてコンテナを生成し、実行・操作。使い終わったコンテナは削除。これを理解すれば、開発時の様々な用途にDockerを活用できる。

Mindmap

Keywords

💡Docker

Dockerは、コンテナ型の仮想環境を作成および配布・実行するためのものです。ホストOSのカーネルを利用することで、軽量で高速に仮想環境を起動できます。ビデオでは、Dockerの概要と基本操作について説明しています。

💡コンテナ

コンテナとは、Dockerなどを利用して作成される軽量な仮想環境のことです。ビデオでは、コンテナのイメージをpullしてコンテナを作成し、中でアプリケーションを実行する方法を説明しています。

💡イメージ

コンテナを作成するためのもとになるパッケージです。Docker Hubなどから必要なイメージをpullしてきて、Docker runコマンドでコンテナを作成します。

💡Dockerファイル

カスタマイズしたコンテナイメージを作成するための設定ファイルです。OSのインストールやミドルウェアの設定などを記述して、Docker buildコマンドでイメージをビルドします。

💡Docker Hub

さまざまなコンテナイメージが公開されているレジストリプラットフォームです。ビデオではMySQLやUbuntuのイメージをpullする例を紹介しています。

💡pull

Docker Hubなどのレジストリからコンテナイメージをダウンロードすることです。ローカルに同じイメージがなければ、Docker run時に自動的にpullされます。

💡ポートフォワーディング

コンテナとホストOS間で特定のポートを紐づける機能です。コンテナ側のアプリケーションにホストOSから接続できるようになります。

💡コンテナ管理

Dockerコマンドを使ってコンテナの起動、停止、削除などライフサイクルを管理することです。psやexecコマンドを使ってコンテナの状態確認や内部での操作ができます。

💡インフラストラクチャ・アズ・コード

インフラの設定やプロビジョニングをコードで実現し、バージョン管理などを容易にするDevOpsの考え方です。Dockerfileがその理念を体現していると言えます。

💡Kubernetes

コンテナをクラスタリングして管理するためのオーケストレーションプラットフォームです。Dockerと連携することで、大規模なコンテナインフラを効率的に運用できます。

Highlights

Dockerはコンテナ型の仮想環境を作成および配布・実行するものです。

コンテナ型はホストOSのカーネルを流用して仮想環境を作成するタイプです。

Dockerの第一のメリットは、どのコンピュータでも完全に同じ環境を作成できることです。

Dockerの第二のメリットは、OSの設定やミドルウェアのインストール、環境設定がコード化されているのでバージョン管理や配布がしやすいことです。

Dockerの第三のメリットは、クラウド上でもサーバーを自動構築できることです。

Docker Hubには必要なものがほぼ揃っているので、公開されたコンテナイメージを用いることができます。

ソフトウェアエンジニアはコンテナイメージを利用することが多いでしょう。

インフラエンジニアはDockerファイルを自分で記述して環境構築をすることが多いでしょう。

コンテナイメージからコンテナを作成し、起動・操作・停止・削除する一連の流れが大切です。

Dockerファイルを使うとカスタマイズしたコンテナイメージを作成できます。

Dockerファイルにはコンテナイメージ作成時に実行するコマンドが含まれます。

DockerビルドコマンドでDockerファイルからイメージを作成します。

コンテナイメージからDocker実行時にプロセスを実行するにはENTRYPOINTを使います。

Dockerにはまだまだできることがたくさんありますが、基本的な操作が分かったと思います。

次回の動画でまたお会いしましょう。

Transcripts

play00:00

Hello, I'm Sabu from Python VTuber.

play00:02

This time, I would like to explain the basics of Docker that can make a fast-paced environment.

play00:08

I've heard of Docker, but I've never used it myself.

play00:12

I'm explaining it to the software engineers who are wondering when to use it, so I'd be happy if you could watch it until the end.

play00:20

This channel is sending information about Python and software development, so please subscribe to the channel if you like.

play00:27

There are also videos and live videos that can only be seen for membership, so please consider becoming a member of the membership if you like.

play00:35

First of all, what is Docker?

play00:38

Docker is a thing to create and distribute and implement a container-type virtual environment.

play00:45

Just listening to this makes you think,

play00:46

Hmm? What is a container-type virtual environment?

play00:50

The virtual environment here is a thing that reproduces a virtual computer so that there are other computers in the computer.

play01:00

For example, you can create a ubuntu server in a Windows PC or create a cent OS server in a Mac.

play01:09

At this time, we will use the OS that is originally used for a physical computer as a guest OS, and the OS that is used in the virtual environment as a host OS.

play01:21

So what is a container type?

play01:24

A container type is a container type that uses a kernel that moves the host OS to create a virtual environment so that there is a guest OS.

play01:35

The image is like this.

play01:37

There will be a lot of words like container in the future, but if you say container, please think of the virtual environment that is on top of this Docker.

play01:48

There are also virtual environments that are not container-type, such as virtual machines that create virtual environments by putting a guest OS on top of a virtual software.

play01:59

The Docker I'm going to introduce this time is a container type,

play02:02

but the good thing about this container type is that it uses a host OS kernel, so it has the advantage of being able to stop the virtual environment at high speed with a light weight.

play02:13

It may be a little confusing, but the detailed understanding of this area does not have any particular problems even if you do not know it on the operation of Docker, so let's proceed without knowing it well.

play02:23

So what is good about Docker?

play02:27

First of all, you can create a completely same environment on any computer.

play02:33

As I said earlier, the virtual environment can create a virtual environment of your favorite OS, whether it is a host OS or whatever, so for example, even if the members of the same development team are

play02:44

developing on Windows and Mac, even if they are developing on Mac, they can use Docker to prepare a completely same environment for both.

play02:55

There are problems with the environment that makes it difficult for Mac to work with this code, but Windows to work with this code.

play03:04

In such a case, if you create a Docker virtual environment that is exactly the same as OS, middleware, and environment settings, and check the code operation in them,

play03:15

A3 will work, but B3 will not work.

play03:19

This is the first benefit.

play03:21

The second advantage of Docker is that it is easy to use and manage versions and distribute because the OS settings, middleware installs, and environment settings are coded in Docker.

play03:34

To build a virtual environment with Docker, you need a Docker file, but this Docker file is just a text file, and the contents are all recorded as code such as the installation of OS, middleware, and environment settings.

play03:50

Therefore, if you look at the file, you can see what kind of environment you can build, and if you want to change some of the environment you are creating, you can edit only a part of the file.

play04:02

It's just a text file, so you can manage the version with Git.

play04:06

In addition, it is a lightweight text file, so it is easy to distribute to other people.

play04:12

For example, when a new member enters the development team and builds the development environment, you can install Python with your own hands one by one while reading the procedure written in Japanese, and even if you don't set this to the environment variable by inserting the database, you can just enter the Docker file and execute the command to reproduce the same environment as everyone.

play04:35

By the way, code-coding infrastructure management operations is called infrastructure as code, and it has become a very important idea recently.

play04:46

The third advantage of Docker is that you can automatically build servers in the cloud.

play04:51

So far, I've been talking about using Docker on my computer, but you can also use Docker to build servers in the cloud such as AWS and GCP.

play05:03

In particular, it is even more convenient to use a container management system called Kubernetes, not just Docker.

play05:09

When building servers in the cloud, you can press and hold the button from the web console, select OS, and decide the disk capacity and make it manually, but by using the container image created by Docker file, you can automatically build servers, so you can create clusters by building multiple instances of the same container image.

play05:31

In this case, you can automatically and efficiently work.

play05:35

This container image is a package that can be implemented in a Docker environment that includes commands and metadata that are necessary to build containers that can be made based on Docker files.

play05:48

I think it's a little hard to understand, but I think I can understand it somehow because it will be implemented in various ways later.

play05:55

And the fourth advantage is that anyone can use the container image that is publicly released on the Docker Hub platform.

play06:03

I've been talking about the need for Docker files to create a virtual environment with so far.

play06:09

But even if you don't write that file yourself from scratch, you can easily use the environment and tools you want to use by using the image that is publicly released on the Docker Hub platform.

play06:22

For example, if you want to use an environment that contains MySQL, you can use it by looking for that kind of image.

play06:30

I think that Docker Hub has almost everything you need, such as NGINX, Node.js, and Redis.

play06:37

How about it? Did you somehow understand the benefits of using Docker?

play06:42

Next, I would like to talk about the difference between a software engineer and an infrastructure engineer in handling Docker to make the distribution of this video and the range clear.

play06:52

That's because Docker is not a software engineer who writes Docker files.

play06:58

I think there are more cases where you use the image that is publicly released on the Docker Hub that I explained earlier, or use the Docker file that the infrastructure engineer prepared.

play07:09

For example, it is an image that you can use to develop an app by starting a container for your database on your computer.

play07:18

So, I think it would be enough if you knew the basic operation of starting a container, getting the image of the container that I will explain in the video after this, and connecting and stopping.

play07:31

On the other hand, for infrastructure engineers, they often write Docker files to build an environment that is designed to be built, and build servers on the cloud.

play07:42

So, I often write the Docker file myself.

play07:45

This video is a Docker entry video for software engineers, so I will not touch on how to make Docker files well like infrastructure engineers.

play07:56

I will explain how to make a basic writing method of Docker files and how to create a container image from Docker files, but I will not touch on detailed knowledge or techniques that infrastructure engineers need, so please note that.

play08:09

So, I would like to actually use Docker to create an environment.

play08:15

First, you need to install Docker Desktop.

play08:19

There is an installer on the official site of Docker, so download the installer that fits each environment.

play08:27

I will post the URL of this page in the summary column.

play08:30

Sapoo is now using a Windows PC, so select Windows on this site, download the installer, and when the installation is complete, double-click the installer.

play08:44

This installer will start, so proceed with the installation.

play08:48

Click OK.

play08:52

When the installation is successful, this screen will appear.

play08:56

Here you need to restart the computer, so press this button to restart after completing the installer.

play09:03

When the restart is complete, a screen with a contract change will appear like this, so read the content, check it, and press the accept button.

play09:14

Then, I think this screen will be displayed, but the Docker desktop itself will be closed and it will be fine.

play09:21

Basically, the operation of Docker is implemented from the command, so if it is Windows, it is PowerShell, and if it is Mac, it is Terminal.

play09:29

Also, if the user account is different from the manager account in the case of Windows, you need to add the user to the Docker Users group.

play09:37

The Mac installation is basically the same, and when the installer is downloaded, double-click Docker.dmd to open the installer, and drag the Docker icon to the application folder.

play09:49

Then, I think that a contract will be displayed, so check it, and when you select agree, the Docker desktop will start.

play09:56

First, check if the Docker command can be used properly.

play10:00

If the Docker version is released, the Docker will be installed properly.

play10:08

Next, I will search for the container image that is generally released from the Docker Hub.

play10:13

When you open the official site of Docker Hub, you will see this page.

play10:18

I will put the URL of this page in the summary column.

play10:21

As I explained earlier, the container image is a package that contains commands and metadata necessary to build a container.

play10:30

Therefore, you need to use the container image that meets the purpose, but this time I would like to implement a case where the database is stored as a Docker container.

play10:41

So, search for the container image with MySQL.

play10:45

If you enter MySQL in the search var on the top left, you will see some images that are caught in the search like this.

play10:57

It says Docker official image at the top, so it looks like the image that MySQL is officially releasing.

play11:04

Open this.

play11:06

Then, it contains various descriptions, including the use of this image and the sample command.

play11:13

First of all, I would like to pull this image.

play11:17

There is a pull command on the upper right, so copy this and run this command on the command line tool.

play11:26

When you run this, it will take a little time, but when the pull is complete, you can download the Docker container image on the Docker hub to your local PC.

play11:42

By the way, the Docker pull command can be run in any directory.

play11:47

There is no such thing as folders or files being created under the current directory like the git command.

play11:53

Also, in fact, in the Docker run command that will be implemented after this, if there is no container image locally, it will search for images from the Docker hub.

play12:03

However, this time I pulled it so that it is easy to imagine the container image.

play12:09

When the pull is complete, run the Docker images command.

play12:14

Now, the information of the container image in your PC will be displayed.

play12:20

Here you can see that there is an image of MySQL that you have just pulled.

play12:25

Then, start this MySQL container immediately.

play12:29

In many cases, the container's starting method is written on the Docker hub page.

play12:35

To create and start a container, use the command Docker run, but here the Docker run command is written.

play12:45

Copy this once and paste it on the command line tool and change it a little.

play12:52

First of all, this is the option of hyphen hyphen name, but this is a specification of the container's color.

play12:59

In the sample, it says some-mysql, but I will give you a name that is easy to understand.

play13:06

This time, I will name it supu-mysql.

play13:11

The hyphen e option is an environmental variable setting.

play13:15

So in this case, set a password for the MySQL root user of MySQL, which is called MySQL root password, to an environmental variable.

play13:26

For example, set a password like K12IJ75H.

play13:32

By the way, this is not an environmental variable of a local PC, but an environmental variable of a container.

play13:40

The next hyphen d option is an option to run in detached mode, and if you attach this, the container will be executed in the background.

play13:48

And the last part that is written as MySQL colon tag is the setting of the image to be used, but the tag after the colon is written.

play13:58

Set the version of the necessary MySQL.

play14:01

For the version, open the tab called Tags at the Docker Hub, and you can search for the version.

play14:10

If you don't do anything, it will be the latest, so I'm going to make it the latest this time, so I'll omit the tag part.

play14:20

And here I'm going to add a little more options.

play14:24

If you use the hyphen p option, you can transfer the communication to a specific port with a local PC to a specific port on the container side.

play14:35

This is called port forwarding.

play14:38

If you type the port number of the local PC behind the hyphen p, the port forwarding will be possible if you type the port number of the colon container side.

play14:47

The port of MySQL is usually 3306, so the right side is 3306, and the local side is 13306.

play15:00

When this command is executed, the container ID is displayed and the container is created.

play15:08

To actually check what kind of container is running now, execute the command Docker ps.

play15:16

When this is executed, the container information list that is running at this moment will be displayed.

play15:24

It's a little hard to see because line breaks, but here you can see that the supu-mysql container has been activated.

play15:33

By the way, the container image is only to create containers, so you can create several containers from the same image.

play15:44

This time, I won't handle multiple cases, so I'd like to proceed with one container I made now.

play15:50

Then I would like to enter the container and check if MySQL can be operated.

play15:57

To enter the container, you can use the command Docker exec.

play16:03

If you type the hyphen id option, you can operate the command operation on the container side from this console that you are using now.

play16:13

And if you specify the name of the container, supu-mysql, and write bash, you will use bash inside the container.

play16:21

If you execute this, you can enter the container like this.

play16:28

Then I will try to see if MySQL can be operated.

play16:32

Login to MySQL with mysql -u root -p root user.

play16:39

Since you can ask for a password, enter the root user password set as an environmental variable in Docker Run and press enter.

play16:49

You can log in to MySQL like this.

play16:52

Now you can see that there is a MySQL properly in the container.

play16:56

I'd like to make a table and put in one record data.

play17:01

This video is not a MySQL video, so I will omit the detailed explanation of the command to be implemented from now on.

play17:08

If you don't know the operation of MySQL, I would like you to see it as if you are adding a record to the database.

play17:15

First, create a database with create database supu_db.

play17:23

Set the database to use with use supu_db.

play17:31

Create a user table with create table users.

play17:38

Insert into and enter only one record.

play17:43

Exit to exit MySQL.

play17:47

Now I have entered only one record data into the MySQL table in the container.

play17:52

I want to exit from the container in this state, so I can exit from the container by pressing the control button and pressing Q.

play18:01

Now I'm back on the local PC side, but if you execute the command docker ps, you can see that the supu-mysql container is still running.

play18:13

In this state, I would like to stop this container from the outside.

play18:18

Stop the container with docker stop supu-mysql.

play18:23

If you execute this and execute docker ps again, the container is no longer displayed.

play18:32

But in this state, if you execute this by setting the option docker ps -a, the stopped container will also be displayed.

play18:43

Here is the supu-mysql.

play18:46

And to start the container that was stopped once, use the docker start command instead of the docker run command.

play18:55

It's a little confusing, but the docker run command is a command that creates and starts the container from the image of the container,

play19:03

but it will be the docker start command to start the container that has been built and stopped once.

play19:09

So in the case of docker start, you do not need to specify the number of environments or versions.

play19:15

Simply type the name of the docker start container and type the command supu-mysql, and you will be able to start the same container as before.

play19:26

If you execute this, the name will be output and if you execute docker ps again, you will see that it is certainly running.

play19:35

And since I was port forwarding when I built the container, I would like to acquire the data of MySQL on the container side from the local PC.

play19:45

As I said earlier, this video is not a MySQL commentary video, so I will omit the detailed explanation of the Python code,

play19:52

but I will put a library that connects to MySQL from Python called pip install mysql-connector-python.

play20:00

And with this code, use this password as a root user and access the local host, that is, the 13306 port of this computer.

play20:10

Since I was port forwarding earlier, the communication to the local PC 13306 is transferred to the port of the container side 3306,

play20:18

so it means that it connects to the MySQL on the container side.

play20:23

And if you move this program, which is to take the data of the users table that I made earlier and display it,

play20:32

to the local PC side, you can see that the data is taken properly like this.

play20:38

I will stop the container with docker stop because I will no longer use this container.

play20:45

If you want to delete it, use the docker rm command.

play20:50

Docker rm supu-mysql.

play20:54

If you execute this, the container itself that you used so far will be deleted.

play20:59

Also, if you delete the container image that you have pulled,

play21:03

you can see that the container image that you have pulled is deleted by using the docker rmi.

play21:10

If you delete the container image that you have pulled,

play21:13

you can delete the container image by specifying the image name and version with docker rmi.

play21:25

If you check if there is no image left in the docker images, you can see that there is no image left at all.

play21:33

How about it?

play21:34

Did you get an idea of how to think and operate the container?

play21:38

Let's sort out what we've done so far around containers.

play21:41

First, if you put a docker in a local PC, you can create a virtual environment container on top of it.

play21:48

Containers are created from the container image,

play21:52

but this container image is a container image that can be built with containers that contain MySQL,

play21:58

and a container image that can be built with containers that contain NGINX.

play22:03

Since various images are generally released in the Docker Hub,

play22:07

you can download them from there, get the image, create and start the container.

play22:13

You can also enter the docker exec into the container and directly operate it,

play22:18

or you can also connect to the container app through the port from the local PC by port forwarding.

play22:25

I explained a lot, including the actual performance,

play22:28

but for the first time, my head may be confused,

play22:31

but I think that while operating several times, I can understand it as,

play22:36

oh, that's what it is, and when I remember the operation command,

play22:39

I think it's convenient to build the necessary container on my PC and use various things.

play22:46

Next, I would like to explain how to create an image from a Docker file and build a container.

play22:53

Until earlier, the container image was used as it was,

play22:59

but you can also create a customized container image based on that image,

play23:02

instead of using the container image as it is.

play23:08

The Docker file is the one that can generate that customized container image.

play23:13

The Docker file is just a text, but first create a file called Docker file.

play23:20

Next, specify the image of the container that will be the base.

play23:24

This is to specify the image on the Docker Hub,

play23:28

but I think that it is often used to make a container image of a specific OS that does not contain anything.

play23:34

For example, if you want to create a container image of 20.4 of Ubuntu,

play23:39

search for Ubuntu in the Docker Hub,

play23:44

and click on Ubuntu with the Docker official image,

play23:49

and open the tab called Tags in this,

play23:54

you will see that there are some versions, so if you look at the bottom,

play24:01

you can see that there is 20.4.

play24:04

So, copy the command in the pull command and write from ubuntu colon 20.04 in the Docker file.

play24:14

This will be able to base the container image of Ubuntu 20.04.

play24:20

There are several commands for Docker files like from,

play24:26

but we will customize the container image using these commands.

play24:30

Here are some of the representative commands.

play24:34

First, there is a command called run that executes the shell command

play24:37

when creating a container image.

play24:41

For example, if you leave this Docker file, it's an empty Ubuntu,

play24:46

and you want to install various packages with run apt install here.

play24:51

In that case, run is used.

play24:54

First, to update the apt itself,

play24:56

write run apt update and the shell command behind run.

play25:02

This time, I want to install Python 3.9, so I will use the run command in the same way,

play25:08

and write run apt install -y python 3.9.

play25:13

Originally, run apt install uses and to write the shell command in one liner,

play25:19

and it is more desirable to write a command to delete the cache at the end,

play25:24

but in this video, we are counting each line based on the ease of understanding of the command.

play25:29

In addition, I want to install the pip itself,

play25:34

so run apt install -y python3-pip and install pip.

play25:40

Next, I would like to install the library at once using the requirement.txt

play25:44

to install the Python library with pip.

play25:50

Even if there is a requirement.txt on the local side,

play25:54

you cannot install this file with pip unless this file is actually used on the container side.

play26:01

So, to copy the file on the local pc to the container side,

play26:06

use the command copy.

play26:08

Since it is a copy of the local file,

play26:14

there is a requirement.txt in the same folder with this Docker file,

play26:21

so write copy requirement.txt and half space dot.

play26:26

By the way, numpy and pandas are specified in this requirement.txt.

play26:31

In addition, I will install pip using that requirement.txt,

play26:36

run python 3.9-m pip install-r requirement.txt

play26:43

You can also change the user by using the user command.

play26:48

Since the user name is written as user half space,

play26:52

for example, write user half space root.

play26:57

You can also set the environmental variable by using the command ENV.

play27:02

Write nb.halfspace.environmental variable.

play27:08

For example, if you want to set the value of site domain to vtuber.supu.com,

play27:16

write ENV SITE_DOMAIN=vtuber.supu.com.

play27:21

You can also change the work directory,

play27:24

and if you specify the work directory and directory path,

play27:28

the directory will be the work directory.

play27:31

You can specify the corresponding path and absolute path.

play27:34

For example, write the work directory, absolute path var, etc.

play27:39

Also, it is very similar to copy,

play27:42

but there is an instruction that you can add files to the container side from the local pc.

play27:48

Add, add the original file path, half space, add the file path.

play27:55

This is quite similar to the copy I explained earlier.

play27:59

What is different is that the copy can not be copied to the container side

play28:02

by the URL specification of the file that is released on the net, for example.

play28:09

On the other hand, you can add a remote-like file to the container side.

play28:14

Also, when copying the compressed file to the container side,

play28:17

it will be copied as it is in the copy command,

play28:20

but it will be automatically returned by using add.

play28:23

So basically, I think it's good to use copy and add only if necessary.

play28:28

I would like to add the previous Ayame's public data to the container side.

play28:34

Add, if you write the url. of Ayame's data,

play28:39

an irisdata file will be added to the container side.

play28:44

There is also an ENTRYPOINT.

play28:47

This is a shell command that you want to move when you are running the container.

play28:51

After the ENTRYPOINT, open the half space and write it like a Python list,

play28:58

and then write the execution file, parameter 1, parameter 2.

play29:03

The difference from run is that run is executed when creating a container image,

play29:09

and the ENTRYPOINT is moved when the container execution of the Docker run is executed.

play29:14

This ENTRYPOINT is used when creating a container to execute a certain process

play29:18

such as handling the container as a file that can be executed using the ENTRYPOINT.

play29:27

For example, if you want to create a container only to execute a certain Python script,

play29:32

prepare a Python script, copy the Python script file to the container side,

play29:39

and write python3.9 script.py at the ENTRYPOINT, and enter the command to move this script.

play29:47

If you run Docker run with the image you made like this, the script.py will move at that time.

play29:53

And when this script.py is finished, the container will also be stopped.

play29:58

However, I want to check the results of various environments in the container,

play30:02

so I will delete the command for this ENTRYPOINT and check the operation later.

play30:09

There are several other commands, and the reference is on the official website for details,

play30:15

so I hope you can refer to it.

play30:18

I will put the reference URL in the summary column of the video.

play30:22

I'm going to create a container image with this Docker file I just wrote.

play30:28

To create a container image from a Docker file, use the Docker build command.

play30:33

Now I'm in a place where the Docker file is placed.

play30:36

Set the container image tag with a hyphen t behind the Docker build.

play30:43

It's the same as the MySQL colon latest and the Web2 colon 20.04 that I've mentioned so far.

play30:51

For example, if it's this time, add a tag like supu-python:1.0.

play30:58

You can also specify the path of the Docker file with hyphen f.

play31:02

This time, the place I'm in is the place where the Docker file is, so you can write it as a Docker file,

play31:09

but the default of hyphen f is a Docker file, so I will delete this option this time.

play31:16

Finally, specify the build context.

play31:19

All files of the directory specified in the build context will be read when creating an image.

play31:25

Of course, it's just read, so if you don't copy or add, the file will not be included in the image.

play31:32

However, it will take time to read each one, so I will not put unnecessary things in the build context.

play31:39

It's a little confusing, but basically move to the location where the Docker file is placed,

play31:44

check that there are no unnecessary files, and when building a Docker,

play31:49

specify the current directory, that is, the dot, in the build context.

play31:55

Then, when you run this, it takes a lot of time, but the image of the Docker is created like this.

play32:05

If you look at the image with Docker images, you can see that there is a supu-python:1.0.

play32:14

All you have to do is run Docker as before.

play32:17

However, the difference is that you have to add the hyphen it option to prevent the container from starting.

play32:24

Because the container is finished when the process is finished,

play32:30

for example, if you write an order that starts a process at the end of the Docker file,

play32:36

you don't need this hyphen it, but this time I didn't add it, so I'll add hyphen it.

play32:42

So, for example, the name of the Docker run hyphen hyphen name container,

play32:51

and then run it with hyphen it hyphen d, and then run it with the image of the supu-python:1.0.

play33:06

When you run this, the container id is displayed, and when you run the Docker piece,

play33:13

you can see that one container has been created.

play33:15

As before, enter the container with Docker exec hyphen it supu-server bash.

play33:26

Then, if you look at where you are in pwd, you can see that you are in the var folder specified in the work directory.

play33:35

In addition, if you run echo, dara, and site domain,

play33:42

you can see that the value specified in the Docker file is set to the environment variable of the site domain.

play33:48

In addition, if you run ls, you can see that there is iris.data brought from the web by the ad command.

play33:58

If you check the contents with the head, it looks like Ayame's data.

play34:04

If you move to the directory above one, you can see that there is a requirement.txt.

play34:14

And if you run python3.9, python can be executed, and if you run import pandas as pd,

play34:26

you can see that the library can be installed using the requirement.txt.

play34:32

Now you can create an image from the Docker file you made and start a container,

play34:37

and you can operate it.

play34:39

So, I will remove python and this container, and stop this container.

play34:48

I would like to use the ENTRYPOINT that I left earlier.

play34:52

In the script.py file of python script, it is written that the print python script was implemented.

play35:01

As I explained earlier, copy this script and set it to be implemented at the ENTRYPOINT.

play35:11

Now create a container image with Docker build again.

play35:18

Docker build-t supu-python, this time the version will be 1.1 and the build context will be defined as ..

play35:27

If you execute this, you can get the Docker image supu-python:1.0.

play35:35

Check the position with Docker images, and you can see that the image is made of supu-python:1.0.

play35:44

By the way, it takes a long time to build the first build, but Docker builds with difference,

play35:50

so the second supu-python 1.1 was built pretty early.

play35:58

When you run Docker, remove the background mode and the .it.

play36:06

The image is 1.1 of supu-python, and set the container name to supu-server 2.

play36:13

If you execute this, you can see that the python script has been implemented and is output, so you can see that the python script has been running.

play36:23

By the way, you can see that there is no supu-server 2 even if you do it like Docker ps.

play36:30

As I said earlier, when Python script is completed by executing the ENTRYPOINT,

play36:35

nothing has been created, so the container has been stopped.

play36:41

That's all for this time.

play36:43

There are still many things that Docker can do, but I hope you understand the basic operation explained this time and use Docker conveniently.

play36:53

See you in the next video.

play36:55

Bye bye.

Rate This

5.0 / 5 (0 votes)

Do you need a summary in English?