Day 18 Task: Docker for DevOps Engineers
Streamlining DevOps: Mastering Docker and Docker Compose for Efficient Multi-Container Application Deployment
Docker Compose:
Docker Compose is a tool that was developed to help define and share multi-container applications.
With a YAML file, you define services, and with one command, you bring your environment to life or gracefully shut it down. Dive deeper into Docker Compose.
What is YAML?
YML, often known as "yet another markup language," is the backbone of Docker Compose. It's a data serialization language that's both human-readable and easy to understand.
YAML files use a .yml or .yaml extension.
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.
sample docker-compose file-
version: '3.3'
services:
db:
image: postgres
container_name: postgres_db
restart: always
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydatabase
web:
image: nginx
build: ./webapp
depends_on:
- db
container_name: nginx_web
restart: always
ports:
- "8080:80"
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.SSH into the instance.
- Then update your packages using the command below:
sudo apt update
- Check if Docker is installed, if not, install it using the command below:
sudo apt update install docker.io
- As the user "ubuntu," running
docker ps
is not possible since it is a non-root user. To address this, the user was added to the "docker" group.
- However,
docker ps
is still not working because the server hasn't been rebooted after granting permission.
- Now, Docker commands can be run without sudo. Confirm the user's addition to the docker group using the command:
id
- This command provides details about the current user's UID, GID, and groups.
- Next, pull the Nginx image from the Docker registry (e.g., Docker Hub) and verify it using the command:
docker pull nginx
docker images
- Create a container from the Nginx image, perform port mapping in the same command to run the application on port 8000:
docker run -d -p 8000:80 --name nginx nginx:latest
- Open the security group and enable port 8000 for the Nginx server.
- Now, our Nginx server is running on port 8000.
- Inspect the container's running processes and exposed ports using the command:
docker inspect nginx
Use the docker logs command to view the container's log output.
we can use --tail=n (number of line) we want to see in log.
docker logs nginx
docker log nginx --tail=50
Use the docker stop and docker start commands to stop and start the container.
docker stop nginx docker start nginx
Use the docker rm command to remove the container when you're done.
docker rm -f <cont-id> or <cont-name>
That's it for this Article.
Thank you for reading! ๐