❌

Reading view

There are new articles available, click to refresh the page.

Tasks – Docker

  1. Install Docker on your local machine. Verify the installation by running the hello-world container.
  2. 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.
  3. 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)

TASK – The Botanical Garden and Rose Garden – Python SETS

  1. Create a set named rose_garden containing different types of roses: "red rose", "white rose", "yellow rose". Print the same.
  2. Add "pink rose" to the rose_garden set. Print the set to confirm the addition.
  3. Remove "yellow rose" from the rose_garden set using the remove() method. Print the set to verify the removal.
  4. Create another set botanical_garden with elements "sunflower", "tulip", and "red rose". Find the union of rose_garden and botanical_garden and print the result.
  5. Find the intersection of rose_garden and botanical_garden and print the common elements.
  6. Find the difference between rose_garden and botanical_garden and print the elements that are only in rose_garden.
  7. Find the symmetric difference between rose_garden and botanical_garden and print the elements unique to each set.
  8. Create a set small_garden containing "red rose", "white rose". Check if small_garden is a subset of rose_garden and print the result.
  9. Check if rose_garden is a superset of small_garden and print the result.
  10. Use the len() function to find the number of elements in the rose_garden set. Print the result.
  11. Use the discard() method to remove "pink rose" from the rose_garden set. Try to discard a non-existent element "blue rose" and observe what happens.
  12. Use the clear() method to remove all elements from the rose_garden set. Print the set to confirm it’s empty.
  13. Make a copy of the botanical_garden set using the copy() method. Add "lily" to the copy and print both sets to see the differences.
  14. Create a frozen set immutable_garden with elements "orchid", "daisy", "red rose". Try to add or remove an element and observe what happens.
  15. Iterate over the botanical_garden set and print each element.
  16. Use set comprehension to create a set even_numbers containing even numbers from 1 to 10.
  17. Given a list of flowers ["rose", "tulip", "rose", "daisy", "tulip"], use a set to remove duplicates and print the unique flowers.
  18. Check if "sunflower" is in the botanical_garden set and print the result.
  19. Use the intersection_update() method to update the botanical_garden set with only the elements found in rose_garden. Print the updated set.
  20. Use the difference_update() method to remove all elements in small_garden from botanical_garden. Print the updated set.

❌