Day 18 Task: Docker for DevOps Engineers

What is Docker compose..?

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a single YAML file, making it easy to start and manage multiple Docker containers at once. With Docker Compose, you can configure your entire application stack and run it with a single command.

Docker Compose uses a declarative syntax to define your application’s services and their configurations. You can specify the Docker images to use, the ports to expose, the environment variables to set, and more. By defining all of this in a single file, you can easily manage and deploy your application across different environments.

Docker Compose also provides a command-line interface (CLI) for managing your containers. You can use the CLI to start, stop, and restart your containers, as well as to view their logs and status.

Overall, Docker Compose is a powerful tool for managing multi-container Docker applications, simplifying deployment and making it easier to manage complex applications.

What is YAML..?

YAML (short for “YAML Ain’t Markup Language”) is a human-readable data serialization format that is often used for configuration files, data exchange between programming languages, and other purposes. It was designed to be easily readable by both humans and machines.

YAML uses a simple syntax that is based on indentation and special characters to define data structures. It is commonly used for configuration files in web applications, such as for defining database configurations, server settings, or application parameters. It can also be used for more complex data structures, such as lists, dictionaries, and arrays.

One of the advantages of YAML is that it allows for comments, which makes it easier for developers to document their code and explain its purpose. Additionally, YAML files can be easily version controlled, making it easier to track changes and collaborate on projects.

Overall, YAML is a versatile and easy-to-use data serialization format that has become popular in many programming languages and applications.

Here is the today’s Tasks:-

01. Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

Setting up the Environment

First, you need to have Docker installed on your machine. Once you have Docker installed, you can create a docker-compose.yml file in the root directory of your project.

The docker-compose.yml file is a YAML file that defines the services that make up your application. Each service is defined as a separate block in the YAML file.

Configuring Services

For each service, you need to specify the image that you want to use, any environment variables that you want to set, and any volumes that you want to mount. Here is an example of a docker-compose.yml file that defines two services:

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

In this example, the web service is built using the Dockerfile in the current directory (.), and it exposes port 5000. The redis service uses the redis:alpine image from Docker Hub.

Linking Containers

If you have services that need to communicate with each other, you can use the links option to link the containers. For example, if you have a web application that needs to connect to a database, you can link the web container to the database container like this:

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    links:
      - db
  db:
    image: "postgres:alpine"

In this example, the web service is linked to the db service. This means that the web service can access the db service using the hostname db.

Using Environment Variables

You can use environment variables in the docker-compose.yml file by specifying them in the environment block for each service. Here's an example:

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    environment:
      DB_HOST: db
      DB_PORT: 5432
  db:
    image: "postgres:alpine"

In this example, the web service sets the DB_HOST and DB_PORT environment variables. These variables are then used by the web application to connect to the database.

Running the Application

To run the application, navigate to the directory containing the docker-compose.yml file and run the following command:

docker-compose up

This will start all of the services defined in the docker-compose.yml file. To stop the services, press Ctrl+C. If you want to run the services in the background, you can use the -d option:

docker-compose up -d

To stop the services running in the background, you can use the following command:

docker-compose down

This will stop and remove all of the containers created by docker-compose up.

02. How to Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user Make sure you reboot instance after giving permission to user..?

Here are the steps to pull a pre-existing Docker image from a public repository and run it on your local machine as a non-root user:

  1. Install Docker on your local machine if you haven't already done so.

  2. Open a terminal or command prompt and run the following command to pull the Docker image from Docker Hub:

docker pull <image-name>

Replace <image-name> with the name of the image that you want to pull. For example, to pull the official nginx web server image, you can run:

docker pull nginx

3. Once the image has been downloaded, you can start a container using the following command:

docker run --name my-nginx -d -p 8080:80 <image-name>

Replace <image-name> with the name of the image that you downloaded in step 2. This command starts a container in the background with the name my-nginx and maps port 8080 on your local machine to port 80 in the container.

4. To run the container as a non-root user, you first need to give your user permission to run Docker commands. To do this, you can use the usermod command:

sudo usermod -aG docker <username>

Replace <username> with your username. This command adds your user to the docker group, which allows you to run Docker commands without using sudo.

5. After running the usermod command, you need to reboot your machine for the changes to take effect:

sudo reboot

