Recap the essential Docker commands

In this article, I would like to recap the essential Docker commands that will be used in the upcoming articles of this series of posts. What those essential Docker commands take part in this series is creating Docker Image that is optimized for the Google Cloud Run. I would like to explain the essential commands to achieve this focusing as little as I can.

What will be covered in this article

[ads]

In terms of the Docker commands, we would create the “Docker image” so that the Google Cloud Run could deploy with it. Even though we have to configure a little to optimize for the Google Cloud Run, those will be mentioned in the later articles.

The Commands covered in this article

[ads]

To create the Docker image, we can utilize the below commands only.

  • docker build
  • docker run (aimed for the local confirmation only)

Essential Docker Commands Recap

[ads]

docker build

The command docker build is a command that will create the “Docker image” based on the dockerfile which is located in the current directory.

docker build flow

usage

The usage of this command is as follows.

docker build [OPTIONS] PATH | URL | -

There are a couple of options though, I would like to use the following only in this series of articles.

  • --tag , -t: give a tag (name) to the Docker image

So, the docker build will be like this. Don’t forget to supply the “. (dot)” at the end of the command as an output PATH. The example is as follows.

docker build -t my-image .

This command creates the Docker image on the current directory with the name “my-image”.

https://docs.docker.com/engine/reference/commandline/build/


dockerfile

As mentioned earlier, docker build requires the “dockerfile” to execute. The dockerfile defines the behavior that the docker will do during docker build. In a nutshell, Docker will build based on this dockerfile.

As well as docker command options, there are so many options about dockerfile. But I would like to focus on the below command only. Because we can achieve the purpose, create Docker image for Google Cloud Run, with these only.

  • FROM
  • WORKDIR
  • COPY
  • RUN

Let’s get a little deep dive into these options next.

FROM

FROM tells Docker the base image, such as Alpine Linux or Node, to build with. The base images list is available from the docker hub. The summary is as follows.

The example is as follows.

FROM alpine:latest AS builder

WORKDIR

WORKDIR tells Docker the working directory. Docker will execute the several processes indicated with RUN or COPY and so on in this working directory. We can proceed with docker build without WORKIDIR, however, we should utilize this option to avoid unintended operations.

Note that this working directory is not for our PC but for the Docker image going to create. Therefore we don’t care about the directory tree that existed on our PC.

The example is as follows.

WORKDIR /app

COPY

COPY tells Docker to copy files or directories. This command is used for additional files other than the base image specified with FROM.

The format COPY is as follows.

COPY [src] [dest]

Note that the path indicated as [src] is the path on the PC where docker build executed. And the path indicated as [dest] is the path on the Docker container path where we are going to create as Docker image.

The example is as follows.

COPY package*.json ./

RUN

RUN tells Docker the command to execute. Since this command is expected on the Docker container on the working directory specified with WORKDIR, the command has to be included in the base image specified with FROM.

The example is as follows.

RUN npm install

In the next chapter, I would like to explain about docker run.


docker run

If you have a valid Docker Image, you can run it locally with the docker run the command. For example, if the Docker image has been created from the “Alpine Linux” base image, the image contains Nginx as well. Therefore, once you execute docker run, you can access Nginx via your browser.

docker run flow

docker run options

docker run has several options however, I would like to mention the following only which is used for the later article.

  • -d: Execute command as background service
  • -p: Specify the port of both local PC and Docker Container

The format regarding -p is as follows.

docker run -p [port of PC]:[port of Docker container]

“port of PC” means that the port number going to access with your browser on your PC. And, “port of Docker container” means the listen port of Docker container. It’s better to use examples for understanding. The example is as follows.

docker run -d -p 3000:80 my-image

Once this command has been executed, you can access the Docker container with port 3000 on your browser. Of course, the container has to work with port 80 itself.

This kind of networking is a sort of complicated thing, but since the default port of Nginx is 80, so only you have to think about is the port of your PC.

docker run -p option

Summary

[ads]

These are the required knowledge regarding the essential Docker commands for the later articles. In the next article, I would like to explain how to create the Docker Image optimized for the Google Cloud Run.

  • docker build
    • Create the Docker image based on the dockerfile
  • dockerfile
    • FROM specifies the base image
    • WORKDIR specifies the working directory on the container
    • COPY execute file copy from local PC to the container
    • RUN execute the command on the container
  • docker run
    • Enable to access the container on the local PC

Related articles