Difference between revisions of "Docker"

From Sinfronteras
Jump to: navigation, search
(Docker basics)
(Docker basics)
Line 40: Line 40:
  
  
We can use the «docker ps» command to see all the containers in our machine:
+
Show all the containers in our machine:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo docker ps -a
+
docker ps
 +
docker ps -a
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 50: Line 51:
  
  
Each container and image has an associated ID that looks like the following:
+
Stop/Start a container:
c6f7dfc1a4c8
 
 
 
 
 
We can use the following command to stop a running Docker container:
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo docker stop INSERT_CONTAINER_ID_HERE
+
docker stop INSERT_CONTAINER_ID_HERE
sudo docker stop c6f7dfc1a4c8
+
docker start c6f7dfc1a4c8
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Please note that the run command is used to create a new Docker container while the start command is used to run an existing container.
  
  
Similarly, we can run a Docker container using the start command:
+
Remove a container:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo docker start c6f7dfc1a4c8
+
docker rm c6f7dfc1a4c8
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Please note that the run command is used to create a new Docker container while the start command is used to run an existingDocker container given its ID.
 
 
 
We can use the following command to remove a Docker container:
 
<syntaxhighlight lang="bash">
 
sudo docker rm c6f7dfc1a4c8
 
</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.  
  
  
The following command can be used to remove aDocker image:
+
Remove a Docker image:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo 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 12:10, 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.

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

We have already learned how to download Docker images using the «docker pull» command and how to create a container using the «docker run» command. We will now learn about other basic Docker commands.

docker --version


Show all the containers in our machine:

docker ps
docker ps -a

We should be able to see the Postgres container running.

Output of the docker pa -a command


Stop/Start a container:

docker stop INSERT_CONTAINER_ID_HERE
docker start c6f7dfc1a4c8

Please note that the run command is used to create a new Docker container while the start command is used to run an existing container.


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.