Table of contents
- Docker Compose
- What is YAML?
- Task-1
- 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.
- How to run Docker commands without sudo?
Docker Compose
Docker Compose is a tool that was developed to help define and share multi-container applications.
With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
What is YAML?
YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.
YAML is a popular programming language because it is 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.
To create a docker file, go to the directory in which all app files are saved, and start vim editor
vim docker-compose.yaml
write the following in vim editor:
version: "3.9"
services:
web_app:
container_name: "django-todo-app"
build: .
ports:
- 8000:8000
volumes:
- django-todo-volume:/app
my-db:
container_name: "django-mysql-db"
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "test@123"
volumes:
django-todo-volume:
The docker-compose YAML file is basically the build and run command clubbed together and written like a script in YAML format.
To run the docker-compose file, use command:
docker-compose up
But if you perform docker ps
, your container will not show as running.
so use:
docker-compose up -d
It is now visible in running operations.
Now copy the public key of the instance and use the following url
<publicIP>:<portassigned>
The app is now accessible!
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.
Search for the image to be pulled from docker hub using
docker pull <image>
Here we are using mysql
sudo usermod -a -G docker ubuntu
sudo reboot
Inspect the container's running processes and exposed ports using the docker inspect command.
Use docker ps to check all process IDs of running containers
docker ps
docker inspect <containerid>
Use the docker logs command to view the container's log output.
docker logs <containerID>
Use the docker stop and docker start commands to stop and start the container.
docker stop <containerID>
docker start <containerID>
Use the docker rm command to remove the container when you're done.
docker rm <containerid>
How to run Docker commands without sudo?
Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):
sudo usermod -a -G docker $USER
Reboot the machine.