Docker Machine (personal memorandum)

What is Docker Machine?

A tool that can be executed from the command line, such as creating, starting, stopping, and restarting a virtual machine equipped with Docker Engine. It is possible to start and manage a Docker host </ b> not only on a local PC but also on a remote cloud provider.

People using Docker for Mac, Docker Toolbox -Start a virtual machine with VirtualBox

People using Docker for Windows -Start a virtual machine with Hyper-V

How to create a Docker host on your local PC using Docker Machine

Download Docker Machine

% base=https://github.com/docker/machine/releases/download/v0.16.0 && curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine && chmod +x /usr/local/bin/docker-machine
Usage: docker-machine [OPTIONS] COMMAND [arg...]

Create and manage machines running Docker.

Version: 0.16.0, build 702c267f

Author:
  Docker Machine Contributors - <https://github.com/docker/machine>

Options:
  --debug, -D                                                   Enable debug mode
  --storage-path, -s "/Users/torigoshikouki/.docker/machine"    Configures storage path [$MACHINE_STORAGE_PATH]
  --tls-ca-cert                                                 CA to verify remotes against [$MACHINE_TLS_CA_CERT]
  --tls-ca-key                                                  Private key to generate certificates [$MACHINE_TLS_CA_KEY]
  --tls-client-cert                                             Client cert to use for TLS [$MACHINE_TLS_CLIENT_CERT]
  --tls-client-key                                              Private key used in client TLS auth [$MACHINE_TLS_CLIENT_KEY]
  --github-api-token                                            Token to use for requests to the Github API [$MACHINE_GITHUB_API_TOKEN]
  --native-ssh                                                  Use the native (Go-based) SSH implementation. [$MACHINE_NATIVE_SSH]
  --bugsnag-api-token                                           BugSnag API token for crash reporting [$MACHINE_BUGSNAG_API_TOKEN]
  --help, -h                                                    show help
  --version, -v                                                 print the version
  
Commands:
  active                Print which machine is active
  config                Print the connection config for machine
  create                Create a machine
  env                   Display the commands to set up the environment for the Docker client
  inspect               Inspect information about a machine
  ip                    Get the IP address of a machine
  kill                  Kill a machine
  ls                    List machines
  provision             Re-provision existing machines
  regenerate-certs      Regenerate TLS Certificates for a machine
  restart               Restart a machine
  rm                    Remove a machine
  ssh                   Log into or run a command on a machine with SSH.
  scp                   Copy files between machines
  mount                 Mount or unmount a directory from a machine with SSHFS.
  start                 Start a machine
  status                Get the status of a machine
  stop                  Stop a machine
  upgrade               Upgrade a machine to the latest version of Docker
  url                   Get the URL of a machine
  version               Show the Docker Machine version or a machine docker version
  help                  Shows a list of commands or help for one command
  
Run 'docker-machine COMMAND --help' for more information on a command.

The above will be displayed when the download is complete.

View Docker hosts

-#View docker host
% docker-machine ls

Docker host creation command

-# % docker-machine create —driver virtualbox Docker hostname
% docker-machine create —driver virtualbox default
-#View docker host
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

— Driver virtualbox uses virtualbox by default, so you don't need to add it. You can see that a docker host called "default" has been created.

How to connect to the docker host you created

-# docker-machine env docker hostname
% docker-machine env default
-#The setting command for setting to the docker host to be operated is displayed.
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/username/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval $(docker-machine env default)

If you set the environment variable of the export part, the operation target of the docker command will be set to the docker host specified in the argument of env. However, as described below, it can be executed collectively by executing ** eval $ (docker-machine env default) **.

Checking the docker host ACTIVE

-#View docker host
% docker-machine ls
-#"*" Is attached to ACTIVE, and you can check which docker host is ACTIVE.
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12


-#Docker run hello for confirmation-Start in world
% docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
Status: Downloaded newer image for hello-world:latest

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/

"Hello from Docker!" This means that it is running on the ** default host ** created this time, not the virtual machine such as Docker Mac that I have been using so far. You can also ssh to the host with ** docker-machine ssh **.

SSH connection to Docker host

-# % docker-machine ssh docker hostname
% docker-machine ssh default
docker@default:~$ docker ps -a                                                                                                                                               
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
ab7af80f762d        hello-world         "/hello"            4 minutes ago       Exited (0) 4 minutes ago                       cranky_mccarthy

Now you can see that the container has run on the docker host. Return to the original terminal with exit

Check the IP of the Docker host

-# % docker-machine ip docker host name
% docker-machine ip default
192.168.99.100

Start Nginx and check that the web page is returned by specifying the IP

-#Start nginx
% docker run -d -p 8080:80 nginx

After starting, check at 192.168.99.100:8080. It was started when the docker host was created, but if you want to do it explicitly, use ** docker-machine start ** or ** docker-machine stop **.

Stop Docker host

-# % docker-machine stop docker host name
% docker-machine stop default
-#View docker host
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL   SWARM   DOCKER    ERRORS
default   -        virtualbox   Stopped                 Unknown

It can be seen that the STATE has changed to ** "Stopped" ** and is stopped.

Start Docker host

-# % docker-machine start docker host name
% docker-machine start default
-#View docker host
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

You can see that the STATE has changed to ** "Running" ** and it is running.

If you do not specify the docker host as an argument, the Env, stop, and start commands will automatically operate as if the name ** default ** was specified.

Deselect ACTIVE docker machine

It can be canceled by deleting the environment variable set by the Docker-machine env command.

-# Docker-The command to delete the environment variable of machine is displayed.
% docker-machine env -u
unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
# Run this command to configure your shell: 
# eval $(docker-machine env -u)
-#Eval as set$(docker-machine env -u)You can cancel all at once with.
% eval $(docker-machine env -u)
-#Full view of docker host
% docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.12

You can see that ACTIVE becomes ** "-" ** and is released.

Recommended Posts

Docker Machine (personal memorandum)
Docker network (personal memorandum)
Docker memorandum
Docker memorandum
Creating a docker host on AWS using Docker Machine (personal memorandum)
Technical memorandum (Docker)
Memorandum docker command
Docker command memorandum
Launch docker container on EC2 (personal memorandum)
Building a CICD pipeline using Docker (personal memorandum)
Docker Machine command memo
Data management using volume in Docker (personal memorandum)
docker command personal summary
Data management using Docker bindmount and tmpfs (personal memorandum)
Construction of data analysis environment using Docker (personal memorandum)
What is Docker-compose? (Personal memorandum)
How to use arrays (personal memorandum)
memorandum
A memorandum of personal phpenv install
memorandum
docker
Rails environment construction with Docker (personal apocalypse)
[Personal] JUnit5 memorandum memo (work in progress)