Docker Compose:
Docker Compose is a tool for defining and running multi-container Docker applications. It allows users to configure multiple containers and their settings in a single "docker-compose.yml" file, and then start and stop all of the containers with a single command. This makes it easier to manage and deploy complex applications that consist of multiple components. Additionally, Compose also allows to define the network, volumes, and environment variables for the containers.
Docker Compose Commands
Here's a list of common Docker Compose commands:
docker-compose up
: Create and start containers.docker-compose down
: Stop and remove containers.docker-compose build
: Build or rebuild services.docker-compose start
: Start services.docker-compose stop
: Stop services.docker-compose restart
: Restart services.docker-compose logs
: View service logs.docker-compose ps
: List running containers.docker-compose exec
: Execute a command in a running container.
What is YAML?
YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format. It is often used for configuration files, data exchange between systems, and other uses where a plain-text format is required. YAML is designed to be easy for humans to read and write, and it is often used in place of JSON or XML for these types of applications.
YAML files use indentation and simple punctuation to indicate the structure of the data. For example, a list of items might be represented like this in YAML:
- item1
- item2
- item3
YAML also supports more complex data structures, such as nested lists and dictionaries, and it allows for the use of special characters, such as the # symbol for commenting.
YAML files use the file extension .yml or .yaml
It is also used in many applications, like ansible, kubernetes, docker-compose and more.
Task-1:
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.
We need to install docker-compose.
After installation of docker-compose, create docker-compose.yml file.
- Now run the docker-compose.yml file.
The “docker-compose up” command is used to start and run a Docker Compose application. It reads the configuration defined in the docker-compose.yml file and creates and starts the specified services (containers) and networks.
- Verify that application is working by accessing it in a web browser.
The “docker-compose down” command stops all the services and cleans up the containers, networks, and images.
Task-2:
- 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 (Hint- Use usermod command to give user permission to docker). Make sure you reboot instance after giving permission to user.
- Inspect the container's running processes and exposed ports using the docker inspect command.
- Use the docker logs command to view the container's log output.
- Use the docker stop and docker start commands to stop and start the container.
🌟 Use the docker rm command to remove the container when you're done.
To remove the container...
step 1: First, stop the container using thedocker stop <container_id>
command.
step 2: Then, remove the containerdocker rm <container_id>
command as shown below.first, you have to stop the container then you can remove the container :
so first you have to run the docker stop <container_id>
then remove the container using
docker rm<container_id>