Setting up Docker on GCP and Deploying

  1. gcloud config set account [account_id]
  2. gcloud auth list
  3. gcloud config list project
  4. docker run hello-world
  5. docker run -it ubuntu bash
  6. docker ps # only running will be listed
  7. docker ps -a #include exited ones too
  8. Write docker file in a dir
  9. docker build -t node-app:0.1 .
  10. it will install node etc.. and creates a new image of the node
  11. Docker runs at 4000 and app uses port 80
  12. docker run -p 4000:80 — name my-app node-app:0.1
  13. use curl to check
  14. docker stop my-app && docker rm my-app
  15. docker run -p 4000:80 — name my-app -d node-app:0.1 # run app in the background

Logs

  1. docker logs [container_id]
  2. modify app.js
  3. docker build -t node-app:0.2 .
  4. docker run -p 8080:80 — name my-app-2 -d node-app:0.2
  5. docker ps
  6. both v1 and v2 will run but curl gives v2

Debug

  1. docker logs -f [container_id]
  2. docker exec -it [container_id] bash
  3. This will change user from current to user of the container with container_id which contains only Dockerfile and app.js
  4. exit

Metadata

  1. docker inspect [container_id]
  2. The JSON output is seen
  3. docker inspect — format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ [container_id]
  4. It fetches current IP address where the container is running
  5. use format to get specific fields from the JSON

Publish the image to Docker

  1. gcloud config list project
  2. docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2
  3. docker images
  4. docker push gcr.io/[project-id]/node-app:0.2
  5. it pushes to gcr.io/[project_id]/node-app

Stop and remove all containers

  1. docker stop $(docker ps -q)
  2. 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

curl http://localhost:4000

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store