Difference between revisions of "Docker"
Adelo Vieira (talk | contribs) |
Adelo Vieira (talk | contribs) (→Docker basics) |
||
(30 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | {{Sidebar}} <div style="margin-bottom:-50px"></div> | ||
+ | |||
https://www.docker.com/ | https://www.docker.com/ | ||
Line 4: | Line 6: | ||
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 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. | 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] | 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] | ||
− | + | <br /> | |
+ | ==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/ | ||
<br /> | <br /> | ||
+ | |||
==Why use Docker== | ==Why use Docker== | ||
* Capture all dependencies as code: | * Capture all dependencies as code: | ||
Line 27: | Line 39: | ||
<br /> | <br /> | ||
==Docker basics== | ==Docker basics== | ||
− | |||
− | + | <syntaxhighlight lang="bash"> | |
+ | docker --version | ||
+ | docker-compose --version | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Download Docker images from''' https://hub.docker.com/ | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker pull postgres | ||
+ | docker pull mongo | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''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: | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | : 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. | ||
+ | : <code>Dockerfile</code> | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | # 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"] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker build -t my-custom-mongo-image . | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker run --name my-custom-mongo-container -d -p 27018:27018 my-custom-mongo-image | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <br /> | ||
+ | '''Show all images in our machine:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker images | ||
+ | </syntaxhighlight> | ||
− | |||
− | |||
− | We should be able to see the Postgres container running. | + | <br /> |
+ | '''Show all containers in our machine:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker ps # running containers | ||
+ | docker ps -a # all | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | : We should be able to see the Postgres container running. | ||
[[File:Output_of_the_dockerpa-a_command.png|1000px|thumb|center|Output of the docker pa -a command]] | [[File:Output_of_the_dockerpa-a_command.png|1000px|thumb|center|Output of the docker pa -a command]] | ||
− | |||
− | |||
− | We can use the | + | <br /> |
− | + | '''Start a container:''' | |
− | + | : <syntaxhighlight lang="bash"> | |
+ | docker start <container_id/container_name> | ||
+ | </syntaxhighlight> | ||
+ | : Please note that the '''<code>run</code>''' command is used to create a new Docker container while the '''<code>start</code>''' command is used to start/run an existing container. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Stop a container:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker stop <container_id/container_name> | ||
+ | </syntaxhighlight> | ||
+ | : 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. | ||
+ | |||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker kill <container_id/container_name> | ||
+ | </syntaxhighlight> | ||
+ | : 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. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Remove a container:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker rm <container_id/container_name> | ||
+ | </syntaxhighlight> | ||
+ | : Please note that before you can remove a Docker container, you must stop it. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Remove a Docker image:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker rmi <image_id> | ||
+ | </syntaxhighlight> | ||
+ | : Please note that before you can remove a Docker image, you must stop and remove all its associated containers. | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Show logs of a container:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker logs <container_id/container_name> | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | '''Execute a command inside a running container:''' | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker exec <container_id/container_name> ls | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | : The following runs an interactive shell session within a running Docker container: | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker exec -it <container_id/container_name> /bin/bash | ||
+ | </syntaxhighlight> | ||
+ | : <code>-it</code>: enables an interactive terminal session. <code>-i</code> allows you to interact with the container, and <code>-t</code> allocates a pseudo-TTY terminal. | ||
+ | |||
+ | : We can use this commant to start the MongoDB shell, for example: | ||
+ | : <syntaxhighlight lang="bash"> | ||
+ | docker exec -it <container_id/container_name> mongosh | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | |||
+ | ==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. | ||
+ | |||
+ | : <span style="color:green">a '''Dockerfile''' is used to build custom images while '''Docker compose''' is used for defining and running containers</span> | ||
+ | |||
+ | |||
+ | Examples of '''docker-compose.yml''' can be fount at DockerHub. Here is an example docker-compose.yml for mongo https://hub.docker.com/_/mongo | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | # 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/ | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | 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: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | docker-compose up | ||
+ | docker-compose up -d | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | 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: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | docker-compose down | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | 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: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | docker-compose build | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | 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: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | docker-compose stop | ||
+ | </syntaxhighlight> | ||
+ | |||
− | + | 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: | |
− | + | <syntaxhighlight lang="bash"> | |
+ | docker-compose start | ||
+ | </syntaxhighlight> | ||
− | |||
− | + | 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: | |
− | + | <syntaxhighlight lang="bash"> | |
+ | docker-compose restart | ||
+ | </syntaxhighlight> | ||
− | |||
− | |||
− | + | 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: | |
+ | <syntaxhighlight lang="bash"> | ||
+ | docker-compose ps | ||
+ | </syntaxhighlight> | ||
<br /> | <br /> |
Latest revision as of 14:34, 3 June 2023
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.
Start a container:
docker start <container_id/container_name>
- Please note that the
run
command is used to create a new Docker container while thestart
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