Day 3 : Docker Session
Topics :
- Docker Images
- Docker architecture
- Docker Images - Commands
- Building Docker Images - Dockerfile
Docker Images
An image is a read-only template with instructions for creating a Docker container.
Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.
Docker architecture
Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon.
Docker internal working: dockerd (Docker daemon) => containerd (High level container runtime) => runc (Low level container runtime) => container
- Docker daemon The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.
- Docker client The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API internally.
- Docker Desktop Docker Desktop is an easy-to-install application for your Mac, Windows or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Kubernetes etc.
- Docker registries A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry. When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.
- Docker objects Docker have main following objects Images: read only template with instructions for creating docker containers Container: running instance of a docker image Network: network interface used to connect the containers to each other or external networks Volumes: used to persist the data generated by and used by the containers Registry: private or public collection of docker images
Docker Images - Commands
- docker build -t nginx-demo .
The docker build command builds an image from a Dockerfile and a context. The build’s context is the set of files at a specified location PATH or URL. The first thing a build process does is send the entire context (recursively) to the daemon. In most cases, it’s best to start with an empty directory as context and keep your Dockerfile in that directory. Add only the files needed for building the Dockerfile. To increase the build’s performance, exclude files and directories by adding a .dockerignore file to the context directory.
- docker image pull NAME[:TAG]
- docker image inspect NAME[:TAG]
- docker image ls
- docker image rm NAME[:TAG]
- docker image history NAME[:TAG]
- docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
- docker image push NAME[:TAG]
- docker image prune //Remove all dangling images docker image prune -a //Remove all dangling as well as unused images
Dangling image just means that you've created the new build of the image, but it wasn't given a new name. So the old images you have becomes the "dangling image". Unused image means that it has not been assigned or used in a container.
Building Docker Images - Dockerfile
The Dockerfile contains a series of instructions paired with arguments. Each instruction should be in upper-case and be followed by an argument. Instructions are processed from top to bottom. Each instruction adds a new layer to the image.
Dockerfile instructions
1. FROM - It initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
FROM can appear multiple times within a single Dockerfile to create multiple images or use one build stage as a dependency for another. Each FROM instruction clears any state created by previous instructions.
FROM <image>[:<tag>]
2. ENV - It sets the environment variable <key > to the value <value>. The environment variables set using ENV will persist when a container is run from the resulting image. You can change them using docker run --env <key>=<value>.
ENV <key>=<value>
3. RUN - It execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
RUN ["executable", "param1", "param2"]
4. EXPOSE - It informs Docker that the container listens on the specified network ports at runtime.
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.
EXPOSE <port>
5. WORKDIR - It sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
The WORKDIR instruction can be used multiple times in a Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction.
WORKDIR /path/to/workdir
6. COPY - It copies new files, directories or remote file URLs from <src > and adds them to the filesystem of the image at the path <dest>.
All new files and directories are created with a UID and GID of 0, unless the optional --chown flag specifies a given username, groupname, or UID/GID combination to request specific ownership of the content added.
COPY <src>... <dest>
7. CMD - It provide default arguments to the ENTRYPOINT or to specify a command to run if no ENTRYPOINT is specified. The arguments provided by CMD are optional and can be overridden when the container is run.
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
CMD ["param1","param2"] CMD ["executable","param1","param2"]
8. ENTRYPOINT - It defines the command that is executed when a container starts. You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.
ENTRYPOINT ["executable", "param1", "param2"]
Example
FROM nginx ENV APP_HOME=/usr/share/nginx/html WORKDIR $APP_HOME RUN rm -rf $APP_HOME/* COPY index.html index.html