Docker and basic setup

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Source: aws.amazon.com/docker

The simplest way to explain Docker is that it helps containerize your application so that anyone can run the application regardless of the machine they are using. Using Docker means to remove all the tedious work you need to do in order to set up, install and run the application. No more worries about missing files, software version mismatch, or different environment, …

1. Docker terms

There are some basic terms that we need to know when using docker.

a) Dockerfile

To containerize an application, it all starts with Dockerfile, which is a blueprint so that docker knows how to configure the environment to start your application.

b) Image

After Dockerfile was created, it’s then used to build the Image which contains OS, dependencies and your code. This is like a template for running your application.

c) Docker Hub

The Image can be uploaded to Docker Hub to share with the others, just like pushing code to Github. Anyone with access can pull Docker Image from DockerHub and run on their machine.

Docker Hub is a registry which contains repositories where your images are stored. There are other registries such as Amazon ECR, Github container registry, Azure container registry or your internal container registry.

d) Docker Container

An image itself doesn’t do anything, in order to run the application, the image has to be run as a container. A container is an isolated package which runs your code, can be scaled in cloud and it’s stateless (means if you close container, data will be lost).

2. Implementation

I assume that we have already installed Docker Desktop. It is like the dashboard for your docker where you can see containers, images and can also add configurations.

To check if docker was installed successfully or not, run docker in your terminal and see if it shows command options.

I’ll containerize my Nextjs application as an example.

a) Create Dockerfile

In this file we’ll add instructions so that Docker Image knows how to run our application. We can create a single-stage Dockerfile, multi-stage Dockerfile, or even create more than one Dockerfile then use docker-compose to combine them. There is no one-size-fits-all, so depends on your application, choose the one that is most suitable.

Most of the projects I have worked with use multi-stage Dockerfile since they require different base images, it also makes our builds faster, more efficient, and allows us to switch between production and development stages easily.

No matter which approach you decide to use, these are some of the most common instructions in a Dockerfile.

  • FROM : specifies the base image that the build will extend. This is the foundation of your application.
  • WORKDIR : this instruction specifies the “working directory” or the path in the image where files will be copied and commands will be executed. This is where you application live and docker command will run from
  • COPY <host-path> <image-path> : this instruction tells the builder to copy files from the host and put them into the container image.
  • RUN <command> : this instruction tells the builder to run the specified command.
  • EXPOSE <port-number> : this instruction sets configuration on the image that indicates a port the image would like to expose.
  • CMD [“<command>”, “<arg1>”] : this instruction sets the default command a container using this image will run.
# add Dockerfile to the root of your project
# You can go to docker hub and search for node to see supported tags

FROM node:22-alpine

WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD npm run dev

This file basically says: extend image from node image, copy project to app directory in container, container then run npm install, after that run npm run dev, later container will run this app and when we go to localhost:3000, we can see our app running.

b) Build Image

docker build .  is the simplest command to build the image from Dockerfile but people normally want to give a memorable name to their Image, or, so called tagging. A full image name has the following structure:

[HOST[:PORT_NUMBER]/]PATH[:TAG]

A build command with tag would look like this

docker build -t  docker.io/my-test-docker:latest . 

If you’re using Vscode and have Docker plugin installed, you can also right-click on Dockerfile, choose Build Image and follow instructions.

After Image was created, open your Docker desktop and you can see it there.

As I said earlier, this Image can be pushed to the container registry so that the others can pull and run the application on their machine using docker push command.

If you make changes to the app, you need to push it to docker after changing so Image in registry is the latest one.

c) Run container

The command to run the container from an image can be as below.

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

You can check this to see what the options are. Regardless of the port we defined in Dockerfile, we can always override it with the port we want to run on our local machine (like 5000 for example) and it’s also a good idea to name the container so that if you have more than 1 container, you can distinguish them.

docker run -p 5000:3000 --name testdocker my-test-docker

And again, if you’re running docker desktop, you can run Image and below setting board will appear, so you can set container name, port and other options without having to remember the command.

As you can see, the app runs on port 5000.

From next time, we just have to run the command to start docker, instead of creating container again.

# to start container, testdocker is my container name
docker start testdocker

# to stop container 
docker stop testdocker

3. Summary

That’s basically how you containerize an app, share and run it.
Let’s summarize the step:

First, install Docker Desktop
Second,
a. In case you are initiating a project of your own:
– Add Dockerfile
– Build Image
– Run container

b. In case you are pulling from a registry:
– Pull Image from registry
– Run container

Third, to update an Image, before pushing the updates to the registry, remove the running container of that image and run docker file once again.

関連記事

カテゴリー:

ブログ

情シス求人

  1. 登録されている記事はございません。
ページ上部へ戻る