Working with Docker sometimes requires running containers not only on servers but also on a local machine. Below, we will look at how to run a Docker container on the local machine and how to use a Dockerfile.
Frequently Used Commands
First, let's review available commands for creating, managing, and running images and containers.
docker images: returns all available Docker images. The name, tag, and size will be shown for each one.
If the -q flag is set, the command will only show IDs without detailed information. This option is helpful if the resulting list is passed to the next command using the pipe. For example, a user wants to remove all images. It can get a list of their IDs (as shown in the example below) and pass it to the rmi command.
Example:
shell
1docker images -q2
39e3c1b91a44d49f35e15e12c657fe72c486b916fec2740c5178798028cf281bb8fd2d445ddcc39ab33eacc8251 docker build: builds an image with specified parameters and flags.
Example:
shell
1docker build -t <image-name>:<image-tag>docker pull: downloads an image from a remote source to your local machine.
Example:
shell
1docker pull <image-name>docker run: runs a new container from the selected image. It also allows you to configure additional options and run configurations, like running mode or port mapping.
Example:
shell
1docker run -d -p 8080:80 <image-name>The -d flag allows the user to launch the container in the background (the so-called “detached mode”). If it is successfully launched, the command-line tool returns the ID and then the terminal prompt. The -p flag allows you to configure the mapping of the host (local machine) and virtual machine ports.
docker ps : It allows you to view a list of containers, as well as detailed information about each of them (ID, name, status, ports).
Example:
shell
1docker2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3f5d742a79f1b nginx "/docker-entrypoint.…" 11 days ago Up 11 days 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp epic_pasteurBy default, only running containers will be shown. To view the full list, the user needs to add the -a flag. With the -q flag, the command will only show container IDs without detailed information. This option is useful if the resulting list of containers is passed to the next command using the pipeline. For example, a user wants to start all containers. It can get a list of their IDs (as shown in the example below) and pass it to the docker start container command.
Example:
shell
1docker ps -a -q2f5d742a79f1b3c7a6d07bebe74f7d94c57e38a535d04aa077e860d43e40f304c769d00efdda9ddocker start <container-names-or-ids>: It is used to start the existing container(s).
It is possible to start them all at once by getting a list of them with the additional ps command using the pipeline.
Example:
shell
1docker start $(docker ps -a -q)docker stop <container-name-or-id> : It is used to stop running container(s). Example:
It is possible to stop all containers at once by getting a list of them with the additional ps command using the pipeline.
Example:
shell
1docker stop $(docker ps -a -q)docker rm <container-name> : It is used to remove selected container(s). Example:
It is possible to remove all containers at once by getting a list of them with the additional ps command.
Example:
shell
1docker rm $(docker ps -a -q)docker rmi <image-name> : Remove selected image(s).
It is possible to remove all images at once by using the additional images command to get a list of them.
Example:
shell
1docker rmi $(docker images -q)Dockerfile
The software development and deployment process often requires building new images repeatedly. This process may be automated using the so-called Dockerfile.
A Dockerfile is a regular text file that contains a set of commands (a script) for building an image.
Dockerfile example:
dockerfile
1# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.mjs file.2# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile3
4FROM node:22.12.0-alpine AS base5
6# Install dependencies only when needed7FROM base AS deps8# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.9RUN apk add --no-cache libc6-compat10WORKDIR /app11
12# Install dependencies based on the preferred package manager13COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./14RUN \15 if [ -f yarn.lock ]; then yarn --frozen-lockfile; \16 elif [ -f package-lock.json ]; then npm ci; \17 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \18 else echo "Lockfile not found." && exit 1; \19 fi20
21
22# Rebuild the source code only when needed23FROM base AS builder24WORKDIR /app25COPY /app/node_modules ./node_modules26COPY . .27
28# Next.js collects completely anonymous telemetry data about general usage.29# Learn more here: https://nextjs.org/telemetry30# Uncomment the following line in case you want to disable telemetry during the build.31# ENV NEXT_TELEMETRY_DISABLED 132
33RUN \34 if [ -f yarn.lock ]; then yarn run build; \35 elif [ -f package-lock.json ]; then npm run build; \36 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \37 else echo "Lockfile not found." && exit 1; \38 fi39
40# Production image, copy all the files and run next41FROM base AS runner42WORKDIR /app43
44ENV NODE_ENV production45# Uncomment the following line in case you want to disable telemetry during runtime.46# ENV NEXT_TELEMETRY_DISABLED 147
48RUN addgroup --system --gid 1001 nodejs49RUN adduser --system --uid 1001 nextjs50
51# Remove this line if you do not have this folder52COPY /app/public ./public53
54# Set the correct permission for prerender cache55RUN mkdir .next56RUN chown nextjs:nodejs .next57
58# Automatically leverage output traces to reduce image size59# https://nextjs.org/docs/advanced-features/output-file-tracing60COPY /app/.next/standalone ./61COPY /app/.next/static ./.next/static62
63USER nextjs64
65EXPOSE 300066
67ENV PORT 300068
69# server.js is created by next build from the standalone output70# https://nextjs.org/docs/pages/api-reference/next-config-js/output71CMD HOSTNAME="0.0.0.0" node server.js72
How to Run Dockerfile
To build an image using a Dockerfile, the following steps must be performed:
- Create your own Dockerfile or download an existing one
- Move the Dockerfile to the project directory in which you want to create the image
- Run the command-line tool and change the directory to the project directory
- Run the
buildcommand with the required flags and parameters
In this case, the build command automatically uses the Dockerfile.
Local Machine
Using the commands described above, running containers on a local machine will be very easy.
Download the Dockerfile or create one yourself, then move it to the project directory.
Using the command line, build an image:
shell
1docker build -t test-imageRun a new container using the created image:
shell
1docker run -p 9000:80 -t test-imageCheck the list of containers to make sure that the required one has been created and launched:
shell
1docker psConfigure the environment properties if necessary. For the virtual machine to work correctly with the container, you may need to configure its name, virtual machine and host addresses, and the user profile.
These steps will create an image named test-image, which could be launched later on a dedicated virtual machine running Linux. We configured container port 80 to map port 9000 on the Docker host. In the future, the operation of the created container can be stopped and restored using the stop and start commands, respectively.
Conclusions
Sometimes, you may need to run Docker locally on your machine.
You must create an appropriate image using the build command to do this. To automate and simplify this process, you can use the Dockerfile, which contains a set of commands for creating an image. You can create it yourself if there is no ready-made Dockerfile suitable for the project. It is important to move the Dockerfile to the same directory in which the build command will be executed.
When the image is successfully created, you should run it in a dedicated container using the run command. The container will automatically create a virtual machine running the Linux operating system.
When starting the container, you must specify the mapping for the host and virtual machine ports. You should also ensure that the environment properties are configured correctly.
