Tasks β Docker
- Install Docker on your local machine. Verify the installation by running the
hello-world
container. - Pull the
nginx
image from Docker Hub and run it as a container. Map port 80 of the container to port 8080 of your host. - Create a Dockerfile for a simple Node.js application that serves βHello Worldβ on port 3000. Build the Docker image with tag my-node-app and run a container. Below is the sample index.js file.
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
4. Tag the Docker image my-node-app
from Task 3 with a version tag v1.0.0
.
5. Push the tagged image from Task 4 to your Docker Hub repository.
6. Run a container from the ubuntu
image and start an interactive shell session inside it. You can run commands like ls
, pwd
, etc.
7. Create a Dockerfile for a Go application that uses multi-stage builds to reduce the final image size. The application should print βHello Dockerβ. Sample Go code.
package main import "fmt" func main() { fmt.Println("Hello Docker") }
8. Create a Docker volume and use it to persist data for a MySQL container. Verify that the data persists even after the container is removed. Try creating a test db.
9. Create a custom Docker network and run two containers (e.g., nginx
and mysql
) on that network. Verify that they can communicate with each other.
10. Create a docker-compose.yml
file to define and run a multi-container Docker application with nginx
as a web server and mysql
as a database.
11. Scale the nginx
service in the previous Docker Compose setup to run 3 instances.
12. Create a bind mount to share data between your host system and a Docker container running nginx
. Modify a file on your host and see the changes reflected in the container.
13. Add a health check to a Docker container running a simple Node.js application. The health check should verify that the application is running and accessible.
Sample Healthcheck API in node.js,
const express = require('express'); const app = express(); // A simple route to return the status of the application app.get('/health', (req, res) => { res.status(200).send('OK'); }); // Example main route app.get('/', (req, res) => { res.send('Hello, Docker!'); }); // Start the server on port 3000 const port = 3000; app.listen(port, () => { console.log(`App is running on http://localhost:${port}`); });
14. Modify a Dockerfile to take advantage of Dockerβs build cache, ensuring that layers that donβt change are reused.
15. Run a PostgreSQL database in a Docker container and connect to it using a database client from your host.
16. Create a custom Docker network and run a Node.js application and a MongoDB container on the same network. The Node.js application should connect to MongoDB using the container name.
const mongoose = require('mongoose'); mongoose.connect('mongodb://mongodb:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true, }).then(() => { console.log('Connected to MongoDB'); }).catch(err => { console.error('Connection error', err); });
17. Create a docker-compose.yml
file to set up a MEAN (MongoDB, Express.js, Angular, Node.js) stack with services for each component.
18. Use the docker stats
command to monitor resource usage (CPU, memory, etc.) of running Docker containers.
docker run -d --name busybox1 busybox sleep 1000 docker run -d --name busybox2 busybox sleep 1000
19. Create a Dockerfile for a simple Python Flask application that serves βHello Worldβ.
20. Configure Nginx as a reverse proxy to forward requests to a Flask application running in a Docker container.
21. Use docker exec
to run a command inside a running container.
docker run -d --name ubuntu-container ubuntu sleep infinity
22. Modify a Dockerfile to create and use a non-root user inside the container.
23. Use docker logs
to monitor the output of a running container.
24. Use docker system prune
to remove unused Docker data (e.g., stopped containers, unused networks).
25. Run a Docker container in detached mode and verify that itβs running in the background.
26. Configure a Docker container to use a different logging driver (e.g., json-file
or syslog
).
27. Use build arguments in a Dockerfile to customize the build process.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Docker Build Arguments!' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
28. Set CPU and memory limits for a Docker container (for busybox)