メモ: Docker ＠ GCPコマンド

*Installing apps with native OS tool*

# Cloud shell - set compute/zone
gcloud compute zones list
gcloud config set compute/zone <zone>

#Cloud shell - launch a new VM instance
gcloud compute instances create ubuntu \
* * 

#Cloud shell - log into the VM instance
gcloud compute ssh ubuntu

#VM instance - update packages and install nginx
sudo apt-get update
sudo apt-get install nginx
nginx -v

#VM instance - start nginx
sudo systemctl start nginx

#Check that it's running
sudo systemctl status nginx
curl http://*** . * . * . *

-----
*install /specific versions*

## VM instance
#Start nginx and check nginx status
sudo systemctl start nginx
sudo systemctl status nginx
sudo ps aux | grep nginx

#Check nginx config file
cat /etc/init/nginx.conf

#Stop nginx
sudo systemctl stop nginx


-----
*Installing Images with Docker*
##Commands to run (on the VM Instance)
#Install Docker
sudo apt-get install docker.io

#Check Docker images
sudo docker images

#Pull nginx image
sudo docker pull nginx:*.**.*
sudo docker images

#Verify the versions match
sudo dpkg -l | grep nginx

# update the VM instance:
sudo apt-get update
sudo apt-get install nginx


-----
*Running Images with Docker*

# Run the  instance
sudo docker run -d nginx:*.**.*

#Check if it's up
sudo docker ps

#Run a different version of nginx
sudo docker run -d nginx:*.**.*
sudo docker run -d nginx:*.**.*

#Check how many instances are running
sudo docker ps
sudo ps aux | grep nginx

# List all running container processes
sudo docker ps

-To show ALL instances -> -a 
-To show just the numeric ID-> -q 
sudo docker ps -aq

#Inspect the container
sudo docker inspect \<CONTAINER ID >
or
sudo docker inspect \<COMMAND NAMES >

# Connect to the nginx using the internal IP
CN="\<COMMAND NAMES >"
CIP=\$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' \$CN)
curl  http://$CIP

# get all instance IDs and their corresponding IP addresses
sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' 
$(sudo docker ps -aq)

# Stop an instance
sudo docker stop \<cid>
or 
sudo docker stop $(sudo docker ps -aq)

# Verify no more instances running
sudo docker ps

# Remove the docker containers from the system
sudo docker rm \<cid>
or 
sudo docker rm $(sudo docker ps -aq)

-----
*Create new image*
#Install Go
wget https://storage.googleapis.com/golang/go*.*.*linux-amd64.tar.gz
rm -rf /usr/local/bin/go
sudo tar -C /usr/local -xzf go*.*.*linux-amd64.tar.gz
export PATH=\$PATH:/usr/local/go/bin
export GOPATH=~/go
#Get the app code
mkdir -p \$GOPATH/src/github.com/* * *
cd $GOPATH/src/github.com/* * *
git clone https://github.com/* * */* * *.git

#Build a static binary of the monolith app
cd (path to folder where app is stored)
go get -u
go build --tags netgo --ldflags '-extldflags "-lm -lstdc++ -static"'

# Create a container for the app
# Look at the Dockerfile
cat Dockerfile

# Build the app container
sudo docker build -t \<app name>:\<version> .

#List the \<app name> image
sudo docker images \<app name>:\<version>

#Run the app container and get it's IP
sudo docker run -d \<app name>:\<version>
sudo docker inspect \<container name or cid>
or
CID=\$(sudo docker run -d \<app name>:\<version>
CIP=\$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID})

#Test the container
curl \<the container IP>
or
curl $CIP

----
# Build the app
cd \$GOPATH/src/github.com/\<path to the app>/app
cd \<app name>
go build --tags netgo --ldflags '-extldflags "-lm -lstdc++ -static"'
sudo docker build -t \<app name>:\<version>) .
CID3=$(sudo docker run -d \<app name>:\<version>))

-----

*Push Images*
#See all images
sudo docker images

# Add own tag
sudo docker tag \<app name>:\<version> \<username>/\<new app name>:\<new version>)

# push images to Dockerhub, Login and use the docker push command
sudo docker login
sudo docker push \<username>/\<new app name>:\<new version>)