## To get started, if you want to run docker as non-root user
## Make the current user a member of docker group.
## Set docker as the secondary group for that user and then logout and re-login
sudo usermod -aG docker ${USER}
## get help on docker command or subcommands
docker --help
docker run --help
...
## Run a container image
## if the image does not exist locally, it will be downloaded from the dockerhub repository and then run
docker run hello-world
(command output) Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
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/
## List container images saved locally
docker images
(Command output) REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1815c82652c0 6 days ago 1.84 kB
## List running containers
docker ps
(command output) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
## List all containers, including those that finished executing
docker ps -a
(command output) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a7203d68bb9 hello-world "/hello" About a minute ago Exited (0) About a minute ago wizardly_buck
## For the following command to build a docker image, use the following files
## in your current directory
Filename
Contents
Dockerfile
# Use an official Node runtime as the parent image
FROM node:6
# 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"]
app.js
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();
});
## Build the container image, note there is a . (period) at the end
## to indicate using current working directory
## Run the container image just built above
## mapping the container's port 80 to port 4000 of the host that you are on now
docker run -p 4000:80 --name my-app node-app:0.1
(command output) Server running at http://0.0.0.0:80/
## test accessing the app on your local machine (from another terminal or SSH session) by
curl http://localhost:4000
(command output) Hello World
## To stop the above app (from another terminal)
## list the running container and confirm the NAME or ID
docker ps
(command output) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78b300012906 node-app:0.1 "node app.js" 3 minutes ago Up 3 minutes 0.0.0.0:4000->80/tcp, :::4000->80/tcp my-app
docker stop my-app
(command output) my-app
## remove the container
docker rm my-app
(command output) my-app
## we can run the container again as the image is already built and available locally
## we can run in the background by adding -d option
docker run -p 4000:80 --name my-app -d node-app:0.1
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e541a04923db node-app:0.1 "node app.js" 25 seconds ago Up 22 seconds 0.0.0.0:4000->80/tcp, :::4000->80/tcp my-app
## to view the container log
docker logs e541a04923db
(command output) Server running at http://0.0.0.0:80/
## to view and follow the log in realtime
docker logs -f e541a04923db
(command output) Server running at http://0.0.0.0:80/
## after making changes to your application code
## e.g. change the output from "Hello World" to "Good Morning World"
## you can build a new container image with a different tag (-t) e.g. version 0.2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16c65e7b2ddb node-app:0.2 "node app.js" 4 seconds ago Up 1 second 0.0.0.0:8080->80/tcp, :::8080->80/tcp my-app-2
d2c284301b72 node-app:0.1 "node app.js" 12 minutes ago Up 12 minutes 0.0.0.0:4000->80/tcp, :::4000->80/tcp my-app
## to run interactive bash session inside the running container
## To push docker image to a reposity like Google Container Registry (gcr.io)
## do it from a machine with docker and Gcloud sdk installed (gcloud command line)
## or from your GCP account's cloud shell
## Have your GCP project_id
## assuming we have the node-app:0.2 image built as above
## create a TAG target image on GCR repository for that docker image
docker tag node-app:0.2 gcr.io/your_gcp_project_id/node-app:0.2
docker images
(command output) REPOSITORY TAG IMAGE ID CREATED SIZE
node-app 0.2 f93f95d979b9 About a minute ago 884MB
gcr.io/fluent-sprite-326604/node-app 0.2 f93f95d979b9 About a minute ago 884MB
node-app 0.1 838564ea7a00 6 minutes ago 884MB
node 6 ab290b853066 2 years ago 884MB
## To push to GCR, we need to have docker configured with the credentials to authenticate with
## the container registry. Follow the instructions here, depending on your scenario
## Push our image to the registry