Setting up Docker on GCP and Deploying
Commands to Create, Build, Run, Deploy and Publish to Docker Hub
gcloud config set account [account_id]
gcloud auth list
gcloud config list project
docker run hello-world
docker run -it ubuntu bash
docker ps # only running will be listed
docker ps -a #include exited ones too
- Write docker file in a dir
docker build -t node-app:0.1 .
- it will install node etc.. and creates a new image of the node
- Docker runs at 4000 and app uses port 80
docker run -p 4000:80 — name my-app node-app:0.1
- use curl to check
docker stop my-app && docker rm my-app
docker run -p 4000:80 — name my-app -d node-app:0.1 # run app in the background
Logs
docker logs [container_id]
- modify app.js
- docker build -t node-app:0.2 .
docker run -p 8080:80 — name my-app-2 -d node-app:0.2
docker ps
- both v1 and v2 will run but curl gives v2
Debug
docker logs -f [container_id]
docker exec -it [container_id] bash
- This will change user from current to user of the container with container_id which contains only Dockerfile and app.js
exit
Metadata
docker inspect [container_id]
- The JSON output is seen
docker inspect — format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ [container_id]
- It fetches current IP address where the container is running
- use format to get specific fields from the JSON
Publish the image to Docker
gcloud config list project
docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2
docker images
docker push gcr.io/[project-id]/node-app:0.2
it pushes to gcr.io/[project_id]/node-app
Stop and remove all containers
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
You have to remove the child images (of node:6) before you remove the node image. Replace [project-id]
docker rmi node-app:0.2 gcr.io/[project-id]/node-app node-app:0.1
docker rmi node:6
docker rmi $(docker images -aq) # remove remaining images
docker images
Pull our image from docker hub and run
docker pull gcr.io/[project-id]/node-app:0.2
it will take a while to download…
docker run -p 4000:80 -d gcr.io/[project-id]/node-app:0.2