1. Introduction to Docker

📒 link

Overview

  • What is Docker?

    • Docker is an open platform for developing, shipping, and running applications. With Docker, you can separate your applications from your infrastructure and treat your infrastructure like a managed application.

    • Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between writing code and running code.

Hello World

✔️ Run a hello world container

docker run hello-world

✔️ Take a look at the container image

docker images

✔️ Look at the runnung containers

docker ps

If you want to see all containers, including ones that have finished executing, run docker ps -a

Build

✔️ Build a Docker image based on a simple node application.

# Switch into a folder named test

mkdir test && cd test
# Create a Dockerfile

# Use an official Node runtime as the parent image
FROM node:lts
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Make the container's port 80 available to the outside world
EXPOSE 80
# Run app.js using node when the container launches
CMD ["node", "app.js"]
EOF
// Create the node appllication

const http = require("http");
const hostname = "0.0.0.0";
const port = 80;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World\n");
});
server.listen(port, hostname, () => {
  console.log("Server running at http://%s:%s/", hostname, port);
});
process.on("SIGINT", function () {
  console.log("Caught interrupt signal and will exit");
  process.exit();
});

Now build the image.

docker build -t node-app:0.1 .

Run

✔️ Run containers based on the image you built

docker run -p 4000:80 --name my-app:0.1

✔️ Stop and remove the container

docker stop my-app && docker rm my-app

✔️ Start the container in the background

docker run -p 4000:80 --name my-app -d node-app:0.1

✔️ Run another container with the new image version

Edit app.js

....
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Welcome to Cloud\n');
});
....

Build this new image and tag it with 0.2

docker build -t node-app:0.2 .

Run another container

docker run -p 8080:80 --name my-app-2 -d node-app:0.2

Debug

✔️ Look at the logs of a container

docker logs [container_id]

If you want to follow the log’s output as the container is running, use th -f option. docker logs -f [container_id]

✔️ Start an interactive Bash session inside the running container.

docker exec -t [container_id] bash

✔️ Examine a container’s metadata in Docker by using Docker inspect

docker inspect [container_id]

Publish

✔️ Find your project ID by running

gcloud config list project

✔️ Tag node-app:0.2.

docker tag node-app:0.2 gcr.io/[project-id]/node-app:0.2

✔️ Push image to gcr

docker push gcr.io/[project-id]/node-app:0.2

✔️ Check that the image exists in gcr

✔️ Stop and remove all container

docker stop $(docker ps -q)
docker rm $(docker ps -aq)

Remove the child images before you remove the node image

docker rmi node-app:0.2 gcr.io/[project-id]/node-app node-app:0.1
docker rmi node:lts
docker rmi $(docker images -aq) # remove remaining images

✔️ Pull the image and run it

docker pull gcr.io/[project-id]/node-app:0.2
docker run -p 4000:80 -d gcr.io/[project-id]/node-app:0.2

curl http://localhost:4000

Summary

  • Build, run, and debug docker containers.

  • Pull Docker images from Docker Hub and Google Container Registry.

  • Push Docker images to Google Container Registry.

Last updated