liveBook · Manning

Container image

As explained in Road to Kubernetes

A container image is an immutable package containing an application, its dependencies, and the filesystem configuration needed to create a container.

A container image is a built package for running an application with its dependencies. A Dockerfile supplies the build instructions; a container builder turns those instructions and the application code into an image. A container runtime runs the image as a container. This packaging makes applications easier to share and deploy across machines. section 1.4

Try an existing image before building one: `docker run nginx` runs the NGINX image. If it is not on your machine, Docker pulls it from Docker Hub, a container registry. To reach the running web server from your browser, you also need a port-mapping flag. section 5.1.1

To make an image for your own application:

  • Choose a base image and version tag, such as `python:3.11.4` or `node:18.17.0`. Use that image in the Dockerfile’s `FROM` instruction. section 5.3.1 section 5.4
  • Copy in the application, install its dependencies, and specify how it starts. Build the image with `docker build`, give it a tag, then run that tag with `docker run`. section 5.4.2
  • Push a tagged image to Docker Hub when another machine needs it. You can publish both a version tag and `latest`; a GitHub Actions workflow can automate the build and push. section 5.5.1 section 7.3.1
Listing 5.13 Node.js Dockerfile
# Using the official Node.js image for the LTS version
FROM node:18.17.0

# Set our app's working directory
WORKDIR /app

# copy the primary source code
COPY ./src /app/src/

# copy the package.json and package-lock.json to
# ensure we use the same dependencies
COPY package.json /app/
COPY package-lock.json /app/

# copy the entrypoint.sh for running the app
COPY conf/entrypoint.sh /app/

# install dependencies
RUN npm install

# make the entrypoint.sh executable
RUN chmod +x entrypoint.sh

# run our app
CMD ["./entrypoint.sh"]

The Node.js Dockerfile shows the build recipe together: a versioned base image, copied source and dependency files, `npm install`, and the command that starts the app. section 5.4.2

A successful build does not guarantee that the container will run correctly. Test it before deployment. Dockerfile instructions also produce cached layers, which can speed later builds and transfers. If you build on an ARM machine for an x86 cloud server, check the build platform rather than assuming the image will run there. section 1.5 section 5.5.1

An image is the deployable package, not the whole deployment system. Docker can run one on a virtual machine; Kubernetes can deploy built images using a YAML manifest. Updating versions, scaling, and recovering from failures become harder when managing containers without Kubernetes. section 7.1 section 8.0 section 1.5

How Road to Kubernetes covers it

How deep it goes
Walks through pulling, building, tagging, running, and pushing images; leaves intricate Dockerfiles and self-hosted registries aside.
Examples
Uses an NGINX first run and versioned Python and Node.js images, including a complete Node.js Dockerfile.
What it assumes
Starts with basic Docker installation and commands rather than assuming prior container-operations knowledge.
What you can do after
Build it from scratch with a Dockerfile, then run, tag, and push it using Docker.
How it connects
Connects Dockerfiles, container registries, image tags, GitHub Actions, and Kubernetes manifests.

Also explained in

Compare how 25 books explain container image →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.