6. Once your machine has rebooted, you can stop the container that you started in step 3 using the following command:

docker stop my-nginx

7. Then, start a new container using the following command to run it as a non-root user:

docker run --name my-nginx -d -p 8080:80 -u 1000:1000 <image-name>

Replace <image-name> with the name of the image that you downloaded in step 2. The -u option specifies the user and group IDs to run the container as. In this example, it runs the container as user ID 1000 and group ID 1000.

8. Finally, you can check that the container is running as a non-root user by running the following command:

docker exec -it my-nginx id

This command runs the id command inside the container and displays the user and group IDs. If the container is running as a non-root user, the output should show the user and group IDs that you specified in step 7.

03. How to Inspect the container’s running processes and exposed ports using the docker inspect command.

Here are the steps to inspect a container’s running processes and exposed ports using the docker inspect command:

  1. First, start a container if you haven’t already done so. You can start a container using the following command:
docker run --name my-container -d -p 8080:80 nginx

This command starts a container in the background with the name my-container, maps port 8080 on your local machine to port 80 in the container, and uses the official nginx image.

2. To inspect the container’s running processes and exposed ports, run the following command:

docker inspect my-container

This command displays detailed information about the container in JSON format.

3. To view the container’s running processes, look for the ProcessLabel key in the output. The value of this key should be an array of running processes inside the container.

4. To view the container’s exposed ports, look for the HostConfig.PortBindings key in the output. The value of this key should be an object that maps the exposed ports in the container to the host ports that they are mapped to.

For example, if you used the -p 8080:80 option to map port 80 in the container to port 8080 on the host, the PortBindings object should have a key "80/tcp" with a value of an array containing an object with a "HostPort" key set to "8080".

That’s it! You can use the docker inspect command to view a wide range of information about a container, including its network settings, mounted volumes, environment variables, and more.

04. How to use the docker logs command to view the container’s log output..?

Here are the steps to use the docker logs command to view a container's log output:

  1. First, start a container if you haven’t already done so. You can start a container using the following command:
docker run --name my-container -d nginx

This command starts a container in the background with the name my-container using the official nginx image.

2. To view the container’s log output, run the following command:

docker logs my-container

This command displays the log output from the container. By default, it displays the entire log output from the container, starting with the most recent log entry.

3. You can use the -f option to follow the log output in real-time, similar to the tail -f command. For example, to follow the log output from the container, run the following command:

docker logs -f my-container

This command displays the log output from the container in real-time, and continues to display new log entries as they are generated.

4. By default, the docker logs command only displays the stdout log stream from the container. To display the stderr log stream instead, you can use the --stderr option. For example, to display the stderr log stream from the container, run the following command:

docker logs --stderr my-container

This command displays the stderr log stream from the container.

That’s it! You can use the docker logs command to view a container's log output, and use options like -f and --stderr to customize the output.

05. How to use the docker stop and docker start commands to stop and start the container..?

Here are the steps to use the docker stop and docker start commands to stop and start a container:

  1. First, start a container if you haven’t already done so. You can start a container using the following command:
docker run --name my-container -d nginx

This command starts a container in the background with the name my-container using the official nginx image.

2. To stop the container, run the following command:

docker stop my-container

This command stops the container by sending a SIGTERM signal to the main process inside the container. If the process does not exit gracefully within a certain amount of time (by default, 10 seconds), Docker sends a SIGKILL signal to force it to exit.

3. To start the container again after stopping it, run the following command:

docker start my-container

This command starts the container again with the same configuration that it had when it was originally started. Any changes made to the container’s filesystem or configuration while it was running will still be present.

That’s it! You can use the docker stop and docker start commands to stop and start containers as needed. You can also use the docker restart command to stop and start a container in one step.

06. How to use the docker rm command to remove the container when you’re done..?

Here are the steps to use the docker rm command to remove a container:

  1. First, stop the container if it’s still running. You can stop the container using the following command:
docker stop my-container

This command stops the container by sending a SIGTERM signal to the main process inside the container.

2. To remove the container, run the following command:

docker rm my-container

This command removes the container with the name my-container from your system. Any changes made to the container's filesystem or configuration while it was running will be lost.

That’s it! You can use the docker rm command to remove containers that you no longer need. Be careful when using this command, as it permanently deletes the container and its associated data.