Day 20 of #90DaysOfDevOps

Day 20 of #90DaysOfDevOps

Docker installation and configuration on Ubuntu

Download updates for already installed packages - sudo apt-get update

Update the already installed packages - sudo apt upgrade

Install Docker - sudo apt install docker.io

Check docker version - docker --version

Create a docker group if not present - sudo groupadd docker

Add current user to docker group - sudo usermod -aG docker $USER

Reboot your system for above changes to take effect - sudo reboot

Start docker - sudo systemctl start docker

Stop docker - sudo systemctl stop docker

Check Status of docker - sudo systemctl status docker

Enable docker on boot - sudo systemctl enable docker

INSTALLATION -- Docker Desktop is available for Mac, Linux and Windows https://docs.docker.com/desktop

General Docker commands

Get help with Docker, can also use –help on all subcommands - docker --help

Display system-wide information - docker info

List the port mappings for a container - docker port <container-name>

View resource usage statistics for one or more containers - docker stats

View the processes running inside a container - docker top

Save an image to a tar archive - docker save

Load an image from a tar archive - docker load

Manage Docker Images

List local images - docker images

Build an Image from a Dockerfile - docker build -t <image_name>

Build an Image from a Dockerfile without the cache - docker build -t <image_name> . –no-cache

Create image from running container - docker commit <container-name> <image-name>

Delete an Image - docker rmi <image_name>

Remove all unused images - docker image prune

To remove all the images forcefully - docker rmi -f $(docker images -q)

Docker Hub

Login into Docker - docker login -u <username>

Publish an image to Docker Hub - docker push <image_name>

Search Hub for an image - docker search <image_name>

Pull an image from a Docker Hub - docker pull <image_name>

Manage Docker Containers

List all running docker containers - docker ps

List all running and stopped/exited containers - docker ps -a

Run a container from a image - docker run <image> OR

docker run -d -it --name <container-name> -p <host-port>:<container-port> -e <environment-variable>=<value> <image-name>

To stop a running container - docker stop <container-name>

To start a stopped container - docker start <container-name>

To remove the stopped container - docker rm <container-name>

docker attach <container-name> - To access the container in interactive mode, it will connect to the standard input/output of the main process inside the container.

docker exec -it <container-name> /bin/bash - To access the container in interactive mode, it will create a new process in the containers environment.

docker inspect <container-name> - Display detailed information on one or more containers.

Fetch and follow the logs of a container: docker logs -f <container-name>

To stop all the running containers at once - docker stop $(docker ps -a -q)

To remove all the stopped containers at once - docker rm $(docker ps -a -q)

Docker Volumes

Create and manage volumes

Create a volume: docker volume create my-vol

List volumes: docker volume ls

Inspect a volume: docker volume inspect my-vol

Remove a volume: docker volume rm my-vol

Start a container with a volume

Docker Volume Mount

docker volume create my-vol2

docker run -d --name devtest --mount source=my-vol2,target=/app nginx:latest

docker run -d --name devtest -v my-vol2:/app nginx:latest

Bind Mount

#Create a directory : mkdir -p /path/to/directory/

docker run -d --name devtest --mount source=/path/to/directory/,target=/app nginx:latest

docker run -d --name devtest -v /path/to/directory/:/app nginx:latest

Using volumes from other containers

Create the first container

docker run -d --name db -v app_data:/data database-image:latest

Create the second container

docker run -d --name backup --volumes-from db backup-image:latest

Docker Networks

Create and manage Networks

Create a network: docker network create -d bridge my-network

List networks: docker network ls

Inspect a network: docker network inspect my-network

Remove a network: docker network rm my-network

Using network from other containers

Create the first container

docker run -d --name=my-container1 --network=my-network busybox

Create the second container

docker run -d --name=my-container2 --network container:my-container nginx

Docker Compose

Show version information and quit - docker-compose version

Build or rebuild services - docker-compose build

Create and start containers - docker-compose up

Stop and remove resources - docker-compose down

Start services - docker-compose start

Stop services - docker-compose stop

Pause services - docker-compose pause

Unpause services - docker-compose unpause

List containers - docker-compose ps

Execute a command in a running container - docker-compose exec

View output from containers - docker-compose logs

List images - docker-compose images