Basic Docker

Basic Docker

Overview

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

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

How Docker works

Docker works by providing a standard method for running your code. Docker is an operating system for containers. In the same way that a virtual machine virtualizes (eliminating the need to directly manage) server hardware, containers virtualize the host’s operating system. Docker is installed on each host and provides simple commands that 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 the lab, we execute basic Docker commands with AWS Cloud9

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

Docker basic

  1. Docker container is built from image.
  • command pull
docker pull [OPTIONS] NAME[: TAG|@DIGEST]
  • First of all, 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 test pull the image successfully (image will be in the local machine Docker cache).
  • Usage
docker images [OPTIONS] [REPOSITORY[: TAG]]
  • Purpose of listing images.
docker images

Docker basic

  1. Docker Daemon acts as server, receives RESTful requests from Docker Client and executes.
  • We use the command docker run -d -p 8080:80 –name nginx nginx:latest.
docker run -d -p 8080:80 --name nginx nginx\:latest
  • -d: Run the container in the background

  • Name the container as nginx

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

  • nginx:latest: Container launched from image is nginx:latest

  • Reference command to run a container:

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

Docker basic

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

Docker basic

  1. Use command curl http://localhost:8080 to use nginx container and verify it is working with index.html
curl http://localhost:8080
  • Refer to the basic curl command:
curl [options/URLs]

Also you can read more about CURL

Docker basic

  1. To view nginx and container logs.
  • We use the docker logs nginx command. curl request event occurs
docker logs nginx
  • Refer to the fetch log command of the container:
 docker logs [OPTIONS] CONTAINER

Docker basic

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

Docker basic

  1. We perform the content view of nginx with the command:
cd /usr/share/nginx/html
cat index.html

Docker basic

  1. Use exit to get out

Docker basic

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

Docker basic

  1. Use docker ps -a to view container (stopped container) to restart use command: docker start nginx
docker ps -a

Docker basic

  1. Perform container deletion (input is container ID or container name)
docker rm nginx
  • Refer to the command to delete one or more containers:
docker rm [OPTIONS] CONTAINER [CONTAINER...]

Docker basic

  1. Perform nginx image deletion with command (deleted image format can be name: tag or IMAGE ID)
docker rmi nginx\:latest
  • Refer to the command to delete one or more images:
 docker rmi [OPTIONS] IMAGE [IMAGE...]

Docker basic