Try   HackMD

Docker is a tool that allows developers to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development.

Terminology

In the last section, we used a lot of Docker-specific jargon which might be confusing to some. So before we go further, let me clarify some terminology that is used frequently in the Docker ecosystem.

  • Images - The blueprints of our application which form the basis of containers. In the demo above, we used the docker pull command to download the busybox image.
  • Containers - Created from Docker images and run the actual application. We create a container using docker run which we did using the busybox image that we downloaded. A list of running containers can be seen using the docker ps command.
  • Docker Daemon - The background service running on the host that manages building, running and distributing Docker containers. The daemon is the process that runs in the operating system which clients talk to.
  • Docker Client - The command line tool that allows the user to interact with the daemon. More generally, there can be other forms of clients too - such as Kitematic which provide a GUI to the users.
  • Docker Hub - A registry of Docker images. You can think of the registry as a directory of all available Docker images. If required, one can host their own Docker registries and can use them for pulling images.

Installing docker

#docker
sudo apt-get update
sudo apt-get -y install ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker run hello-world

# add group for docker
sudo groupadd docker
sudo usermod -aG docker $USER

#--------------------------------
# log out of your computer, and log back in
# Once, you are logged in again, run the next two commands:
# ---------------------------------------------
newgrp docker
docker run hello-world

Get the app

Before you can run the application, you need to get the application source code onto your machine.

  1. Clone the getting-started repository using the following command:

    ​​​​​​​ git clone <https://github.com/docker/getting-started.git>
    
  2. View the contents of the cloned repository. Inside the getting-started/app directory you should see package.json and two subdirectories (src and spec).

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Build the app’s container image

To build the container image, you’ll need to use a Dockerfile. A Dockerfile is simply a text-based file with no file extension that contains a script of instructions. Docker uses this script to build a container image.

In the app directory, the same location as the package.json file, create a file named Dockerfile.

In the terminal, run the following commands listed below.

Change directory to the app directory. Replace /path/to/appwith the path to your getting-started/app directory.

​​​​cd /path/to/app

Create an empty file named Dockerfile.

​​​​touch Dockerfile

Using a text editor or code editor, add the following contents to the :

​​​​# syntax=docker/dockerfile:1

​​​​FROM node:18-alpine
​​​​WORKDIR /app
​​​​COPY . .
​​​​RUN yarn install --production
​​​​CMD ["node", "src/index.js"]
​​​​EXPOSE 3000

Build the container image using the following commands:

In the terminal, change directory to the getting-started/app directory. Replace /path/to/app with the path to your getting-started/app directory.

​​​​cd /path/to/app

Build the container image.

​​​​sudo docker build -t getting-started .

The docker build command uses the Dockerfile to build a new container image. You might have noticed that Docker downloaded a lot of “layers”. This is because you instructed the builder that you wanted to start from the node:18-alpine image. But, since you didn’t have that on your machine, Docker needed to download the image.

After Docker downloaded the image, the instructions from the Dockerfile copied in your application and used yarn to install your application’s dependencies. The CMD directive specifies the default command to run when starting a container from this image.

Finally, the -t flag tags your image. Think of this simply as a human-readable name for the final image. Since you named the image getting-started, you can refer to that image when you run a container.

The . at the end of the docker build command tells Docker that it should look for the Dockerfile in the current directory.

Start an app container

Now that you have an image, you can run the application in a container. To do so, you will use the docker run command.

Start your container using the docker run command and specify the name of the image you just created:

​​​​ docker run -dp 127.0.0.1:3000:3000 getting-started

The -d flag (short for --detached) runs the container in the background. The -p flag (short for --publish) creates a port mapping between the host and the container. The -p flag take a string value in the format of HOST:CONTAINER, where HOST is the address on the host, and CONTAINER is the port on the container. The command shown here publishes the container’s port 3000 to 127.0.0.1:3000 (localhost:3000) on the host. Without the port mapping, you wouldn’t be able to access the application from the host.

After a few seconds, open your web browser to http://localhost:3000. You should see your app.

Go ahead and add an item or two and see that it works as you expect. You can mark items as complete and remove them. Your frontend is successfully storing items in the backend.

At this point, you should have a running todo list manager with a few items, all built by you.

If you take a quick look at your containers, you should see at least one container running that is using the getting-started image and on port 3000. To see your containers, you can use the CLI or Docker Desktop’s graphical interface.

Run the following docker ps command in a terminal to list your containers.

​​​​docker ps

Output similar to the following should appear.

​​​​CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS              PORTS                      NAMES
​​​​e8b3d0bc6b82   getting-started   "docker-entrypoint.s…"   About a minute ago   Up About a minute   127.0.0.1:3000->3000/tcp   recursing_bartik

You can stop the container with:

​​​​sudo docker stop <CONTAINER_ID>

Check the result with docker ps.

You're probably wondering if there is a way to run more than just one command in a container. Let's try that now:

​​​​docker run -it getting-started sh

Running the run command with the -it flags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want. Take some time to run your favorite commands. You can exit with CTRL+D.

Pulling

To get started, let's run the following in our terminal:

​​​​sudo docker pull busybox

The pull command fetches the busybox image from the Docker registry and saves it to our system. You can use the docker images command to see a list of all images on your system.

​​​​docker images

Further practice

https://docker-curriculum.com/