[Introduction to docker / docker-compose] The new corona made my house longer, and I wanted to switch, but I couldn't buy it because of the reseller, so I made something that could be Mario.

Purpose of this article

We are planning to use docker-compose in the video projection system of the video group in the circle, but since there are many non-engineers, we will practice + output to explain them.

Begin

This is Yoshitora. I wanted to create a video tool with docker-compose and raspi, so I will write an introduction to docker-compose ...

It may be difficult to read because it is the first post of qiita ... I'm sorry ...

Due to the new coronavirus, there are no new performances, and even if I was assigned to the laboratory, I had to work from home ... The faculty member in charge of the laboratory read a lot of English literature and told me to get used to it, but I couldn't do my best 24 hours a day.

I would like to buy a switch as a break and play a game ...

スクリーンショット (245).png

Damn it ... I want resellers and coronas to die ...

So I wanted to make it myself. ~~ People who made switches from scratch in the world, I think I'll burn or blow it up ~ ~

The instructor in charge wrote in slack that I should touch various technologies, so I'm thinking of using docker / docker-compose in the infrastructure of the video tools in the group, so I will touch it.

What to create this time

As a practice of docker / docker-compose that will definitely be used in the future on the Web system, I will make something using it,

I use docker-compose as a part-time job, but I haven't created anything by setting it from scratch, so I'll do my best ...

Speaking of switch, it's Nintendo, isn't it? Nintendo makes and puts out various games, right?

The most famous of them is Mario, isn't it? So I will make something that can be Mario.

Details of what you actually make

Speaking of Mario, you destroy blocks on the side of the road and scrape coins and items, right? So I will display the block on the PC and make something that makes a sound that you can get coins every time you click

About rough docker and docker-compose

