Main Components
Components and Concepts
Docker Engine
Docker engine is the layer on which Docker runs. It’s a lightweight runtime and tooling that manages containers, images, builds, and more. It runs natively on Linux systems and is made up of: 1. A Docker Daemon that runs in the host computer. The Docker daemon is what actually executes commands sent to the Docker Client, like building, running, and distributing your containers. The Docker Daemon runs on the host machine, but as a user, you never communicate directly with the Daemon. 2. A REST API for interacting with the Docker Daemon remotely 3. A Docker Client that then communicates with the Docker Daemon to execute commands. The Docker Client is what you, as the end-user of Docker, communicate with. Think of it as the UI for Docker.
Source: Docker Documentation |
Docker CLI
Command | Description |
---|---|
docker attach | Attach local standard input, output, and error streams to a running container |
docker build | Build an image from a Dockerfile |
docker builder | Manage builds |
docker checkpoint | Manage checkpoints |
docker commit | Create a new image from a container's changes |
docker config | Manage Docker configs |
docker container | Manage containers |
docker context | Manage contexts |
docker cp | Copy files/folders between a container and the local filesystem |
docker create | Create a new container |
docker diff | Inspect changes to files or directories on a container's filesystem |
docker events | Get real time events from the server |
docker exec | Run a command in a running container |
docker export | Export a container's filesystem as a tar archive |
docker history | Show the history of an image |
docker image | Manage images |
docker images | List images |
docker import | Import the contents from a tarball to create a filesystem image |
docker info | Display system-wide information |
docker inspect | Return low-level information on Docker objects |
docker kill | Kill one or more running containers |
docker load | Load an image from a tar archive or STDIN |
docker login | Log in to a Docker registry |
docker logout | Log out from a Docker registry |
docker logs | Fetch the logs of a container |
docker manifest | Manage Docker image manifests and manifest lists |
docker network | Manage networks |
docker node | Manage Swarm nodes |
docker pause | Pause all processes within one or more containers |
docker plugin | Manage plugins |
docker port | List port mappings or a specific mapping for the container |
docker ps | List containers |
docker pull | Pull an image or a repository from a registry |
docker push | Push an image or a repository to a registry |
docker rename | Rename a container |
docker restart | Restart one or more containers |
docker rm | Remove one or more containers |
docker rmi | Remove one or more images |
docker run | Run a command in a new container |
docker save | Save one or more images to a tar archive (streamed to STDOUT by default) |
docker search | Search the Docker Hub for images |
docker secret | Manage Docker secrets |
docker service | Manage services |
docker stack | Manage Docker stacks |
docker start | Start one or more stopped containers |
docker stats | Display a live stream of container(s) resource usage statistics |
docker stop | Stop one or more running containers |
docker swarm | Manage Swarm |
docker system | Manage Docker |
docker tag | Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE |
docker top | Display the running processes of a container |
docker trust | Manage trust on Docker images |
docker unpause | Unpause all processes within one or more containers |
docker update | Update configuration of one or more containers |
docker version | Show the Docker version information |
docker volume | Manage volumes |
docker wait | Block until one or more containers stop, then print their exit codes |
Dockerfile
A Dockerfile is where you write the instructions to build a Docker image. It contains all the commands you would normally execute manually in order to build a Docker image.
Docker can build images automatically by reading the instructions from a Dockerfile. These instructions can be for example:
RUN apt-get y install some-package: to install a software package
EXPOSE 8000: to expose a port
ENV ANT_HOME /usr/local/apache-ant to pass an environment variable
…
Once you’ve got your Dockerfile set up, you can use the docker build command to build an image from it. Here’s an example of a Dockerfile:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=myapp-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Djava…","-jar","/app.jar"]
Source: Docker Documentation |
Docker Image
A Docker image is a collection of all of the files that make up a software application. Each change that is made to the original image is stored in a separate layer. To be precise, any Docker image has to originate from a base image according to the various requirements.
Each time you commit to a Docker image you are creating a new layer on the Docker image, but the original image and each pre-existing layer remains unchanged.
A base image has been illustrated here. Ubuntu is the base image, and a variety of desired capabilities in the form of functional modules can be incorporated on the base image for arriving at multiple images.
An important distinction to be aware of when it comes to images is the difference between base and child images.
- Base images are images that have no parent image, usually images with an OS like ubuntu, busybox or debian.
- Child images are images that build on base images and add additional functionality.
Then there are official and user images, which can be both base and child images.
- Official images are images that are officially maintained and supported by the folks at Docker. These are typically one word long. In the list of images above, the python, ubuntu, busybox and hello-world images are official images.
- User images are images created and shared by users like you and me. They build on base images and add additional functionality. Typically, these are formatted as user/image-name.
Images and Containers
A container is launched by running an image (containers are built off images). An image is an executable package that includes everything needed to run an application:
- the code
- a runtime
- Libraries
- environment variables
- configuration files
Since images are read-only, Docker adds a read-write file system over the read-only file system of the image to create a container.
Docker Container Lifecycle
Create container
Create a container to run it later on with required image.
Run container
Run the container with the required image and specified command / process. ‘-d’ flag is used for running the container in background.
Pause container
Used to pause the processes running inside the container.
Unpause container
Used to unpause the processes inside the container.
Start container
Start the container, if present in stopped state.
Stop container
To stop the container and processes running inside the container:
To stop all the running docker containersRestart container
It is used to restart the container as well as processes running inside the container.
Kill container
We can kill the running container.
Destroy container
Its preferred to destroy container, only if present in stopped state instead of forcefully destroying the running container.
To remove all the stopped docker containersSource: github.io |
Exercise
Develop - App
where the index.js
file is:
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello From NodeJS Container\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Ship - Image
The Dockerfile looks as follows (it uses alpine as the base image):
FROM alpine
RUN apk update && apk add nodejs
COPY . /app
WORKDIR /app
EXPOSE 8080
CMD ["node","index.js"]
build the hellojs-image:
Deploy - Container
run the hellojs-cotainer using node.js base images:
You can test your application by calling http://127.0.0.1:8080/
-d, --detach
: Run container in background and print container ID--rm
: Automatically remove the container when it exits-p, --publish list
: Publish a container's port(s) to the host--name string
: Assign a name to the container