Difference between revisions of "Docker"

From Sinfronteras
Jump to: navigation, search
(Docker basics)
(Docker basics)
Line 42: Line 42:
  
 
'''Download Docker images from''' https://hub.docker.com/
 
'''Download Docker images from''' https://hub.docker.com/
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker pull postgres
 
docker pull postgres
 
docker pull mongo
 
docker pull mongo
Line 87: Line 87:
  
 
'''Show all images in our machine:'''
 
'''Show all images in our machine:'''
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker images
 
docker images
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 93: Line 93:
  
 
'''Show all containers in our machine:'''
 
'''Show all containers in our machine:'''
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker ps      # running containers
 
docker ps      # running containers
 
docker ps -a  # all
 
docker ps -a  # all
 
</syntaxhighlight>
 
</syntaxhighlight>
  
We should be able to see the Postgres container running.
+
: 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]]
Line 104: Line 104:
  
 
'''Start a container:'''
 
'''Start a container:'''
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker start <container_id/container_name>
 
docker start <container_id/container_name>
 
</syntaxhighlight>
 
</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.
+
: 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.
  
  
Line 123: Line 123:
  
 
'''Remove a container:'''
 
'''Remove a container:'''
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker rm c6f7dfc1a4c8
 
docker rm c6f7dfc1a4c8
 
</syntaxhighlight>
 
</syntaxhighlight>
Please note that before you can remove a Docker container, you must stop it.  
+
: Please note that before you can remove a Docker container, you must stop it.  
  
  
 
'''Remove a Docker image:'''
 
'''Remove a Docker image:'''
<syntaxhighlight lang="bash">
+
: <syntaxhighlight lang="bash">
 
docker rmi INSERT_IMAGE_ID_HERE
 
docker rmi INSERT_IMAGE_ID_HERE
 
</syntaxhighlight>
 
</syntaxhighlight>
Please note that before you can remove a Docker image, you must stop and remove all its associated containers.
+
: Please note that before you can remove a Docker image, you must stop and remove all its associated containers.
  
  
 
<br />
 
<br />

Revision as of 17:50, 2 June 2023



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


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


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 c6f7dfc1a4c8
Please note that before you can remove a Docker container, you must stop it.


Remove a Docker image:

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