How to build, run and debug Docker containers
Run the following command to take a look at the container image it pulled from Docker Hub:
docker images
Run the container
docker run hello-world
Finally, look at the running containers by running the following command
docker ps
Build Docker Container
Execute the following command to create and switch into a folder named test
mkdir test && cd test
Create a Dockerfile:
cat > Dockerfile <<EOF
# 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
Run the following to create the node application
cat > app.js <<EOF
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();
});
EOF
Note again the “.”, which means current directory so you need to run this command from within the directory that has the Dockerfile
docker build -t node-app:0.1 .
Now, run the following command to look at the images you built
docker images
RUN docker container
docker run -p 4000:80 --name my-app node-app:0.1
Test the server
curl http://localhost:4000
docker stop my-app && docker rm my-app
Now run the following command to start the container in the background
docker run -p 4000:80 --name my-app -d node-app:0.1
docker ps
Debugging Container
Notice the container is running in the output of docker ps. You can look at the logs by executing docker logs [container_id]
docker logs [container_id]
cd test
Edit app.js with a text editor of your choice (for example nano or vim) and replace “Hello World” with another string
cat > app.js <<EOF
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('Welcome to Cloud\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();
});
EOF
docker build -t node-app:0.2 .
Run another container with the new image version. Notice how we map the host’s port 8080 instead of 80. You can’t use host port 4000 because it’s already in use
docker run -p 8080:80 --name my-app-2 -d node-app:0.2
docker ps
Test the containers
curl http://localhost:8080
And now test the first container you made
curl http://localhost:4000
You can look at the logs of a container using docker logs [container_id]. If you want to follow the log’s output as the container is running, use the -f option
docker logs -f [container_id]
You can use docker exec to do this. Open another terminal (in Cloud Shell, click the + icon) and enter the following command
docker exec -it [container_id] bash
Look at the directory
ls
Exit the Bash session
exit
You can examine a container’s metadata in Docker by using Docker inspect
docker inspect [container_id]
Use — format to inspect specific fields from the returned JSON. For example
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_id]