Installing docker. Docker file, docker Compose and there is more ?

Noob Blogger
5 min readDec 30, 2022

--

Photo by Vlad Hilitanu on Unsplash

To install Docker on a Linux machine, you can follow these steps:

#Update your package manager's database:
sudo apt-get update

#Install the dependencies required to add a new repository over HTTPS:
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

#Add the Docker GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

#Add the Docker repository to your package manager's list of sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

#Update your package manager's database again:
sudo apt-get update

#Install Docker:
sudo apt-get install docker-ce docker-ce-cli containerd.io

#Start the Docker daemon:
sudo systemctl start docker

# Verify that Docker is installed and running:
sudo docker run hello-world

#This should download and run a test image, and display a message indicating that Docker is working correctly.

To install Docker on other platforms, visit the Docker website (https://www.docker.com/).

Docker Desktop:

Docker Desktop is a desktop application for Mac and Windows that provides an easy-to-use interface for running Docker containers. It includes tools for building and deploying applications, as well as a built-in container runtime and support for working with containerized applications. To install Docker Desktop on a Mac, you can follow these steps:

1. Download the Docker Desktop installer from the Docker website (https://www.docker.com/products/docker-desktop).
2. Double-click the installer file to start the installation process.
3. Follow the prompts to complete the installation. This may include agreeing to the Docker terms of service and selecting the components to install.
4. Once the installation is complete, click the Docker Desktop icon in the menu bar to launch the application.
5. Click the "Sign In" button in the top right corner of the Docker Desktop window to log in with your Docker ID.
6. After logging in, you should see a list of available Docker images and containers.
7. You can use the Docker Desktop interface to build and run containers, manage your images and containers, and access other Docker tools and resources.

What is a docker file ?

A Dockerfile is a text file that contains instructions for building a Docker image. It is used to define the base image, package dependencies, and any additional customizations that are needed to run the application. Here is an example of a Dockerfile that could be used to containerize a Java application:

# Use an existing Docker image as the base
FROM openjdk:8-jdk-alpine
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the required files from the host machine to the container
COPY . .
# Compile the application
RUN javac Main.java
# Run the application when the container starts
CMD ["java", "Main"]
#To build a Docker image from this Dockerfile:
docker build -t my-java-app .

This command will create a new Docker image called “my-java-app” from the instructions in the Dockerfile. The . at the end specifies the current directory, which should contain the Dockerfile and the files needed to build the image. Start a new container using following command:

docker run -it my-java-app

Checking details of a running docker container:

docker ps: This command lists all of the running containers on the host machine. The output will look something like this:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f7d7dd8e8e5f nginx "nginx -g 'daemon of…" 4 days ago Up 4 days 0.0.0.0:80->80/tcp nginx
3e68c3a8fbe1 mysql "docker-entrypoint.s…" 4 days ago Up 4 days 3306/tcp, 33060/tcp mysql
#Checking stats container with the ID f7d7dd8e8e5f: 
docker stats f7d7dd8e8e5f

#output:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f7d7dd8e8e5f my-java-app 0.00% 10.87MiB / 1.952GiB 0.54% 2.44kB / 1.05kB 0B / 0B 1

This shows the CPU and memory usage for the container, as well as the network and block input/output (I/O). You can also use the --no-stream flag to disable the real-time streaming of stats, and the --all flag to show stats for all containers:

docker stats --no-stream --all

#output
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
f7d7dd8e8e5f my-java-app 0.00% 10.87MiB / 1.952GiB 0.54% 2.44kB / 1.05kB 0B / 0B 1
3e68c3a8fbe1 mysql 0.00% 72.46MiB / 1.952GiB 3.65% 4.59kB / 2.01kB 0B / 0B 1

This can be useful for monitoring the resource usage of your containers and ensuring that they are running efficiently.

Docker Compose

Photo by weston m on Unsplash

Docker Compose is a tool for defining and running multi-container Docker applications. It allows developers to use a YAML file to define the services and dependencies in their application, and it provides a way to start and stop the entire application with a single command.
To use Docker Compose, you first need to create a docker-compose.yml file in the root directory of your application. This file should define the services that make up your application, as well as any dependencies or configurations that are needed to run the application. Example:

version: '3'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: password

This file defines two services: web and db. The web service is built from the current directory and exposes port 3000. The db service uses the mysql image and sets the MYSQL_ROOT_PASSWORD environment variable

To run a Docker Compose file, you can use the docker-compose command and specify the path to the docker-compose.yml file. For example, if your docker-compose.yml file is in the current directory, you can run it with the following command:


#running a docker compose file:
docker-compose up

#run them in the background with -d flag
docker-compose up -d

#view the logs for the services
docker-compose logs

#combined logs for all of the services in the docker-compose.yml
docker-compose logs -f

#To stop the services,
docker-compose down

Overall, Docker Compose provides a simple and convenient way to manage multi-container Docker applications. By using a docker-compose.yml file, you can define all of the services and dependencies needed to run your application, and use a single command to start and stop the entire application.

--

--

Noob Blogger

Hello! I am a blogger who is just starting out to share my thoughts and ideas. Please like, follow and comment for improvements. Add requests for new topics!