Introduction
Docker is a platform for developing, shipping, and running applications in containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be sure that the application will run on any other machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code. Official Documentation
It should be the default choice for deploying applications in a reliable and repeatable way, because all the dependencies are packaged in the container, and the container can be run in any machine that has Docker installed.
Installation
ubuntu
Test the installation
Docker Images
Containers are designed to run applications or services, requiring their images to include all necessary OS and application files. These images are typically small and stripped of non-essential components to maintain speed and efficiency. Images are composed of independent layers, with the image itself being a configuration object listing these layers and metadata. Each image and layer is identified by a unique crypto ID, which is a hash of their respective contents. Any change to the image or its layers alters these hashes, making images and layers immutable.
You can find pre-built images on the Docker Hub, a cloud-based registry service that allows you to download images and upload your own. Docker Hub is the default registry for Docker, but you can also use other registries.
Data Persistance
Docker Volumes
Volumes are directories that are outside of the container and exist as normal directories and files on the host filesystem. They are used to persist data generated by and used by Docker containers. Volumes are the preferred way to persist data generated by and used by Docker containers, because they are completely managed by Docker . Example
Bind Mounts
Bind mounts link a directory on the host machine to a directory in the container. Bind mounts are useful for sharing files between the host machine and the container, and for persisting data generated by and used by Docker containers. Example
Essential Commands
Pull image
Will download the image from the repository to the local machine
Pull image from official repo
Pull image from unnofficial repo
This will pull the image tagged as v2
from the tu-demo
repository within the namespace of ‘nigelpoulton’ Docker Hub account.
List images
Will list all images in the local machine
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
teste latest 0a019530d7f8 2 hours ago 94.9MB
alpine latest 324bc02ae123 5 days ago 7.8MB
ubuntu latest 35a88802559d 7 weeks ago 78.1MB
postgres latest f23dc7cd74bd 2 months ago 432MB
Remove image
Inspect image
Will show the metadata of the image
Run Container
iteractively
detached
interactively and detached
run command
forward port
port 3000 of the host machine will be forwarded to port 8080 of the container
set name
Exit the container terminal without terminating
Ctrl-PQ
List Containers
Will list all containers in the local machine
Running
All
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96138d70f4ec ubuntu:latest "/bin/bash" 6 minutes ago Up 6 minutes hardcore_jepsen
Stop Container
Does not destroy data stored in the container
Resume
Kill Container
Destroys data stored in the container