docker / docker-compose is always handled in the Web world, isn't it? In a nutshell, I personally think it's a very strong tool to prevent environmental problems (I'm sorry if I make a mistake).

The hardest part of development is building the first environment, and it often happens that if you install a different version, it will not work. However, by using docker / docker-compose, you can build the environment (version etc.) once decided as it is, so that is not the case! !! ~~ Still, there is an error in windows 10 pro, rails and docker-compose ... ~~

Build docker / docker-compose

Even if you use docker / docker-compose, you will build those environments at first! This can't escape

This time we will do it with ubuntu 18.04 lts How to install ubuntu18.04lts If you are afraid that you may install it on one computer and erase the original data, you should use virtual box or something (If you don't understand, write a commentary article ...)

install docker

First, update and upgrade ubuntu and install the software required for docker installation. If you don't do this you can't do anything

instal-software


sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y \
     apt-transport-https \
     ca-certificates \
     curl \
     software-properties-common

A brief description of the installed software is as follows

software Commentary
apt-transport-https Security related. Is it software that enables https communication?
ca-certificates This is also related to security. Is it software that handles certificates related to SSL connection?
curl Communication related. Software that allows you to connect to the Internet by hand.
software-properties-common Is it related to software management?

If you can install it successfully, a lot of characters will appear and the process will end

Next, install GPG Public Key with the following command so that docker can be installed.

install-GPGkey


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

You can check if the key was installed successfully with the following command

check-install-key


sudo apt-key fingerprint 0EBFCD88

From the result of executing the command, if "Docker Release (CE deb) [email protected]" is written in the uid line, it is successful.

Next, set up the apt repository so that you can install it with apt-get. Basically, if you install docker stable, there is no problem so you can install it (there are other edges and tests !!)

Please note that the command differs depending on the faction (?) Of the CPU you are using ...

x86_64 (Intel or amd CPU)

x86_How to do in 64


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

armhf (ARM CPU)

How to do with armhf


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

Update apt-get to reflect the installation result

update


sudo apt-get update

At this point, you can install docker itself! You did it! You can install docker itself with the following command

docker-install


sudo apt-get install -y docker-ce

The installed docker cannot be handled without administrator privileges (sudo), which is very inconvenient. So let's grant administrator privileges

add-docker-sudo


sudo usermod -aG docker $USER

After that I will restart

docker hello world and brief commentary / image

You can do docker hello world (test if it was installed) with the following command

hello-world


docker run hello-world

If you see the following result with this command, it is successful! You did it!

docker-hello-world-result


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/

docker is based on the idea of immutable instructions. Immutable means immutable or unchanged, and instruction means instruction. In other words, if you decide on a command such as how to move only at the beginning, I will use it from the next time onwards.

docker works with the following image.

image.png

--Run a docker container on the docker engine (docker itself) --The docker container is a virtual environment that runs inside the docker engine. ――In other words, you create a new environment on top of the docker engine and run it on it.

The docker container works with the following image.

image.png

--Docker image is the procedure for building the environment (what to put in!) --Layer is the main environment construction --The base image is about the OS of the virtual environment (windows, os x, ubuntu etc ..)

Install docker-compose

Now install docker-compose! !! To install docker-compose, install the main unit directly with CURL, but since the installation destination is a place that requires administrator privileges, install with CURL with administrator privileges (sudo).

The following command is a command for installation, but since 1.25.5 in the command is the version of docker-compose, find the version you want to install from docker-compose github and rewrite the command to that version (latest) release is the latest version)

install-docker-compose


sudo curl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

Like docker, docker-compose is easier to grant administrator privileges, so grant it.

add-docker-compose-sudo


sudo chmod +x /usr/local/bin/docker-compose

This completes the docker-compose installation! You did it!

hello world of docker-compose and brief commentary / image

Let's do docker-compose hello world Create the following docker-compose.yml file

docker-compose.yml


version: '3.8'
services:
  hello:
    image: hello-world:latest

After creating it, execute the following command in the hierarchy where the file is located to launch docker-compose.

docker-compose-run


docker-compose up

The execution result is almost the same as docker's hello world It's not a bug that is too similar

One container can be handled by docker, while docker-compose can handle multiple containers. So if you rewrite docker-compose.yml as follows, you can use docker-compose to do hello world of multiple dockers at once (although you usually do not)

docker-compose.yml


version: '3.8'
services:
  hello:
    image: hello-world:latest
  hello2:
    image: hello-world:latest

If you can handle more than one, you can set up a database in addition to the site server, and the range of things you can do will increase at once,

About python and flask

About python

python is an easy-to-use scripting language with low learning costs There are 2 series and 3 series in python, but I think that 3 series is the mainstream

In python you can

--Machine learning --Web development --Microcomputer programming --Image processing, etc ...

You can do various things, it's strong

About flask

python web framework I feel that many people are more immersed in Django, but Django will be a large-scale development. Flask, on the other hand, can be developed on a small to medium scale. I think Flask is easier to handle even for the first time because the learning cost is lower I think the Documentation is also light and easy to read.

Run flask on docker-compose

Folder structure

folder/
       app.py
       Dockerfile
       docker-compose.yml

Creating a python file

Create the following files

app.py


from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello world"

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

Creating a Dockerfile

Dockerfile is a file that writes what to execute first when docker-compose is launched.

FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
RUN pip install flask
COPY . .
CMD ["flask", "run"]

Commentary

Create docker-compose.yml

The docker-compose.yml file is a file that sets the environment of the container on docker-compose.

docker-compose.yml


version: '3.8'
services:
  app:
    build: .
    ports:
      - "5000:5000"

Server startup

You can start it with the following command

docker-compose up

Once launched, go to http://0.0.0.0:5000 and you'll see "hello world"! I did it!

You can stop the startup with Ctrl + C

Commentary

By doing docker-compose up, the contents of Dockerfile will be executed in order from the top This time, "Install python image", "Create a directory on the container", "Set environment variable (ENV)", "Install required software", "Run", and so on (quite) It's rough and broken, but ...)

Creation

It was created-> https://github.com/KinoshitaYstr/mario Download from github and unzip If you launch with docker-compose up in the extracted folder and access http: // localhost: 5000, a box will be displayed Click on the box to get coins (Please let me know if you want a code explanation ...)

end

Everyone is Mario with this, yafu

Afterword

If you are glared at by the Nintendo Legal Department, keep this article private or delete it I also want resellers and Corona to be glared at by the Nintendo Legal Department or fined one Kyen. Please let me know if you make a mistake ... I'll fix it ...

Screenshot_20200501_213232_com.android.chrome.jpg Goodbye…

References

Material used

--Images from illustrations and sama (https://www.irasutoya.com/2017/10/blog-post_455.html) --Sound source from Mr. Taira Komori (https://taira-komori.jpn.org/freesound.html)

While I was writing this, I wondered what I was doing, but I wondered what I was doing ...

Recommended Posts

[Introduction to docker / docker-compose] The new corona made my house longer, and I wanted to switch, but I couldn't buy it because of the reseller, so I made something that could be Mario.
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
I made my own OSS because I wanted to contribute to it
I wanted to use the find module of Ansible2, but it took some time, so make a note
I tried to visualize the lyrics of GReeeen, which I used to listen to crazy in my youth but no longer listen to it.