Docker Basics

Overview

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run, including libraries, system tools, code, and runtime. By using Docker, you can quickly deploy and scale applications into any environment and know your code will run. Running Docker on AWS provides developers and administrators a highly reliable, low-cost way to build, ship, and run distributed applications at any scale.

Docker collaborates with AWS to help developers quickly bring modern applications to the cloud. This collaboration helps developers use Docker Compose and Docker Desktop to leverage the same local workflow they use today to seamlessly deploy applications on Amazon ECS and AWS Fargate.

How Docker works

Docker works by providing a standard way to run your code. Docker is an operating system for containers. Similar to how a virtual machine virtualizes (removes the need to directly manage) server hardware, containers virtualize the server operating system. Docker is installed on each server and provides simple commands you can use to build, start, or stop containers.

AWS services like AWS Fargate, Amazon ECS, Amazon EKS, and AWS Batch make it easy to run Docker containers at scale.

Docker basic

In this lab, we execute basic Docker commands with AWS VSCode

  1. Check that the client and server are running with the command:
docker --version

Docker basic

  1. Docker containers are built from images.
  • Pull command
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • First, we use the docker pull nginx:latest command to pull the latest nginx image from Docker Hub.
docker pull nginx\:latest

Docker basic

  1. To check if pulling the image was successful (the image will be in the local machine Docker cache).
  • Usage
docker images [OPTIONS] [REPOSITORY[:TAG]]
  • Purpose is to list the images.
docker images

Docker basic

  1. Docker Daemon acts as the server, receiving RESTful requests from the Docker Client and executing them.
  • We use the command docker run -d -p 8081:80 –name nginx nginx:latest.
docker run -d -p 8081:80 --name nginx nginx\:latest
  • -d: Run the container in the background (detached mode)

  • Name the container nginx

  • -p 8081:80: Expose port 80 of the container to port 8081 of the host machine

  • nginx:latest: The container is launched from the image nginx:latest

  • Reference command to run a container:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Docker basic

  1. Check the running nginx containers with the command:
docker ps
  • To list containers, we execute the command:
docker ps [OPTIONS]

Docker basic

  1. Use the command curl http://localhost:8081 to use the nginx container and verify it is running with index.html
curl http://localhost:8081
  • The curl command pattern is as follows:
curl [options/URLs]

Additionally, you can read more about CURL

Docker basic

  1. To view the logs of nginx and the container.
  • We use the command docker logs nginx. The curl request event appears.
docker logs nginx
  • Reference command to fetch the logs of a container:
 docker logs [OPTIONS] CONTAINER

Docker basic

  1. Next, execute the command docker exec -it nginx /bin/bash to interact with the container filesystem and constraints
docker exec -it nginx /bin/bash
  • Reference command to interact in a running container:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Docker basic

  1. We view the contents of nginx using the command:
cd /usr/share/nginx/html
cat index.html

Docker basic

  1. Enter exit to exit

Docker basic

  1. To stop running the container, we execute the command
docker stop nginx
  • Reference command to stop one or more containers:
docker stop [OPTIONS] CONTAINER [CONTAINER...]

Docker basic

  1. Use docker ps -a to view containers (including stopped containers). To restart, use the command: docker start nginx
docker ps -a

Docker basic

  1. Execute container removal (input is container ID or container name)
docker rm nginx
  • Reference command to remove one or more containers:
docker rm [OPTIONS] CONTAINER [CONTAINER...]

Docker basic

  1. Execute the removal of the nginx image using the command (the format of the removed image can be name:tag or IMAGE ID)
docker rmi nginx\:latest
  • Reference command to remove one or more images:
 docker rmi [OPTIONS] IMAGE [IMAGE...]

Docker basic