Navigate back to the homepage

Understand the concept of Images and Containers

A step by step guide to understanding the concept of Images and Containers in Docker

Ahmed Abdulrahman
Feb 13th, 2020 • ☕️ 2 min read
macostoolstools

Prerequisites

  • Basic command line and bash scripting knowledge

In docker, all images are defined within special text files called Dockerfile, and you need to define all the steps explicitly inside that file. Let’s look at the below example:

1# Pull from a base image
2FROM node:12-alpine
3
4# Copy the files from the current directory to app/
5COPY . app/
6
7# Use app/ as the working directory
8WORKDIR app/
9
10# Install dependencies (npm ci is similar to npm i, but for automated builds),
11# then build production client side React application
12RUN npm ci --only-production &&\
13 npm run build
14
15# Listen to the port
16EXPOSE 5000
17
18# Set Node server
19ENTRYPOINT npm run start

Let’s split this file into lines:

  • FROM - is the first command in the Dockerfile. Without this, we can’t build an image. It pulls the base image with the tag 12-alpine, which is a specific version of Node along with specific version of Alpine.
  • COPY - is used to copy files or directories from source host filesystem to a destination in the container file system.
  • WORKDIR - sets the working directory for subsequent commands. We can have multiple WORKDIR commands and will be appended with a relative path.
  • RUN - executes commands in a new layer on top of the existing image. Consider our example, it install npm dependencies, and build client side app with one RUN to avoid any additional layers.
  • EXPOSE - is a communication between the person who builds the image and the person who runs the container so basically just sets a port.
  • ENTRYPOINT - is used as an executable for the container. Consider our example, we are using ENTRYPOINT to execute npm run start command.

Once we have this file and named it as Dockerfile, we can just run this command to build our image:

1docker build . -t our-image-name:our-tag

this will build our image and once the build is done, we can run our image with one simple command:

1docker run -it our-image-name:our-tag --name webapp sh

This command will give us a shell session inside the container, which we can use to do whatever we want. To understand the concept a little bit better, just open another terminal session while keeping the one in the container running, and run docker ps:

1CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24a81c6a020c8 our-image-name:container-tag "/bin/sh" 44 seconds ago Up 44 seconds webapp

The one listed there is our currently running container, which we can see with the IMAGE column set to our new image. However, there is a small detail, if you exit the shell session inside the container by running exit or CTRL+D, our container will die and docker ps will give you an empty output. There is a simple explanation behind this behavior; when you have executed the runner command above as:

1docker run -it our-image-name:our-tag --name webapp /bin/sh

we have basically told Docker to start this container with the /bin/sh process as the main process inside the container, which means once your process is dead that’s what happens when you exit the shell session, our container will die simple as that.

We built a smiple project step by step with Dockerfile and looked at most of the instructions. Please look at the Docker docs if you need more info.

If you have enjoyed this article? drop a coffee ☕️ tip here or support me for less than the cost of a coffee

Thanks for reading!


Discuss on TwitterFollow @_ahmed_ab

Other things I've written

Clone any Private or Public repo from Organization

Have you ever wanted to clone all repos under your account or specific organization or even from the company where you work?

Feb 3rd, 2020 · 1 min read

Git Tips & Tricks 🔥

Learn how to improve your Git-based workflow with cool tips & tricks that can save you some time. It's time to clean up your command line!

Jan 8th, 2020 · 3 min read

© 2018–2020
Ahmed Abdulrahman

Contact
hello@aadev.me

Link to $https://twitter.com/_ahmed_abLink to $https://www.behance.net/ahmedabdulrahmanLink to $https://github.com/AhmedAbdulrahman