Docker

From Sinfronteras
Jump to: navigation, search



https://www.docker.com/

Docker hub: https://hub.docker.com/

Docker is a popular open-source platform for building, shipping, and running applications in containers. Containers are lightweight and portable environments that allow you to run applications and services with their dependencies isolated from the underlying host system.


Docker images are static snapshots or templates of a specific environment that are used to create and run Docker containers. A Docker image is a lightweight, standalone, and read-only package that contains everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and system tools. It is a snapshot or template of a specific environment that can be used to create and run containers. A Docker container is a running instance of an image.


Using Docker, developers can create containerized applications, package them with all the necessary dependencies, and ship them as a single unit that can be deployed on any system that supports Docker. Docker also provides a way to manage containerized applications at scale, with features such as container orchestration, automated builds, and version control.


Some of the benefits of using Docker include improved application portability, faster development cycles, and better resource utilization. With Docker, developers can focus on building and testing applications, while operations teams can easily deploy and manage them across different environments and infrastructure. [ChatGPT]



Installation

Last time I installed Docker and Docker Compose, I follwed ChatGPT and everything was perfect!

The official documentation to install it in Ubuntu is here: https://docs.docker.com/engine/install/ubuntu/



Why use Docker

  • Capture all dependencies as code:
  • Python requerements
  • OS dependencies.
  • Consistent dev and prod environment: You can use the same image for development and production. So you eliminate all the issues that can arrive when changing to another environment in production.
  • Easier collaboration: When you share your code with other developers you can be sure it will work. You eliminate all dependencies issues in another developer's machines.
  • Different version of Python / Different version of databases / Different version of SDK.



Docker basics

docker --version
docker-compose --version



Download Docker images from https://hub.docker.com/

docker pull postgres
docker pull mongo



Create a new Docker container:

In the case of the official images already built and available on Docker Hub, we you can directly pull and run the image:
docker run mongo
docker run -d mongo
docker run --name my-mongo-container    -d -p 27017:27017 mongo
docker run --name my-postgres-container -d -p 27015:27015 -e POSTGRES_PASSWORD=a1640774200 -e POSTGRES_USER=postgres -e POSTGRES_DB=demo_db postgres


The docker build command is used when you want to build a custom Docker image based on specific requirements, using a Dockerfile. So it is used to add customizations or extend the functionality of the image.
Dockerfile
# Use the official MongoDB image as the base
FROM mongo

# Add a custom configuration file
COPY my-custom-config.conf /etc/mongodb.conf

# Expose a custom port (optional)
EXPOSE 27018

# Set an environment variable (optional)
ENV MY_VAR=my_value

# Run a custom command (optional)
CMD ["mongod", "--config", "/etc/mongodb.conf"]
docker build -t my-custom-mongo-image .
docker run --name my-custom-mongo-container -d -p 27018:27018 my-custom-mongo-image



Show all images in our machine:

docker images



Show all containers in our machine:

docker ps      # running containers
docker ps -a   # all
We should be able to see the Postgres container running.
Output of the docker pa -a command



Start a container:

docker start <container_id/container_name>
Please note that the run command is used to create a new Docker container while the start command is used to start/run an existing container.



Stop a container:

docker stop <container_id/container_name>
This command is used to gracefully stop a running container by sending a termination signal (SIGTERM) to the main process inside the container. The container is given a chance to perform any necessary cleanup tasks before it stops.
docker kill <container_id/container_name>
This command is used to forcefully stop a running container by sending a kill signal (SIGKILL) to the main process inside the container. Unlike docker stop, there is no graceful shutdown period or cleanup process. The container is immediately terminated, and any running processes inside the container are abruptly stopped.



Remove a container:

docker rm <container_id/container_name>
Please note that before you can remove a Docker container, you must stop it.



Remove a Docker image:

docker rmi <image_id>
Please note that before you can remove a Docker image, you must stop and remove all its associated containers.



Show logs of a container:

docker logs <container_id/container_name>



Execute a command inside a running container:

docker exec <container_id/container_name> ls
The following runs an interactive shell session within a running Docker container:
docker exec -it <container_id/container_name> /bin/bash
-it: enables an interactive terminal session. -i allows you to interact with the container, and -t allocates a pseudo-TTY terminal.
We can use this commant to start the MongoDB shell, for example:
docker exec -it <container_id/container_name> mongosh



Docker compose

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It provides a simple and declarative way to define the services, networks, and volumes required for your application and manage their configuration and relationships.


With Docker Compose, you define your application's services, their dependencies, and other configuration details in a YAML file called docker-compose.yml. This file serves as a blueprint for your application, specifying the containers to be run, the images to use, the ports to expose, the environment variables to set, and more.


By using Docker Compose, you can orchestrate the creation, running, and management of multiple containers as a cohesive application. It simplifies the process of setting up complex multi-container environments, such as a web server with a separate database container, by automating the container creation and network setup.


Once you have defined the docker-compose.yml file, you can use the docker-compose command-line tool to manage the lifecycle of the containers. It allows you to start, stop, and restart the containers, as well as manage their logs, scale the services, and perform other operations.


It's important to understant the difference bettween a Dockerfile and a docker-compose.yml.

A Dockerfile contains a set of instructions to build a Docker image. It defines the steps and configurations needed to create an image that can be run as a Docker container. The Dockerfile includes instructions such as specifying a base image, adding files and dependencies, setting environment variables, exposing ports, and defining the default command to run when the container starts. You typically use the docker build command with the Dockerfile to build the image.
A docker-compose.yml is used for defining and running multi-container Docker applications. It is a YAML file that allows you to define multiple services, networks, and volumes for your application. The docker-compose.yml file includes details such as the services to run, their respective images, ports to expose, environment variables, network connections, and volume mappings.
a Dockerfile is used to build custom images while Docker compose is used for defining and running containers


Examples of docker-compose.yml can be fount at DockerHub. Here is an example docker-compose.yml for mongo https://hub.docker.com/_/mongo

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/


This command starts the containers defined in the docker-compose.yml file. It creates and starts the containers, networks, and volumes as necessary. If the images for the services are not present, it builds them before starting the containers:

docker-compose up
docker-compose up -d


This command stops and removes the containers, networks, and volumes defined in the docker-compose.yml file. It effectively stops the application and cleans up the resources created by the docker-compose up command:

docker-compose down


This command builds the images for the services defined in the docker-compose.yml file. It is useful when you have made changes to the service's Dockerfile or any other build-related configuration and want to rebuild the images before starting the containers:

docker-compose build


This command stops the running containers defined in the docker-compose.yml file without removing them. It halts the execution of the containers but keeps their state intact:

docker-compose stop


This command starts the stopped containers defined in the docker-compose.yml file. It resumes the execution of the containers from their previously saved state:

docker-compose start


This command restarts the containers defined in the docker-compose.yml file. It stops and starts the containers, ensuring any changes in the configuration or images take effect:

docker-compose restart


This command lists the containers managed by the docker-compose.yml file. It shows the status of the containers, including whether they are running, stopped, or exited:

docker-compose ps